pub repository
mirror.p-hatan.comchecked_yaml
2.0.4Generate more helpful exceptions when decoding YAML documents using package:json_serializable and package:yaml.
Cached in Iran · upstream_checksum
Readme
package:checked_yaml provides a checkedYamlDecode function that wraps 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)
class Configuration {
@JsonKey(required: true)
final String name;
final int count;
Configuration({required this.name, required 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) {
final sourcePathOrYaml = arguments.single;
String yamlContent;
Uri? sourceUri;
if (FileSystemEntity.isFileSync(sourcePathOrYaml)) {
yamlContent = File(sourcePathOrYaml).readAsStringSync();
sourceUri = Uri.parse(sourcePathOrYaml);
} else {
yamlContent = sourcePathOrYaml;
}
final config = checkedYamlDecode(
yamlContent,
(m) => Configuration.fromJson(m!),
sourceUrl: sourceUri,
);
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}
│ ^^
╵
Changelog
2.0.4
- Require Dart 3.8
2.0.3
- Require Dart 2.19
- Add topics
2.0.2
- Require
json_annotation^4.3.0 - Require Dart SDK
>=2.18 - Work-around for dart-lang/sdk#50756.
2.0.1
-
If
CheckedFromJsonExceptionis caught for a key missing in the source map, include those details in the thrownParsedYamlException. -
Correctly handle the case where
CheckedFromJsonException.messageisnull.
2.0.0
- BREAKING
checkedYamlDecodesourceUrlparameter is now aUri. - Require at least Dart
2.12.0-0.
1.0.4
- Allow
package:yamlv3.x.
1.0.3
- Require at least Dart
2.7.0. - Allow
package:json_annotationv4.x.
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.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add checked_yamlpubspec.yaml
name: checked_yaml
version: 2.0.4
description: >-
Generate more helpful exceptions when decoding YAML documents using
package:json_serializable and package:yaml.
repository: https://github.com/google/json_serializable.dart/tree/master/checked_yaml
topics:
- yaml
- json
- build-runner
- json-serializable
- codegen
environment:
sdk: ^3.8.0
resolution: workspace
dependencies:
json_annotation: ^4.3.0
source_span: ^1.8.0
yaml: ^3.0.0
dev_dependencies:
build_runner: ^2.0.6
build_verify: ^3.0.0
json_serializable: ^6.0.0
path: ^1.8.0
test: ^1.17.10
test_process: ^2.0.0
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 1.0.0 | 2019-05-02T19:49:25.293340Z | Cached | 02e2d5a8af7d2ed5b01f4b846ecce395f0f347e504e5761dd0c9b42c8c7dfec8 |
| 1.0.1 | 2019-05-02T20:18:41.615293Z | Cached | 02cf3fda71144b22cf2d217dcafe383d0dd4f8a19892d174a1bcf2d48b00c563 |
| 1.0.2 | 2019-08-07T23:41:01.597373Z | Cached | f49720937533374a869cc6da763346cee518df5d47db635311bb9b73a1de2b43 |
| 1.0.3 | 2020-12-08T20:51:08.871351Z | Cached | 699acca807f494950a07e4e41d371c039b2eeed55bf44c97bdfe20a5f0e0ffe6 |
| 1.0.4 | 2020-12-09T00:25:42.504408Z | Cached | d051b31d88264beeeef1fa971f19d9a1d614b157bc60dad5b3b360f996e48656 |
| 2.0.0 | 2021-02-26T19:45:07.719038Z | Cached | 03daa056808b80f3d09e06667ec393d0d4bcceca9578491c1e2e575dc8994274 |
| 2.0.1 | 2021-03-02T16:39:04.102598Z | Cached | dd007e4fb8270916820a0d66e24f619266b60773cddd082c6439341645af2659 |
| 2.0.2 | 2023-01-02T23:29:33.194165Z | Cached | 3d1505d91afa809d177efd4eed5bb0eb65805097a1463abdd2add076effae311 |
| 2.0.3 | 2023-05-03T17:38:04.376495Z | Cached | feb6bed21949061731a7a75fc5d2aa727cf160b91af9a3e464c5e3a32e28b5ff |
| 2.0.4 | 2025-06-05T00:41:05.969146Z | Cached | 959525d3162f249993882720d52b7e0c833978df229be20702b33d48d91de70f |
Dependencies
json_annotation "^4.3.0"
source_span "^1.8.0"
yaml "^3.0.0"