-
Notifications
You must be signed in to change notification settings - Fork 418
Add disallowUnrecognizedKeys
option
#212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
06bf1f6
5d5acfb
15a13fd
2756a2a
f321e74
bdd52e4
6e721ff
e800077
389fda7
8cbf830
e521eae
3eee0e0
4aabaef
7c8c1be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2018, 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. | ||
|
||
/// Helper function used in generated code when | ||
/// `JsonSerializable.disallowUnrecognizedKeys` is `true`. | ||
/// | ||
/// Should not be used directly. | ||
void $checkAllowedKeys(Map map, Iterable<String> allowedKeys) { | ||
if (map == null) return; | ||
var invalidKeys = map.keys.where((k) => !allowedKeys.contains(k)); | ||
if (invalidKeys.isNotEmpty) { | ||
throw new UnrecognizedKeysException( | ||
new List<String>.from(invalidKeys), map, allowedKeys.toList()); | ||
} | ||
} | ||
|
||
/// Exception thrown if there is an unrecognized key in a json map that was | ||
/// provided during deserialization. | ||
class UnrecognizedKeysException implements Exception { | ||
/// The allowed keys for [map]. | ||
final List<String> allowedKeys; | ||
|
||
/// The keys from [map] that were unrecognized. | ||
final List<String> unrecognizedKeys; | ||
|
||
/// The source [Map] that the key was found in. | ||
final Map map; | ||
|
||
/// A human-readable message corresponding to the error. | ||
String get message => | ||
'Unrecognized keys [${unrecognizedKeys.join(', ')}], supported keys are ' | ||
'[${allowedKeys.join(', ')}]'; | ||
|
||
UnrecognizedKeysException(this.unrecognizedKeys, this.map, this.allowedKeys); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
## 0.5.6 | ||
|
||
* Added support for `JsonSerializable.disallowUnrecognizedKeys`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also note that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* Throws an `UnrecognizedKeysException` if it finds unrecognized keys in the | ||
JSON map used to create the annotated object. | ||
* Will be captured captured and wrapped in a `CheckedFromJsonException` if | ||
`checked` is enabled in `json_serializable`. | ||
* All `fromJson` constructors now use block syntax instead of fat arrows. | ||
|
||
## 0.5.5 | ||
|
||
* Added support for `JsonKey.defaultValue`. | ||
|
@@ -35,9 +44,9 @@ | |
|
||
* If `JsonKey.fromJson` function parameter is `Iterable` or `Map` with type | ||
arguments of `dynamic` or `Object`, omit the arguments when generating a | ||
cast. | ||
cast. | ||
`_myHelper(json['key'] as Map)` instead of | ||
`_myHelper(json['key'] as Map<dynamic, dynamic>)`. | ||
`_myHelper(json['key'] as Map<dynamic, dynamic>)`. | ||
|
||
* `JsonKey.fromJson`/`.toJson` now support functions with optional arguments. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: json_serializable | ||
version: 0.5.5 | ||
version: 0.5.6-dev | ||
author: Dart Team <misc@dartlang.org> | ||
description: Generates utilities to aid in serializing to/from JSON. | ||
homepage: https://github.com/dart-lang/json_serializable | ||
|
@@ -12,7 +12,7 @@ dependencies: | |
|
||
# Use a tight version constraint to ensure that a constraint on | ||
# `json_annotation`. Properly constrains all features it provides. | ||
json_annotation: '>=0.2.6 <0.2.7' | ||
json_annotation: '>=0.2.7 <0.2.8' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you need a dep override to make tests run? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added back - this got removed when I merged master (I didn't have to add it originally so it wasn't a merge conflict.... source control is fun) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack |
||
meta: ^1.1.0 | ||
path: ^1.3.2 | ||
source_gen: '>=0.8.1 <0.9.0' | ||
|
@@ -25,3 +25,7 @@ dev_dependencies: | |
logging: ^0.11.3+1 | ||
test: ^0.12.3 | ||
yaml: ^2.1.13 | ||
|
||
dependency_overrides: | ||
json_annotation: | ||
path: ../json_annotation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put runtime info here from json_annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done