JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
Line 50: | Line 50: | ||
* [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:// | * [https://ajv.js.org/ Ajv JSON schema validator] | ||
| valign="top" | | | valign="top" | | ||
Line 58: | Line 58: | ||
* [https://json-schema.org/understanding-json-schema/index.html Understanding JSON Schema] | * [https://json-schema.org/understanding-json-schema/index.html Understanding JSON Schema] | ||
* [https://stackoverflow.com/questions/9391370/ JSON Schema file extension] | * [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] | * [https://www.hl7.org/fhir/patient.schema.json.html JSON Schema for Patient] | ||
* [https://www.hl7.org/fhir/documentation.html FHIR Documentation] | * [https://www.hl7.org/fhir/documentation.html FHIR Documentation] |
Revision as of 01:50, 13 June 2022
<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