The library supports draft-4, draft-6, draft-7, draft-2019-09 and draft-2020-12 versions of Json Schema.
import via maven:
<dependencies>
<dependency>
<groupId>es.elixir.bsc.json.schema</groupId>
<artifactId>jaronuinga</artifactId>
<version>0.5.6</version>
</dependency>
...
<repositories>
<repository>
<id>jaronuinga</id>
<url>https://inb.bsc.es/maven</url>
</repository>
The simplest usage:
JsonSchema schema = JsonSchemaReader.getReader().read(url); // parse JsonSchema from the URL location
List<ValidationError> errors = new ArrayList(); // array to collect errors
schema.validate(json, errors); // validate JsonObject
Note that instead of URL users could provide their own schema locators. JsonSchemaLocator object is used for JsonSchema URI resolution and as a cache for local Schemas' definitions - to resolve "$ref" Json Pointers.
To provide flexibility it is possible to get callbacks during the validation process.
schema.validate(json, errors, (
JsonSchema subschema, String pointer, JsonValue value, JsonValue parent, List<ValidationError> err) -> {
});
Here above we have:
- subschema - current validating Json (sub)schema
- pointer - Json Pointer to the validating Json value
- value - currently validating Json value
- parent - a parent of currently validating Json value
- err - collected validation errors so far
JsonSchemaReader reader = JsonSchemaReader.getReader();
JsonSchemaLocator locator = reader.getJsonSchemaLocator(uri);
JsonSchema schema = reader.read(locator);
schema.validate(json, errors, (
JsonSchema subschema, String pointer, JsonValue value, JsonValue parent, List<ValidationError> err) -> {
JsonObject subschemaJsonObject = locator.getSchema(subschema.getId(), subschema.getJsonPointer());
});
We can also stop further parsing on error via the callback:
schema.validate(json, errors, (
JsonSchema subschema, String pointer, JsonValue value, JsonValue parent, List<ValidationError> err) -> {
throw new ValidationException(new ValidationError(subschema.getId(), subschema.getJsonPointer(), ""));
});