JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
{|class='wikitable | {|class='wikitable' | ||
|valign='top' style='width:50%'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight | <syntaxhighlight lang='xml'> | ||
<dependency> | <dependency> | ||
<groupId>com.networknt</groupId> | <groupId>com.networknt</groupId> | ||
| Line 11: | Line 11: | ||
|- | |- | ||
|valign='top' style='width:50%'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight | <syntaxhighlight lang='java'> | ||
private static InputStream inputStreamFromClasspath(String path) { | private static InputStream inputStreamFromClasspath(String path) { | ||
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path); | return Thread.currentThread().getContextClassLoader().getResourceAsStream(path); | ||
| Line 40: | Line 40: | ||
==Schema Naming== | ==Schema Naming== | ||
{|class='wikitable mw-collapsible | {|class='wikitable mw-collapsible' | ||
!scope='col' style='text-align:left' colspan='2'| | !scope='col' style='text-align:left' colspan='2'| | ||
Schema Naming | Schema Naming | ||
|- | |- | ||
|valign='top' style='width:50%'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight | <syntaxhighlight lang='text'> | ||
Age: Age.schema.json | Age: Age.schema.json | ||
Address: Address.schema.json | Address: Address.schema.json | ||
| Line 56: | Line 56: | ||
==Knowledge== | ==Knowledge== | ||
{|class='wikitable mw-collapsible | {|class='wikitable mw-collapsible' | ||
!scope='col' style='text-align:left' colspan='3'| | !scope='col' style='text-align:left' colspan='3'| | ||
Knowledge | Knowledge | ||
|- | |- | ||
|valign='top' style='width:50%'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight | <syntaxhighlight lang='text'> | ||
https://cdnjs.com/libraries/ajv | https://cdnjs.com/libraries/ajv | ||
https://cdnjs.com/libraries/moment.js | https://cdnjs.com/libraries/moment.js | ||
| Line 69: | Line 69: | ||
|valign='top' style='width:50%'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight | <syntaxhighlight lang='bash'> | ||
closure-compiler\ | closure-compiler\ | ||
--js ./handlebars.js\ | --js ./handlebars.js\ | ||
| Line 77: | Line 77: | ||
|- | |- | ||
|valign='top'| | |valign='top'| | ||
<syntaxhighlight | <syntaxhighlight lang='bash'> | ||
closure-compiler\ | closure-compiler\ | ||
--js ./mustache.js\ | --js ./mustache.js\ | ||
| Line 85: | Line 85: | ||
|valign='top'| | |valign='top'| | ||
<syntaxhighlight | <syntaxhighlight lang='bash'> | ||
closure-compiler\ | closure-compiler\ | ||
--js ./moment.js\ | --js ./moment.js\ | ||
| Line 93: | Line 93: | ||
|- | |- | ||
|valign='top'| | |valign='top'| | ||
<syntaxhighlight | <syntaxhighlight lang='bash'> | ||
closure-compiler\ | closure-compiler\ | ||
--js ./ajv7.js\ | --js ./ajv7.js\ | ||
| Line 102: | Line 102: | ||
|- | |- | ||
|valign='top'| | |valign='top'| | ||
<syntaxhighlight | <syntaxhighlight lang='java'> | ||
TypeReference<SomeStype> ref = new TypeReference<SomeType>(){}; | TypeReference<SomeStype> ref = new TypeReference<SomeType>(){}; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 110: | Line 110: | ||
==Feasibility== | ==Feasibility== | ||
{|class='wikitable mw-collapsible | {|class='wikitable mw-collapsible' | ||
!scope='col' style='text-align:left' colspan='2'| | !scope='col' style='text-align:left' colspan='2'| | ||
Feasibility | Feasibility | ||
| Line 127: | Line 127: | ||
==References== | ==References== | ||
{|class='wikitable mw-collapsible | {|class='wikitable mw-collapsible' | ||
!scope='col' style='text-align:left' colspan='3'| | !scope='col' style='text-align:left' colspan='3'| | ||
References | References | ||
Latest revision as of 16:35, 18 January 2026
<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
|
Schema Naming | |
|---|---|
Age: Age.schema.json
Address: Address.schema.json
Account: Account.schema.json
Quantity: Quantity.schema.json
|
|
Knowledge
|
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
|
Feasibility | |
|---|---|
# Java
https://vertx.io/docs/vertx-json-schema/java/
# JavaScript
https://ajv.js.org/
|
|