JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (33 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{|class='wikitable' | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight lang='xml'> | |||
<dependency> | |||
<groupId>com.networknt</groupId> | |||
<artifactId>json-schema-validator</artifactId> | |||
<version>1.0.42</version> | |||
</dependency> | |||
</syntaxhighlight> | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight lang='java'> | |||
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())); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
|} | |||
==Schema Naming== | |||
{|class='wikitable mw-collapsible' | |||
!scope='col' style='text-align:left' colspan='2'| | |||
Schema Naming | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight lang='text'> | |||
Age: Age.schema.json | |||
Address: Address.schema.json | |||
Account: Account.schema.json | |||
Quantity: Quantity.schema.json | |||
</syntaxhighlight> | |||
|valign='top' style='width:50%'| | |||
|} | |||
==Knowledge== | |||
{|class='wikitable mw-collapsible' | |||
!scope='col' style='text-align:left' colspan='3'| | |||
Knowledge | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight lang='text'> | |||
https://cdnjs.com/libraries/ajv | |||
https://cdnjs.com/libraries/moment.js | |||
https://cdnjs.com/libraries/mustache.js | |||
https://cdnjs.com/libraries/handlebars.js | |||
</syntaxhighlight> | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight lang='bash'> | |||
closure-compiler\ | |||
--js ./handlebars.js\ | |||
--js_output_file ./handlebars.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
</syntaxhighlight> | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight lang='bash'> | |||
closure-compiler\ | |||
--js ./mustache.js\ | |||
--js_output_file ./mustache.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
</syntaxhighlight> | |||
|valign='top'| | |||
<syntaxhighlight lang='bash'> | |||
closure-compiler\ | |||
--js ./moment.js\ | |||
--js_output_file ./moment.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
</syntaxhighlight> | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight lang='bash'> | |||
closure-compiler\ | |||
--js ./ajv7.js\ | |||
--js_output_file ./ajv7.min.js\ | |||
--language_out ECMASCRIPT_2015 | |||
</syntaxhighlight> | |||
|valign='top'| | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight lang='java'> | |||
TypeReference<SomeStype> ref = new TypeReference<SomeType>(){}; | |||
</syntaxhighlight> | |||
|valign='top'| | |||
|} | |||
==Feasibility== | |||
{|class='wikitable mw-collapsible' | |||
!scope='col' style='text-align:left' colspan='2'| | |||
Feasibility | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
# Java | |||
https://vertx.io/docs/vertx-json-schema/java/ | |||
# JavaScript | |||
https://ajv.js.org/ | |||
</syntaxhighlight> | |||
|valign='top' style='width:50%'| | |||
|} | |||
==References== | ==References== | ||
{| | {|class='wikitable mw-collapsible' | ||
| valign= | !scope='col' style='text-align:left' colspan='3'| | ||
* [https://stackoverflow.com/questions/62117492/ NetworkNT JSON Schema | References | ||
|- | |||
|valign='top' style='width:33%'| | |||
* [https://stackoverflow.com/questions/27952472/ Jackson Serialize/Deserialize for Java 8 <code>java.time</code>] | |||
* [https://stackoverflow.com/questions/62117492/ NetworkNT JSON Schema With Unknown keywords] | |||
* [https://github.com/networknt/json-schema-validator NetworkNT fastest Java JSON Schema Validator] | * [https://github.com/networknt/json-schema-validator NetworkNT fastest Java JSON Schema Validator] | ||
* [https://www.baeldung.com/introduction-to-json-schema-in-java Introduction to JSON Schema in Java] | * [https://www.baeldung.com/introduction-to-json-schema-in-java Introduction to JSON Schema in Java] | ||
| Line 8: | Line 139: | ||
* [https://www.mscharhag.com/java/json-schema-validation JSON Schema validation in Java] | * [https://www.mscharhag.com/java/json-schema-validation JSON Schema validation in Java] | ||
* [https://json-schema.org/implementations.html JSON Schema Implementations] | * [https://json-schema.org/implementations.html JSON Schema Implementations] | ||
* [https://json-schema.org/understanding-json-schema/structuring.html Structuring a Complex Schema] | |||
* [https://github.com/everit-org/json-schema EverIt JSON Schema Validator] | * [https://github.com/everit-org/json-schema EverIt JSON Schema Validator] | ||
* [https://ajv.js.org/ Ajv JSON schema validator] | |||
|valign='top' style='width:34%'| | |||
* [https://www.googlecloudcommunity.com/gc/Apigee/Rhino-Cannot-modify-a-property-of-a-sealed-object/m-p/33852/highlight/true Rhino » Modify a Property of a Sealed Object] | |||
* [https://opis.io/json-schema/2.x/ Opis JSON Schema Documentation] | |||
* [https://json-schema.org/understanding-json-schema/index.html Understanding JSON Schema] | |||
* [https://stackoverflow.com/questions/9391370/ JSON Schema file extension] | |||
* [https://github.com/FasterXML/jackson-modules-java8 <code>jackson-modules-java8</code>] | |||
* [https://www.hl7.org/fhir/patient.schema.json.html JSON Schema for Patient] | |||
* [[Google Closure Compiler]] | |||
* [https://www.hl7.org/fhir/documentation.html FHIR Documentation] | |||
* [https://www.hl7.org/fhir/index.html FHIR Specification] | |||
* [[J2V8]] | |||
| valign= | |valign='top' style='width:33%'| | ||
* [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]] | |||
* [[Yarn]] | |||
* [[NPM]] | |||
|} | |} | ||
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/
|
|