-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Adds lenient validation mode for primitive values. In lenient mode, strings are accepted as numbers, booleans or nulls, if they are parseable. By default, lenient mode is turned off. ## Details Adding `PrimitiveValidationStrategy` enum as a feature switch for the lenient validation mode. Also adding appropriate builder method for `Validator` for setting it up. Changing signature of `ValidatingVisitor#passesTypeCheck()` so that instead of returning a boolean, it accepts a callback, which is either called or not, depending on if the type-check passed or not. Furthermore, if the validator runs in LENIENT mode, the passesTypeCheck() attempts to perform a conversion to the expected value, if that is possible. If it succeeds, then the `onPass` callback will be invoked with the converted value as the parameter. The following ValidatingVisitor subclasses are updated to call the new `passesTypeCheck()` method correctly: * `ArraySchemaValidatingVisitor` * `StringSchemaValidatingVisitor` * `NumberSchemaValidatingVisitor` * `ObjectSchemaValidatingVisitor` The string-to-other-primitive conversion is performed by the `StringToValueConverter` class. The methods of this class are copied from `org.json.JSONObject`. Although it would be possible to call `JSONObject#stringToValue()` from ValidatingVisitor#ifPassesTypeCheck()`, we can not do it, because `JSONObject#stringToValue()` does not exist in the android flavor of the org.json package, therefore on android it would throw a `NoSuchMethodError`. For that reason, these methods are copied to the everit-org/json-schema library, to make sure that they exist at run-time. Furthermore, this implementation accepts [all 22 boolean literals of YAML](https://yaml.org/type/bool.html) as valid booleans. Credits go to @nfrankel for pointing out this extensive boolean support in a recent LinkedIn post :)
- Loading branch information
Showing
14 changed files
with
535 additions
and
142 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
5 changes: 5 additions & 0 deletions
5
core/src/main/java/org/everit/json/schema/PrimitiveValidationStrategy.java
This file contains 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,5 @@ | ||
package org.everit.json.schema; | ||
|
||
public enum PrimitiveValidationStrategy { | ||
STRICT, LENIENT | ||
} |
This file contains 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.