JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
(12 intermediate revisions by the same user not shown) | |||
Line 41: | Line 41: | ||
==Knowledge== | ==Knowledge== | ||
{| | |||
|valign='top' colspan='2'| | |||
https://cdnjs.com/libraries/ajv | https://cdnjs.com/libraries/ajv | ||
https://cdnjs.com/libraries/moment.js | |||
https://cdnjs.com/libraries/mustache.js | https://cdnjs.com/libraries/mustache.js | ||
https://cdnjs.com/libraries/handlebars.js | https://cdnjs.com/libraries/handlebars.js | ||
|valign='top'| | |||
closure-compiler\ | |||
--js ./handlebars.js\ | |||
--js_output_file ./handlebars.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
|- | |||
|colspan='3'| | |||
---- | |||
|- | |||
|valign='top'| | |||
closure-compiler\ | |||
--js ./mustache.js\ | |||
--js_output_file ./mustache.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
|valign='top'| | |||
closure-compiler\ | |||
--js ./moment.js\ | |||
--js_output_file ./moment.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
|valign='top'| | |||
closure-compiler\ | |||
--js ./ajv7.js\ | |||
--js_output_file ./ajv7.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
|- | |||
|colspan='3'| | |||
---- | |||
|- | |||
|valign='top' colspan='2'| | |||
TypeReference<SomeStype> ref = new TypeReference<SomeType>(){}; | |||
|valign='top'| | |||
|} | |||
==Feasibility== | |||
'''Java''' | |||
https://vertx.io/docs/vertx-json-schema/java/ | |||
'''JavaScript''' | |||
https://ajv.js.org/ | |||
==References== | ==References== | ||
Line 72: | Line 122: | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/54734482/ Serialize/deserialize <code>java.time.DayOfWeek</code>] | |||
* [https://stackoverflow.com/questions/47883374/ Register modules in <code>ObjecMapper</code> globally] | |||
* [https://guillaumeblanchet.medium.com/three-ways-to-validate-json-in-java-4b38d95ba7c Three Ways to Validate JSON in Java] | |||
* [https://www.linkedin.com/pulse/json-schema-validator-eran-shaham Vertx » A JSON schema validator] | |||
* [https://github.com/ersh112356/vertx-json-validator/tree/master Vertx » JSON validator] | |||
* [[GraphQL]] | |||
* [[HTTPie]] | |||
* [[Rhino]] | * [[Rhino]] | ||
* [[Yarn]] | |||
* [[NPM]] | |||
|- | |- |
Latest revision as of 20:48, 5 January 2024
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.42</version>
</dependency>
private static InputStream inputStreamFromClasspath(String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
try (
InputStream jsonStream = inputStreamFromClasspath("example.json");
InputStream schemaStream = inputStreamFromClasspath("example-schema.json")
) {
JsonNode json = objectMapper.readTree(jsonStream);
JsonSchema schema = schemaFactory.getSchema(schemaStream);
Set<ValidationMessage> validationResult = schema.validate(json);
// print validation errors
if (validationResult.isEmpty()) {
System.out.println("no validation errors :-)");
} else {
validationResult.forEach(vm -> System.out.println(vm.getMessage()));
}
}
}
Schema Naming
Age: Age.schema.json Address: Address.schema.json Account: Account.schema.json Quantity: Quantity.schema.json
Knowledge
https://cdnjs.com/libraries/ajv https://cdnjs.com/libraries/moment.js https://cdnjs.com/libraries/mustache.js https://cdnjs.com/libraries/handlebars.js |
closure-compiler\ --js ./handlebars.js\ --js_output_file ./handlebars.min.js\ --language_out ECMASCRIPT_2015 | |
| ||
closure-compiler\ --js ./mustache.js\ --js_output_file ./mustache.min.js\ --language_out ECMASCRIPT_2015 |
closure-compiler\ --js ./moment.js\ --js_output_file ./moment.min.js\ --language_out ECMASCRIPT_2015 |
closure-compiler\ --js ./ajv7.js\ --js_output_file ./ajv7.min.js\ --language_out ECMASCRIPT_2015
|
| ||
TypeReference<SomeStype> ref = new TypeReference<SomeType>(){}; |
Feasibility
Java https://vertx.io/docs/vertx-json-schema/java/ JavaScript https://ajv.js.org/