json-Skema is a Json Schema validator library for the Java Virtual Machine. It implements the draft 2020-12 specification.
Are you new to JSON Schema? Get started with Understanding JSON Schema!
Add the following dependency to the <dependencies>
section of your project:
<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>json-sKema</artifactId>
<version>0.7.0</version>
</dependency>
dependencies {
implementation("com.github.erosb:json-sKema:0.7.0")
}
// parse the schema JSON as string
JsonValue schemaJson = new JsonParser("""
{
"type": "object",
"properties": {
"age": {
"type": "number",
"minimum": 0
},
"name": {
"type": "string"
}
}
}
""").parse();
// map the raw json to a reusable Schema instance
Schema schema = new SchemaLoader(schemaJson).load();
// create a validator instance for each validation (one-time use object)
Validator validator = Validator.forSchema(schema);
// parse the input instance to validate against the schema
JsonValue instance = new JsonParser("""
{
"age": -5,
"name": null
}
""").parse();
// run the validation
ValidationFailure failure = validator.validate(instance);
// print the validation failures (if any)
System.out.println(failure);
// HTTP protocol is also supported
Schema schema = SchemaLoader.forURL("classpath:///path/to/your/schema.json").load();
// create a validator instance for each validation (one-time use object)
Validator validator = Validator.forSchema(schema);
// ...
The library implements the JSON Schema draft 2020-12 core and validation specifications, with the following notes:
$dynamicAnchor
and$dynamicRef
support is partially implemented
The library currently has built-in support for the following "format"
values defined in the specification:
"format" | Supported? | Notes |
date | Yes | |
date-time | Yes | Non-UTC values with leap seconds not supported |
time | Yes | Leap seconds not supported |
duration | Partially | |
Yes | IPV6 domain parts not supported | |
uri | Yes | |
ipv4 | Yes | |
ipv6 | Yes | |
uuid | Yes |
The following formats are NOT supported: hostname, idn-email, idn-hostname, iri, iri-reference, json-pointer, regex, relative-json-pointer, uri-reference, uri-template .
This project is the successor of everit-org/json-schema. If you want to use draft-04, draft-06 or draft-07 versions of JSON Schema, then you can use the everit library.
Local environment setup:
Prerequisite: JDK and Maven installed
git clone https://github.com/erosb/json-sKema.git
cd json-sKema
git submodule init
git submodule update
mvn clean package
Test annotated with @Tag("acceptance")
require the test suite to be pulled using:
git submodule update --init --recursive
Then run the tests:
mvn clean verify
mvn clean package -Dgroups='!acceptance'