-
Notifications
You must be signed in to change notification settings - Fork 331
apply default in objects and arrays #477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c6f8e54
apply default in objects and arrays
SiemelNaran 4359487
introduce a class ApplyDefaultsStrategy
SiemelNaran c99bb1e
ensure that validate function does not apply defaults
SiemelNaran 73d14a8
add documentation
SiemelNaran be81817
support default value in referenced property
SiemelNaran b6e0c41
fix NullPointerException when no applyDefaultStragey is supplied
SiemelNaran 69ea27c
fix the 4 test errors mentioned: call CollectorContext.getInstance().…
SiemelNaran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/networknt/schema/ApplyDefaultsStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.networknt.schema; | ||
|
||
public class ApplyDefaultsStrategy { | ||
static final ApplyDefaultsStrategy EMPTY_APPLY_DEFAULTS_STRATEGY = new ApplyDefaultsStrategy(false, false, false); | ||
|
||
private final boolean applyPropertyDefaults; | ||
private final boolean applyPropertyDefaultsIfNull; | ||
private final boolean applyArrayDefaults; | ||
|
||
/** | ||
* Specify which default values to apply. | ||
* We can apply property defaults only if they are missing or if they are declared to be null in the input json, | ||
* and we can apply array defaults if they are declared to be null in the input json. | ||
* | ||
* <p>Note that the walker changes the input object in place. | ||
* If validation fails, the input object will be changed. | ||
* | ||
* @param applyPropertyDefaults if true then apply defaults inside json objects if the attribute is missing | ||
* @param applyPropertyDefaultsIfNull if true then apply defaults inside json objects if the attribute is explicitly null | ||
* @param applyArrayDefaults if true then apply defaults inside json arrays if the attribute is explicitly null | ||
* @throws IllegalArgumentException if applyPropertyDefaults is false and applyPropertyDefaultsIfNull is true | ||
*/ | ||
public ApplyDefaultsStrategy(boolean applyPropertyDefaults, boolean applyPropertyDefaultsIfNull, boolean applyArrayDefaults) { | ||
if (!applyPropertyDefaults && applyPropertyDefaultsIfNull) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.applyPropertyDefaults = applyPropertyDefaults; | ||
this.applyPropertyDefaultsIfNull = applyPropertyDefaultsIfNull; | ||
this.applyArrayDefaults = applyArrayDefaults; | ||
} | ||
|
||
public boolean shouldApplyPropertyDefaults() { | ||
return applyPropertyDefaults; | ||
} | ||
|
||
public boolean shouldApplyPropertyDefaultsIfNull() { | ||
return applyPropertyDefaultsIfNull; | ||
} | ||
|
||
public boolean shouldApplyArrayDefaults() { | ||
return applyArrayDefaults; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.