checked_yaml 1.0.2

Pub Package

package:checked_yaml provides a checkedYamlDecode function that wraps the the creation of classes annotated for package:json_serializable it helps provide more helpful exceptions when the provided YAML is not compatible with the target type.

package:json_serializable can generate classes that can parse the YamlMap type provided by package:yaml when anyMap: true is specified for the class annotation.

@JsonSerializable(
  anyMap: true,
  checked: true,
  disallowUnrecognizedKeys: true,
  nullable: false,
)
class Configuration {
  @JsonKey(required: true)
  final String name;
  @JsonKey(required: true)
  final int count;

  Configuration({this.name, this.count}) {
    if (name.isEmpty) {
      throw ArgumentError.value(name, 'name', 'Cannot be empty.');
    }
  }

  factory Configuration.fromJson(Map json) => _$ConfigurationFromJson(json);

  Map<String, dynamic> toJson() => _$ConfigurationToJson(this);

  @override
  String toString() => 'Configuration: ${toJson()}';
}

When checked: true is set, exceptions thrown when decoding an instance from a Map are wrapped in a CheckedFromJsonException. The checkedYamlDecode function catches these exceptions and throws a ParsedYamlException which maps the exception to the location in the input YAML with the error.

void main(List<String> arguments) {
  var sourcePathOrYaml = arguments.single;
  String yamlContent;

  if (FileSystemEntity.isFileSync(sourcePathOrYaml)) {
    yamlContent = File(sourcePathOrYaml).readAsStringSync();
  } else {
    yamlContent = sourcePathOrYaml;
    sourcePathOrYaml = null;
  }

  final config = checkedYamlDecode(
      yamlContent, (m) => Configuration.fromJson(m),
      sourceUrl: sourcePathOrYaml);
  print(config);
}

When parsing an invalid YAML file, an actionable error message is produced.

$ dart example/example.dart '{"name": "", "count": 1}'
Unhandled exception:
ParsedYamlException: line 1, column 10: Unsupported value for "name". Cannot be empty.
  ╷
1 │ {"name": "", "count": 1}
  │          ^^
  ╵

1.0.2 #

  • Require at least Dart 2.3.0.
  • Support the latest package:json_annotation.

1.0.1 #

  • Fix readme.

1.0.0 #

  • Initial release.

example/example.dart

// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:checked_yaml/checked_yaml.dart';
import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable(
  anyMap: true,
  checked: true,
  disallowUnrecognizedKeys: true,
  nullable: false,
)
class Configuration {
  @JsonKey(required: true)
  final String name;
  @JsonKey(required: true)
  final int count;

  Configuration({this.name, this.count}) {
    if (name.isEmpty) {
      throw ArgumentError.value(name, 'name', 'Cannot be empty.');
    }
  }

  factory Configuration.fromJson(Map json) => _$ConfigurationFromJson(json);

  Map<String, dynamic> toJson() => _$ConfigurationToJson(this);

  @override
  String toString() => 'Configuration: ${toJson()}';
}

void main(List<String> arguments) {
  var sourcePathOrYaml = arguments.single;
  String yamlContent;

  if (FileSystemEntity.isFileSync(sourcePathOrYaml)) {
    yamlContent = File(sourcePathOrYaml).readAsStringSync();
  } else {
    yamlContent = sourcePathOrYaml;
    sourcePathOrYaml = null;
  }

  final config = checkedYamlDecode(
      yamlContent, (m) => Configuration.fromJson(m),
      sourceUrl: sourcePathOrYaml);
  print(config);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  checked_yaml: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:checked_yaml/checked_yaml.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
85
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on Aug 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:checked_yaml/checked_yaml.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
json_annotation >=2.2.0 <4.0.0 3.0.0
source_span ^1.0.0 1.5.5
yaml ^2.1.13 2.1.16
Transitive dependencies
charcode 1.1.2
collection 1.14.12
meta 1.1.7
string_scanner 1.0.5
term_glyph 1.1.0
Dev dependencies
build_runner ^1.0.0
build_verify ^1.1.0
json_serializable ^3.0.0
path ^1.0.0 1.6.4
test ^1.6.0
test_process ^1.0.1