Stack Overflow |
Google Group |
Gitter Chat |
Subreddit |
Youtube |
Documentation |
Contribution Guide |
Jackson Upgrade Checks |
This is a Java implementation of the JSON Schema Core Draft v4, v6, v7, v2019-09 and v2020-12 specification for JSON schema validation. This implementation supports Customizing Dialects, Vocabularies, Keywords and Formats.
The JSON parser used is the Jackson parser.
OpenAPI 3 request/response validation is supported with the use of the appropriate dialect.
As it is a key component in our light-4j microservices framework to validate request/response against OpenAPI specification for light-rest-4j and RPC schema for light-hybrid-4j at runtime, performance is the most important aspect in the design.
JSON Schema Specification compatibility
Information on the compatibility support for each version, including known issues, can be found in the Compatibility with JSON Schema versions document.
Since Draft 2019-09 the format keyword only generates annotations by default and does not generate assertions.
This behavior can be overridden to generate assertions by setting the formatAssertionsEnabled to true in SchemaRegistryConfig or ExecutionConfig.
Upgrading to new versions
This library can contain breaking changes in minor version releases that may require code changes.
Information on notable or breaking changes when upgrading the library can be found in the Upgrading to new versions document.
The Releases page will contain information on the latest versions.
Comparing against other implementations
The JSON Schema Validation Comparison project from Creek has an informative Comparison of JVM based Schema Validation Implementations which compares both the functional and performance characteristics of a number of different Java implementations.
The Bowtie project has a report that compares functional characteristics of different implementations, including non-Java implementations, but does not do any performance benchmarking.
Why this library
Performance
The following is the benchmark results from the JSON Schema Validator Perftest project that uses the Java Microbenchmark Harness.
Note that the benchmark results are highly dependent on the input data workloads and schemas used for the validation.
In this case this workload is using the Draft 4 specification and largely tests the performance of the evaluating the properties keyword. You may refer to Results of performance comparison of JVM based JSON Schema Validation Implementations for benchmark results that use the JSON Schema Test Suite.
If performance is an important consideration, the specific sample workloads should be benchmarked, as there are different performance characteristics when certain keywords are used. For instance the use of the unevaluatedProperties or unevaluatedItems keyword will trigger annotation collection in the related validators, such as the properties or items validators, and annotation collection will adversely affect performance.
Special attention should also be made for inefficient schemas using deeply nested oneOf or anyOf that do not have a condition to short-circuit the evaluation using if and then. The validator has no choice but to perform all the evaluations, and the error messages would be typically very confusing as it will return all the messages from the children.
NetworkNT 2.0.0
Benchmark Mode Cnt Score Error Units
NetworkntBenchmark.basic thrpt 10 5297.105 ± 290.078 ops/s
NetworkntBenchmark.basic:gc.alloc.rate thrpt 10 1618.328 ± 88.626 MB/sec
NetworkntBenchmark.basic:gc.alloc.rate.norm thrpt 10 320360.020 ± 0.002 B/op
NetworkntBenchmark.basic:gc.count thrpt 10 365.000 counts
NetworkntBenchmark.basic:gc.time thrpt 10 130.000 ms
Everit 1.14.6
Benchmark Mode Cnt Score Error Units
EveritBenchmark.basic thrpt 10 4615.637 ± 151.195 ops/s
EveritBenchmark.basic:gc.alloc.rate thrpt 10 2097.810 ± 68.708 MB/sec
EveritBenchmark.basic:gc.alloc.rate.norm thrpt 10 476592.023 ± 0.001 B/op
EveritBenchmark.basic:gc.count thrpt 10 521.000 counts
EveritBenchmark.basic:gc.time thrpt 10 170.000 ms
Functionality
This implementation is tested against the JSON Schema Test Suite. As tests are continually added to the suite, these test results may not be current.
| Implementations |
Overall |
DRAFT_03 |
DRAFT_04 |
DRAFT_06 |
DRAFT_07 |
DRAFT_2019_09 |
DRAFT_2020_12 |
| NetworkNt |
pass: r:4840 (100.0%) o:2421 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
|
pass: r:610 (100.0%) o:255 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
pass: r:829 (100.0%) o:322 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
pass: r:913 (100.0%) o:554 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
pass: r:1227 (100.0%) o:639 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
pass: r:1261 (100.0%) o:651 (100.0%) fail: r:0 (0.0%) o:0 (0.0%) |
- Note that this uses the
JoniRegularExpressionFactory for the pattern and format regex tests.
Jackson Parser
This library uses Jackson which is a Java JSON parser that is widely used in other projects. If you are already using the Jackson parser in your project, it is natural to choose this library over others for schema validation.
YAML Support
The library works with JSON and YAML on both schema definitions and input data.
OpenAPI Support
The OpenAPI 3.0 specification is using JSON schema to validate the request/response. The library has support for the OpenAPI 3.0 and OpenAPI 3.1 dialects.
Minimal Dependencies
Following the design principle of the Light Platform, this library has minimal dependencies to ensure there are no dependency conflicts when using it.
Required Dependencies
The following are the dependencies that will automatically be included when this library is included.
<dependency>
<!-- Used for logging -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${version.slf4j}</version>
</dependency>
<dependency>
<!-- Used to process JSON -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<!-- Used to process YAML -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<!-- Used to validate RFC 3339 date and date-time -->
<groupId>com.ethlo.time</groupId>
<artifactId>itu</artifactId>
<version>${version.itu}</version>
</dependency>
Optional Dependencies
The following are the optional dependencies that may be required for certain options.
These are not automatically included and setting the relevant option without adding the library will result in a ClassNotFoundException.
<dependency>
<!-- Used to validate ECMA 262 regular expressions -->
<!-- Approximately 50 MB in dependencies -->
<!-- GraalJSRegularExpressionFactory -->
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>${version.graaljs}</version>
</dependency>
<dependency>
<!-- Used to validate ECMA 262 regular expressions -->
<!-- Approximately 2 MB in dependencies -->
<!-- JoniRegularExpressionFactory -->
<groupId>org.jruby.joni</groupId>
<artifactId>joni</artifactId>
<version>${version.joni}</version>
</dependency>
Excludable Dependencies
The following are required dependencies that are automatically included, but can be explicitly excluded if they are not required.
The YAML dependency can be excluded if this is not required. Attempting to process schemas or input that are YAML will result in a ClassNotFoundException.
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</exclusion>
</exclusions>
</dependency>
The date-time format validates the RFC 3339 full-date "T" full-time grammar, also accepting lowercase t and z. Seconds and a complete offset are required; a space separator, an empty fraction and trailing text are rejected. Both dependency paths accept -00:00, numeric offsets up to ±23:59 and fractions longer than nine digits.
The Ethlo Time dependency can be excluded if less accurate leap-second validation is acceptable. With Ethlo present, a leap second must pass Ethlo's historical table check for its UTC date. Without Ethlo, leap seconds are accepted only at the end of June 30 or December 31 in UTC, without checking whether one was actually inserted on that date. The written offset can move a leap second onto a different local date; validation checks the corresponding UTC date. These checks validate the string without changing the instance value.
Space-separated values and offset-less leap-second strings such as 2016-12-31T23:59:60 were previously accepted with Ethlo and are now rejected. These are breaking validation changes. See the date-time compatibility notes for migration guidance and the historical leap-table differences that remain.
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<exclusions>
<exclusion>
<groupId>com.ethlo.time</groupId>
<artifactId>itu</artifactId>
</exclusion>
</exclusions>
</dependency>
Community
This library is very active with a lot of contributors. New features and bug fixes are handled quickly by the team members. Because it is an essential dependency of the light-4j framework in the same GitHub organization, it will be evolved and maintained along with the framework.
Prerequisite