JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
Line 55: | Line 55: | ||
| valign="top" | | | valign="top" | | ||
* [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://opis.io/json-schema/2.x/ Opis JSON Schema Documentation] | ||
* [https://json-schema.org/understanding-json-schema/index.html Understanding JSON Schema] | * [https://json-schema.org/understanding-json-schema/index.html Understanding JSON Schema] |
Revision as of 10:16, 5 July 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