1*78c4dd6aSAndroid Build Coastguard Worker[Stack Overflow](https://stackoverflow.com/questions/tagged/light-4j) | 2*78c4dd6aSAndroid Build Coastguard Worker[Google Group](https://groups.google.com/forum/#!forum/light-4j) | 3*78c4dd6aSAndroid Build Coastguard Worker[Gitter Chat](https://gitter.im/networknt/json-schema-validator) | 4*78c4dd6aSAndroid Build Coastguard Worker[Subreddit](https://www.reddit.com/r/lightapi/) | 5*78c4dd6aSAndroid Build Coastguard Worker[Youtube](https://www.youtube.com/channel/UCHCRMWJVXw8iB7zKxF55Byw) | 6*78c4dd6aSAndroid Build Coastguard Worker[Documentation](https://doc.networknt.com/library/json-schema-validator/) | 7*78c4dd6aSAndroid Build Coastguard Worker[Contribution Guide](https://doc.networknt.com/contribute/) | 8*78c4dd6aSAndroid Build Coastguard Worker 9*78c4dd6aSAndroid Build Coastguard Worker[](https://github.com/networknt/json-schema-validator/actions/workflows/ci.yml) 10*78c4dd6aSAndroid Build Coastguard Worker[](http://search.maven.org/#search%7Cga%7C1%7Cg%3Acom.networknt%20a%3Ajson-schema-validator) 11*78c4dd6aSAndroid Build Coastguard Worker[](https://codecov.io/github/networknt/json-schema-validator?branch=master) 12*78c4dd6aSAndroid Build Coastguard Worker[](https://www.javadoc.io/doc/com.networknt/json-schema-validator) 13*78c4dd6aSAndroid Build Coastguard Worker 14*78c4dd6aSAndroid Build Coastguard WorkerThis is a Java implementation of the [JSON Schema Core Draft v4, v6, v7, v2019-09 and v2020-12](http://json-schema.org/latest/json-schema-core.html) specification for JSON schema validation. This implementation supports [Customizing Meta-Schemas, Vocabularies, Keywords and Formats](doc/custom-meta-schema.md). 15*78c4dd6aSAndroid Build Coastguard Worker 16*78c4dd6aSAndroid Build Coastguard WorkerIn addition, it also works for OpenAPI 3.0 request/response validation with some [configuration flags](doc/config.md). For users who want to collect information from a JSON node based on the schema, the [walkers](doc/walkers.md) can help. The JSON parser used is the [Jackson](https://github.com/FasterXML/jackson) parser. As it is a key component in our [light-4j](https://github.com/networknt/light-4j) microservices framework to validate request/response against OpenAPI specification for [light-rest-4j](http://www.networknt.com/style/light-rest-4j/) and RPC schema for [light-hybrid-4j](http://www.networknt.com/style/light-hybrid-4j/) at runtime, performance is the most important aspect in the design. 17*78c4dd6aSAndroid Build Coastguard Worker 18*78c4dd6aSAndroid Build Coastguard Worker## JSON Schema Specification compatibility 19*78c4dd6aSAndroid Build Coastguard Worker 20*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/implementations/java-json-schema-validator) 21*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/dialects/draft2020-12) 22*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/dialects/draft2019-09) 23*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/dialects/draft7) 24*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/dialects/draft6) 25*78c4dd6aSAndroid Build Coastguard Worker[](https://bowtie.report/#/dialects/draft4) 26*78c4dd6aSAndroid Build Coastguard Worker 27*78c4dd6aSAndroid Build Coastguard WorkerInformation on the compatibility support for each version, including known issues, can be found in the [Compatibility with JSON Schema versions](doc/compatibility.md) document. 28*78c4dd6aSAndroid Build Coastguard Worker 29*78c4dd6aSAndroid Build Coastguard WorkerSince [Draft 2019-09](https://json-schema.org/draft/2019-09/json-schema-validation#rfc.section.7) the `format` keyword only generates annotations by default and does not generate assertions. 30*78c4dd6aSAndroid Build Coastguard Worker 31*78c4dd6aSAndroid Build Coastguard WorkerThis behavior can be overridden to generate assertions by setting the `setFormatAssertionsEnabled` to `true` in `SchemaValidatorsConfig` or `ExecutionConfig`. 32*78c4dd6aSAndroid Build Coastguard Worker 33*78c4dd6aSAndroid Build Coastguard Worker## Upgrading to new versions 34*78c4dd6aSAndroid Build Coastguard Worker 35*78c4dd6aSAndroid Build Coastguard WorkerThis library can contain breaking changes in `minor` version releases that may require code changes. 36*78c4dd6aSAndroid Build Coastguard Worker 37*78c4dd6aSAndroid Build Coastguard WorkerInformation on notable or breaking changes when upgrading the library can be found in the [Upgrading to new versions](doc/upgrading.md) document. 38*78c4dd6aSAndroid Build Coastguard Worker 39*78c4dd6aSAndroid Build Coastguard WorkerThe [Releases](https://github.com/networknt/json-schema-validator/releases) page will contain information on the latest versions. 40*78c4dd6aSAndroid Build Coastguard Worker 41*78c4dd6aSAndroid Build Coastguard Worker## Comparing against other implementations 42*78c4dd6aSAndroid Build Coastguard Worker 43*78c4dd6aSAndroid Build Coastguard WorkerThe [JSON Schema Validation Comparison](https://github.com/creek-service/json-schema-validation-comparison) project from Creek has an informative [Comparison of JVM based Schema Validation Implementations](https://www.creekservice.org/json-schema-validation-comparison/) which compares both the functional and performance characteristics of a number of different Java implementations. 44*78c4dd6aSAndroid Build Coastguard Worker* [Functional comparison](https://www.creekservice.org/json-schema-validation-comparison/functional#summary-results-table) 45*78c4dd6aSAndroid Build Coastguard Worker* [Performance comparison](https://www.creekservice.org/json-schema-validation-comparison/performance#json-schema-test-suite-benchmark) 46*78c4dd6aSAndroid Build Coastguard Worker 47*78c4dd6aSAndroid Build Coastguard WorkerThe [Bowtie](https://github.com/bowtie-json-schema/bowtie) project has a [report](https://bowtie.report/) that compares functional characteristics of different implementations, including non-Java implementations, but does not do any performance benchmarking. 48*78c4dd6aSAndroid Build Coastguard Worker 49*78c4dd6aSAndroid Build Coastguard Worker## Why this library 50*78c4dd6aSAndroid Build Coastguard Worker 51*78c4dd6aSAndroid Build Coastguard Worker#### Performance 52*78c4dd6aSAndroid Build Coastguard Worker 53*78c4dd6aSAndroid Build Coastguard WorkerThis should be the fastest Java JSON Schema Validator implementation. 54*78c4dd6aSAndroid Build Coastguard Worker 55*78c4dd6aSAndroid Build Coastguard WorkerThe following is the benchmark results from the [JSON Schema Validator Perftest](https://github.com/networknt/json-schema-validator-perftest) project that uses the [Java Microbenchmark Harness](https://github.com/openjdk/jmh). 56*78c4dd6aSAndroid Build Coastguard Worker 57*78c4dd6aSAndroid Build Coastguard WorkerNote that the benchmark results are highly dependent on the input data workloads and schemas used for the validation. 58*78c4dd6aSAndroid Build Coastguard Worker 59*78c4dd6aSAndroid Build Coastguard WorkerIn 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](https://www.creekservice.org/json-schema-validation-comparison/performance#json-schema-test-suite-benchmark) for benchmark results for more typical workloads 60*78c4dd6aSAndroid Build Coastguard Worker 61*78c4dd6aSAndroid Build Coastguard WorkerIf 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. 62*78c4dd6aSAndroid Build Coastguard Worker 63*78c4dd6aSAndroid Build Coastguard Worker##### NetworkNT 1.3.1 64*78c4dd6aSAndroid Build Coastguard Worker 65*78c4dd6aSAndroid Build Coastguard Worker``` 66*78c4dd6aSAndroid Build Coastguard WorkerBenchmark Mode Cnt Score Error Units 67*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate thrpt 10 6776.693 ± 115.309 ops/s 68*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.alloc.rate thrpt 10 971.191 ± 16.420 MB/sec 69*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.alloc.rate.norm thrpt 10 165318.816 ± 0.459 B/op 70*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.churn.G1_Eden_Space thrpt 10 968.894 ± 51.234 MB/sec 71*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.churn.G1_Eden_Space.norm thrpt 10 164933.962 ± 8636.203 B/op 72*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.churn.G1_Survivor_Space thrpt 10 0.002 ± 0.001 MB/sec 73*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.churn.G1_Survivor_Space.norm thrpt 10 0.274 ± 0.218 B/op 74*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.count thrpt 10 89.000 counts 75*78c4dd6aSAndroid Build Coastguard WorkerNetworkntBenchmark.testValidate:·gc.time thrpt 10 99.000 ms 76*78c4dd6aSAndroid Build Coastguard Worker``` 77*78c4dd6aSAndroid Build Coastguard Worker 78*78c4dd6aSAndroid Build Coastguard Worker###### Everit 1.14.1 79*78c4dd6aSAndroid Build Coastguard Worker 80*78c4dd6aSAndroid Build Coastguard Worker``` 81*78c4dd6aSAndroid Build Coastguard WorkerBenchmark Mode Cnt Score Error Units 82*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate thrpt 10 3719.192 ± 125.592 ops/s 83*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.alloc.rate thrpt 10 1448.208 ± 74.746 MB/sec 84*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.alloc.rate.norm thrpt 10 449621.927 ± 7400.825 B/op 85*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.churn.G1_Eden_Space thrpt 10 1446.397 ± 79.919 MB/sec 86*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.churn.G1_Eden_Space.norm thrpt 10 449159.799 ± 18614.931 B/op 87*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.churn.G1_Survivor_Space thrpt 10 0.001 ± 0.001 MB/sec 88*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.churn.G1_Survivor_Space.norm thrpt 10 0.364 ± 0.391 B/op 89*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.count thrpt 10 133.000 counts 90*78c4dd6aSAndroid Build Coastguard WorkerEveritBenchmark.testValidate:·gc.time thrpt 10 148.000 ms 91*78c4dd6aSAndroid Build Coastguard Worker``` 92*78c4dd6aSAndroid Build Coastguard Worker 93*78c4dd6aSAndroid Build Coastguard Worker#### Functionality 94*78c4dd6aSAndroid Build Coastguard Worker 95*78c4dd6aSAndroid Build Coastguard WorkerThis implementation is tested against the [JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite). As tests are continually added to the suite, these test results may not be current. 96*78c4dd6aSAndroid Build Coastguard Worker 97*78c4dd6aSAndroid Build Coastguard Worker| Implementations | Overall | DRAFT_03 | DRAFT_04 | DRAFT_06 | DRAFT_07 | DRAFT_2019_09 | DRAFT_2020_12 | 98*78c4dd6aSAndroid Build Coastguard Worker|-----------------|-------------------------------------------------------------------------|-------------------------------------------------------------------|---------------------------------------------------------------------|--------------------------------------------------------------------|------------------------------------------------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------| 99*78c4dd6aSAndroid Build Coastguard Worker| NetworkNt | pass: r:4703 (100.0%) o:2369 (100.0%)<br>fail: r:0 (0.0%) o:1 (0.0%) | | pass: r:600 (100.0%) o:251 (100.0%)<br>fail: r:0 (0.0%) o:0 (0.0%) | pass: r:796 (100.0%) o:318 (100.0%)<br>fail: r:0 (0.0%) o:0 (0.0%) | pass: r:880 (100.0%) o:541 (100.0%)<br>fail: r:0 (0.0%) o:0 (0.0%) | pass: r:1201 (100.0%) o:625 (100.0%)<br>fail: r:0 (0.0%) o:0 (0.0%) | pass: r:1226 (100.0%) o:634 (99.8%)<br>fail: r:0 (0.0%) o:1 (0.2%) | 100*78c4dd6aSAndroid Build Coastguard Worker 101*78c4dd6aSAndroid Build Coastguard Worker* Note that this uses the ECMA 262 Validator option turned on for the `pattern` tests. 102*78c4dd6aSAndroid Build Coastguard Worker 103*78c4dd6aSAndroid Build Coastguard Worker#### Jackson Parser 104*78c4dd6aSAndroid Build Coastguard Worker 105*78c4dd6aSAndroid Build Coastguard WorkerThis library uses [Jackson](https://github.com/FasterXML/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. 106*78c4dd6aSAndroid Build Coastguard Worker 107*78c4dd6aSAndroid Build Coastguard Worker#### YAML Support 108*78c4dd6aSAndroid Build Coastguard Worker 109*78c4dd6aSAndroid Build Coastguard WorkerThe library works with JSON and YAML on both schema definitions and input data. 110*78c4dd6aSAndroid Build Coastguard Worker 111*78c4dd6aSAndroid Build Coastguard Worker#### OpenAPI Support 112*78c4dd6aSAndroid Build Coastguard Worker 113*78c4dd6aSAndroid Build Coastguard WorkerThe OpenAPI 3.0 specification is using JSON schema to validate the request/response, but there are some differences. With a configuration file, you can enable the library to work with OpenAPI 3.0 validation. 114*78c4dd6aSAndroid Build Coastguard Worker 115*78c4dd6aSAndroid Build Coastguard Worker#### Minimal Dependencies 116*78c4dd6aSAndroid Build Coastguard Worker 117*78c4dd6aSAndroid Build Coastguard WorkerFollowing the design principle of the Light Platform, this library has minimal dependencies to ensure there are no dependency conflicts when using it. 118*78c4dd6aSAndroid Build Coastguard Worker 119*78c4dd6aSAndroid Build Coastguard Worker##### Required Dependencies 120*78c4dd6aSAndroid Build Coastguard Worker 121*78c4dd6aSAndroid Build Coastguard WorkerThe following are the dependencies that will automatically be included when this library is included. 122*78c4dd6aSAndroid Build Coastguard Worker 123*78c4dd6aSAndroid Build Coastguard Worker```xml 124*78c4dd6aSAndroid Build Coastguard Worker<dependency> 125*78c4dd6aSAndroid Build Coastguard Worker <!-- Used for logging --> 126*78c4dd6aSAndroid Build Coastguard Worker <groupId>org.slf4j</groupId> 127*78c4dd6aSAndroid Build Coastguard Worker <artifactId>slf4j-api</artifactId> 128*78c4dd6aSAndroid Build Coastguard Worker <version>${version.slf4j}</version> 129*78c4dd6aSAndroid Build Coastguard Worker</dependency> 130*78c4dd6aSAndroid Build Coastguard Worker 131*78c4dd6aSAndroid Build Coastguard Worker<dependency> 132*78c4dd6aSAndroid Build Coastguard Worker <!-- Used to process JSON --> 133*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.core</groupId> 134*78c4dd6aSAndroid Build Coastguard Worker <artifactId>jackson-databind</artifactId> 135*78c4dd6aSAndroid Build Coastguard Worker <version>${version.jackson}</version> 136*78c4dd6aSAndroid Build Coastguard Worker</dependency> 137*78c4dd6aSAndroid Build Coastguard Worker 138*78c4dd6aSAndroid Build Coastguard Worker<dependency> 139*78c4dd6aSAndroid Build Coastguard Worker <!-- Used to process YAML --> 140*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.dataformat</groupId> 141*78c4dd6aSAndroid Build Coastguard Worker <artifactId>jackson-dataformat-yaml</artifactId> 142*78c4dd6aSAndroid Build Coastguard Worker <version>${version.jackson}</version> 143*78c4dd6aSAndroid Build Coastguard Worker</dependency> 144*78c4dd6aSAndroid Build Coastguard Worker 145*78c4dd6aSAndroid Build Coastguard Worker<dependency> 146*78c4dd6aSAndroid Build Coastguard Worker <!-- Used to validate RFC 3339 date and date-time --> 147*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.ethlo.time</groupId> 148*78c4dd6aSAndroid Build Coastguard Worker <artifactId>itu</artifactId> 149*78c4dd6aSAndroid Build Coastguard Worker <version>${version.itu}</version> 150*78c4dd6aSAndroid Build Coastguard Worker</dependency> 151*78c4dd6aSAndroid Build Coastguard Worker``` 152*78c4dd6aSAndroid Build Coastguard Worker 153*78c4dd6aSAndroid Build Coastguard Worker##### Optional Dependencies 154*78c4dd6aSAndroid Build Coastguard Worker 155*78c4dd6aSAndroid Build Coastguard WorkerThe following are the optional dependencies that may be required for certain options. 156*78c4dd6aSAndroid Build Coastguard Worker 157*78c4dd6aSAndroid Build Coastguard WorkerThese are not automatically included and setting the relevant option without adding the library will result in a `ClassNotFoundException`. 158*78c4dd6aSAndroid Build Coastguard Worker 159*78c4dd6aSAndroid Build Coastguard Worker```xml 160*78c4dd6aSAndroid Build Coastguard Worker<!-- This is required when setting setEcma262Validator(true) --> 161*78c4dd6aSAndroid Build Coastguard Worker<dependency> 162*78c4dd6aSAndroid Build Coastguard Worker <!-- Used to validate ECMA 262 regular expressions --> 163*78c4dd6aSAndroid Build Coastguard Worker <groupId>org.jruby.joni</groupId> 164*78c4dd6aSAndroid Build Coastguard Worker <artifactId>joni</artifactId> 165*78c4dd6aSAndroid Build Coastguard Worker <version>${version.joni}</version> 166*78c4dd6aSAndroid Build Coastguard Worker <optional>true</optional> 167*78c4dd6aSAndroid Build Coastguard Worker</dependency> 168*78c4dd6aSAndroid Build Coastguard Worker``` 169*78c4dd6aSAndroid Build Coastguard Worker 170*78c4dd6aSAndroid Build Coastguard Worker##### Excludable Dependencies 171*78c4dd6aSAndroid Build Coastguard Worker 172*78c4dd6aSAndroid Build Coastguard WorkerThe following are required dependencies that are automatically included, but can be explicitly excluded if they are not required. 173*78c4dd6aSAndroid Build Coastguard Worker 174*78c4dd6aSAndroid Build Coastguard WorkerThe YAML dependency can be excluded if this is not required. Attempting to process schemas or input that are YAML will result in a `ClassNotFoundException`. 175*78c4dd6aSAndroid Build Coastguard Worker 176*78c4dd6aSAndroid Build Coastguard Worker```xml 177*78c4dd6aSAndroid Build Coastguard Worker<dependency> 178*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.networknt</groupId> 179*78c4dd6aSAndroid Build Coastguard Worker <artifactId>json-schema-validator</artifactId> 180*78c4dd6aSAndroid Build Coastguard Worker <exclusions> 181*78c4dd6aSAndroid Build Coastguard Worker <exclusion> 182*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.dataformat</groupId> 183*78c4dd6aSAndroid Build Coastguard Worker <artifactId>jackson-dataformat-yaml</artifactId> 184*78c4dd6aSAndroid Build Coastguard Worker </exclusion> 185*78c4dd6aSAndroid Build Coastguard Worker </exclusions> 186*78c4dd6aSAndroid Build Coastguard Worker</dependency> 187*78c4dd6aSAndroid Build Coastguard Worker``` 188*78c4dd6aSAndroid Build Coastguard Worker 189*78c4dd6aSAndroid Build Coastguard WorkerThe Ethlo Time dependency can be excluded if accurate validation of the `date-time` format is not required. The `date-time` format will then use `java.time.OffsetDateTime` to determine if the `date-time` is valid . 190*78c4dd6aSAndroid Build Coastguard Worker 191*78c4dd6aSAndroid Build Coastguard Worker```xml 192*78c4dd6aSAndroid Build Coastguard Worker<dependency> 193*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.networknt</groupId> 194*78c4dd6aSAndroid Build Coastguard Worker <artifactId>json-schema-validator</artifactId> 195*78c4dd6aSAndroid Build Coastguard Worker <exclusions> 196*78c4dd6aSAndroid Build Coastguard Worker <exclusion> 197*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.ethlo.time</groupId> 198*78c4dd6aSAndroid Build Coastguard Worker <artifactId>itu</artifactId> 199*78c4dd6aSAndroid Build Coastguard Worker </exclusion> 200*78c4dd6aSAndroid Build Coastguard Worker </exclusions> 201*78c4dd6aSAndroid Build Coastguard Worker</dependency> 202*78c4dd6aSAndroid Build Coastguard Worker``` 203*78c4dd6aSAndroid Build Coastguard Worker 204*78c4dd6aSAndroid Build Coastguard Worker#### Community 205*78c4dd6aSAndroid Build Coastguard Worker 206*78c4dd6aSAndroid Build Coastguard WorkerThis 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](https://github.com/networknt/light-4j) framework in the same GitHub organization, it will be evolved and maintained along with the framework. 207*78c4dd6aSAndroid Build Coastguard Worker 208*78c4dd6aSAndroid Build Coastguard Worker## Prerequisite 209*78c4dd6aSAndroid Build Coastguard Worker 210*78c4dd6aSAndroid Build Coastguard WorkerThe library supports Java 8 and up. If you want to build from the source code, you need to install JDK 8 locally. To support multiple version of JDK, you can use [SDKMAN](https://www.networknt.com/tool/sdk/) 211*78c4dd6aSAndroid Build Coastguard Worker 212*78c4dd6aSAndroid Build Coastguard Worker## Usage 213*78c4dd6aSAndroid Build Coastguard Worker 214*78c4dd6aSAndroid Build Coastguard Worker### Adding the dependency 215*78c4dd6aSAndroid Build Coastguard Worker 216*78c4dd6aSAndroid Build Coastguard WorkerThis package is available on Maven central. 217*78c4dd6aSAndroid Build Coastguard Worker 218*78c4dd6aSAndroid Build Coastguard Worker#### Maven: 219*78c4dd6aSAndroid Build Coastguard Worker 220*78c4dd6aSAndroid Build Coastguard Worker```xml 221*78c4dd6aSAndroid Build Coastguard Worker<dependency> 222*78c4dd6aSAndroid Build Coastguard Worker <groupId>com.networknt</groupId> 223*78c4dd6aSAndroid Build Coastguard Worker <artifactId>json-schema-validator</artifactId> 224*78c4dd6aSAndroid Build Coastguard Worker <version>1.4.0</version> 225*78c4dd6aSAndroid Build Coastguard Worker</dependency> 226*78c4dd6aSAndroid Build Coastguard Worker``` 227*78c4dd6aSAndroid Build Coastguard Worker 228*78c4dd6aSAndroid Build Coastguard Worker#### Gradle: 229*78c4dd6aSAndroid Build Coastguard Worker 230*78c4dd6aSAndroid Build Coastguard Worker```java 231*78c4dd6aSAndroid Build Coastguard Workerdependencies { 232*78c4dd6aSAndroid Build Coastguard Worker implementation(group: 'com.networknt', name: 'json-schema-validator', version: '1.4.0'); 233*78c4dd6aSAndroid Build Coastguard Worker} 234*78c4dd6aSAndroid Build Coastguard Worker``` 235*78c4dd6aSAndroid Build Coastguard Worker 236*78c4dd6aSAndroid Build Coastguard Worker### Validating inputs against a schema 237*78c4dd6aSAndroid Build Coastguard Worker 238*78c4dd6aSAndroid Build Coastguard WorkerThe following example demonstrates how inputs are validated against a schema. It comprises the following steps. 239*78c4dd6aSAndroid Build Coastguard Worker 240*78c4dd6aSAndroid Build Coastguard Worker* Creating a schema factory with the default schema dialect and how the schemas can be retrieved. 241*78c4dd6aSAndroid Build Coastguard Worker * Configuring mapping the `$id` to a retrieval URI using `schemaMappers`. 242*78c4dd6aSAndroid Build Coastguard Worker * Configuring how the schemas are loaded using the retrieval URI using `schemaLoaders`. 243*78c4dd6aSAndroid Build Coastguard Worker For instance a `Map<String, String> schemas` containing a mapping of retrieval URI to schema data as a `String` can by configured using `builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(schemas))`. This also accepts a `Function<String, String> schemaRetrievalFunction`. 244*78c4dd6aSAndroid Build Coastguard Worker* Creating a configuration for controlling validator behavior. 245*78c4dd6aSAndroid Build Coastguard Worker* Loading a schema from a schema location along with the validator configuration. 246*78c4dd6aSAndroid Build Coastguard Worker* Using the schema to validate the data along with setting any execution specific configuration like for instance the locale or whether format assertions are enabled. 247*78c4dd6aSAndroid Build Coastguard Worker 248*78c4dd6aSAndroid Build Coastguard Worker```java 249*78c4dd6aSAndroid Build Coastguard Worker// This creates a schema factory that will use Draft 2012-12 as the default if $schema is not specified 250*78c4dd6aSAndroid Build Coastguard Worker// in the schema data. If $schema is specified in the schema data then that schema dialect will be used 251*78c4dd6aSAndroid Build Coastguard Worker// instead and this version is ignored. 252*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> 253*78c4dd6aSAndroid Build Coastguard Worker // This creates a mapping from $id which starts with https://www.example.org/ to the retrieval URI classpath:schema/ 254*78c4dd6aSAndroid Build Coastguard Worker builder.schemaMappers(schemaMappers -> schemaMappers.mapPrefix("https://www.example.org/", "classpath:schema/")) 255*78c4dd6aSAndroid Build Coastguard Worker); 256*78c4dd6aSAndroid Build Coastguard Worker 257*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig(); 258*78c4dd6aSAndroid Build Coastguard Worker// By default JSON Path is used for reporting the instance location and evaluation path 259*78c4dd6aSAndroid Build Coastguard Workerconfig.setPathType(PathType.JSON_POINTER); 260*78c4dd6aSAndroid Build Coastguard Worker// By default the JDK regular expression implementation which is not ECMA 262 compliant is used 261*78c4dd6aSAndroid Build Coastguard Worker// Note that setting this to true requires including the optional joni dependency 262*78c4dd6aSAndroid Build Coastguard Worker// config.setEcma262Validator(true); 263*78c4dd6aSAndroid Build Coastguard Worker 264*78c4dd6aSAndroid Build Coastguard Worker// Due to the mapping the schema will be retrieved from the classpath at classpath:schema/example-main.json. 265*78c4dd6aSAndroid Build Coastguard Worker// If the schema data does not specify an $id the absolute IRI of the schema location will be used as the $id. 266*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of("https://www.example.org/example-main.json"), config); 267*78c4dd6aSAndroid Build Coastguard WorkerString input = "{\r\n" 268*78c4dd6aSAndroid Build Coastguard Worker + " \"main\": {\r\n" 269*78c4dd6aSAndroid Build Coastguard Worker + " \"common\": {\r\n" 270*78c4dd6aSAndroid Build Coastguard Worker + " \"field\": \"invalidfield\"\r\n" 271*78c4dd6aSAndroid Build Coastguard Worker + " }\r\n" 272*78c4dd6aSAndroid Build Coastguard Worker + " }\r\n" 273*78c4dd6aSAndroid Build Coastguard Worker + "}"; 274*78c4dd6aSAndroid Build Coastguard Worker 275*78c4dd6aSAndroid Build Coastguard WorkerSet<ValidationMessage> assertions = schema.validate(input, InputFormat.JSON, executionContext -> { 276*78c4dd6aSAndroid Build Coastguard Worker // By default since Draft 2019-09 the format keyword only generates annotations and not assertions 277*78c4dd6aSAndroid Build Coastguard Worker executionContext.getConfig().setFormatAssertionsEnabled(true); 278*78c4dd6aSAndroid Build Coastguard Worker}); 279*78c4dd6aSAndroid Build Coastguard Worker``` 280*78c4dd6aSAndroid Build Coastguard Worker 281*78c4dd6aSAndroid Build Coastguard Worker### Validating a schema against a meta-schema 282*78c4dd6aSAndroid Build Coastguard Worker 283*78c4dd6aSAndroid Build Coastguard WorkerThe following example demonstrates how a schema is validated against a meta-schema. 284*78c4dd6aSAndroid Build Coastguard Worker 285*78c4dd6aSAndroid Build Coastguard WorkerThis is actually the same as validating inputs against a schema except in this case the input is the schema and the schema used is the meta-schema. 286*78c4dd6aSAndroid Build Coastguard Worker 287*78c4dd6aSAndroid Build Coastguard WorkerNote that the meta-schemas for Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12 are bundled with the library and these classpath resources will be used by default. 288*78c4dd6aSAndroid Build Coastguard Worker 289*78c4dd6aSAndroid Build Coastguard Worker```java 290*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V202012); 291*78c4dd6aSAndroid Build Coastguard Worker 292*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig(); 293*78c4dd6aSAndroid Build Coastguard Worker// By default JSON Path is used for reporting the instance location and evaluation path 294*78c4dd6aSAndroid Build Coastguard Workerconfig.setPathType(PathType.JSON_POINTER); 295*78c4dd6aSAndroid Build Coastguard Worker// By default the JDK regular expression implementation which is not ECMA 262 compliant is used 296*78c4dd6aSAndroid Build Coastguard Worker// Note that setting this to true requires including the optional joni dependency 297*78c4dd6aSAndroid Build Coastguard Worker// config.setEcma262Validator(true); 298*78c4dd6aSAndroid Build Coastguard Worker 299*78c4dd6aSAndroid Build Coastguard Worker// Due to the mapping the meta-schema will be retrieved from the classpath at classpath:draft/2020-12/schema. 300*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V202012), config); 301*78c4dd6aSAndroid Build Coastguard WorkerString input = "{\r\n" 302*78c4dd6aSAndroid Build Coastguard Worker + " \"type\": \"object\",\r\n" 303*78c4dd6aSAndroid Build Coastguard Worker + " \"properties\": {\r\n" 304*78c4dd6aSAndroid Build Coastguard Worker + " \"key\": {\r\n" 305*78c4dd6aSAndroid Build Coastguard Worker + " \"title\" : \"My key\",\r\n" 306*78c4dd6aSAndroid Build Coastguard Worker + " \"type\": \"invalidtype\"\r\n" 307*78c4dd6aSAndroid Build Coastguard Worker + " }\r\n" 308*78c4dd6aSAndroid Build Coastguard Worker + " }\r\n" 309*78c4dd6aSAndroid Build Coastguard Worker + "}"; 310*78c4dd6aSAndroid Build Coastguard WorkerSet<ValidationMessage> assertions = schema.validate(input, InputFormat.JSON, executionContext -> { 311*78c4dd6aSAndroid Build Coastguard Worker // By default since Draft 2019-09 the format keyword only generates annotations and not assertions 312*78c4dd6aSAndroid Build Coastguard Worker executionContext.getConfig().setFormatAssertionsEnabled(true); 313*78c4dd6aSAndroid Build Coastguard Worker}); 314*78c4dd6aSAndroid Build Coastguard Worker``` 315*78c4dd6aSAndroid Build Coastguard Worker### Results and output formats 316*78c4dd6aSAndroid Build Coastguard Worker 317*78c4dd6aSAndroid Build Coastguard Worker#### Results 318*78c4dd6aSAndroid Build Coastguard Worker 319*78c4dd6aSAndroid Build Coastguard WorkerThe following types of results are generated by the library. 320*78c4dd6aSAndroid Build Coastguard Worker 321*78c4dd6aSAndroid Build Coastguard Worker| Type | Description 322*78c4dd6aSAndroid Build Coastguard Worker|-------------|------------------- 323*78c4dd6aSAndroid Build Coastguard Worker| Assertions | Validation errors generated by a keyword on a particular input data instance. This is generally described in a `ValidationMessage` or in a `OutputUnit`. Note that since Draft 2019-09 the `format` keyword no longer generates assertions by default and instead generates only annotations unless configured otherwise using a configuration option or by using a meta-schema that uses the appropriate vocabulary. 324*78c4dd6aSAndroid Build Coastguard Worker| Annotations | Additional information generated by a keyword for a particular input data instance. This is generally described in a `OutputUnit`. Annotation collection and reporting is turned off by default. Annotations required by keywords such as `unevaluatedProperties` or `unevaluatedItems` are always collected for evaluation purposes and cannot be disabled but will not be reported unless configured to do so. 325*78c4dd6aSAndroid Build Coastguard Worker 326*78c4dd6aSAndroid Build Coastguard WorkerThe following information is used to describe both types of results. 327*78c4dd6aSAndroid Build Coastguard Worker 328*78c4dd6aSAndroid Build Coastguard Worker| Type | Description 329*78c4dd6aSAndroid Build Coastguard Worker|-------------------|------------------- 330*78c4dd6aSAndroid Build Coastguard Worker| Evaluation Path | This is the set of keys from the root through which evaluation passes to reach the schema for evaluating the instance. This includes `$ref` and `$dynamicRef`. eg. ```/properties/bar/$ref/properties/bar-prop``` 331*78c4dd6aSAndroid Build Coastguard Worker| Schema Location | This is the canonical IRI of the schema plus the JSON pointer fragment to the schema that was used for evaluating the instance. eg. ```https://json-schema.org/schemas/example#/$defs/bar/properties/bar-prop``` 332*78c4dd6aSAndroid Build Coastguard Worker| Instance Location | This is the JSON pointer fragment to the instance data that was being evaluated. eg. ```/bar/bar-prop``` 333*78c4dd6aSAndroid Build Coastguard Worker 334*78c4dd6aSAndroid Build Coastguard WorkerAssertions contains the following additional information 335*78c4dd6aSAndroid Build Coastguard Worker 336*78c4dd6aSAndroid Build Coastguard Worker| Type | Description 337*78c4dd6aSAndroid Build Coastguard Worker|-------------------|------------------- 338*78c4dd6aSAndroid Build Coastguard Worker| Message | The validation error message. 339*78c4dd6aSAndroid Build Coastguard Worker| Code | The error code. 340*78c4dd6aSAndroid Build Coastguard Worker| Message Key | The message key used for generating the message for localization. 341*78c4dd6aSAndroid Build Coastguard Worker| Arguments | The arguments used for generating the message. 342*78c4dd6aSAndroid Build Coastguard Worker| Type | The keyword that generated the message. 343*78c4dd6aSAndroid Build Coastguard Worker| Property | The property name that caused the validation error for example for the `required` keyword. Note that this is not part of the instance location as that points to the instance node. 344*78c4dd6aSAndroid Build Coastguard Worker| Schema Node | The `JsonNode` pointed to by the Schema Location. 345*78c4dd6aSAndroid Build Coastguard Worker| Instance Node | The `JsonNode` pointed to by the Instance Location. 346*78c4dd6aSAndroid Build Coastguard Worker| Details | Additional details that can be set by custom keyword validator implementations. This is not used by the library. 347*78c4dd6aSAndroid Build Coastguard Worker 348*78c4dd6aSAndroid Build Coastguard WorkerAnnotations contains the following additional information 349*78c4dd6aSAndroid Build Coastguard Worker 350*78c4dd6aSAndroid Build Coastguard Worker| Type | Description 351*78c4dd6aSAndroid Build Coastguard Worker|-------------------|------------------- 352*78c4dd6aSAndroid Build Coastguard Worker| Value | The annotation value generated 353*78c4dd6aSAndroid Build Coastguard Worker 354*78c4dd6aSAndroid Build Coastguard Worker 355*78c4dd6aSAndroid Build Coastguard Worker#### Output formats 356*78c4dd6aSAndroid Build Coastguard Worker 357*78c4dd6aSAndroid Build Coastguard WorkerThis library implements the Flag, List and Hierarchical output formats defined in the [Specification for Machine-Readable Output for JSON Schema Validation and Annotation](https://github.com/json-schema-org/json-schema-spec/blob/8270653a9f59fadd2df0d789f22d486254505bbe/jsonschema-validation-output-machines.md). 358*78c4dd6aSAndroid Build Coastguard Worker 359*78c4dd6aSAndroid Build Coastguard WorkerThe List and Hierarchical output formats are particularly helpful for understanding how the system arrived at a particular result. 360*78c4dd6aSAndroid Build Coastguard Worker 361*78c4dd6aSAndroid Build Coastguard Worker| Output Format | Description 362*78c4dd6aSAndroid Build Coastguard Worker|-------------------|------------------- 363*78c4dd6aSAndroid Build Coastguard Worker| Default | Generates the list of assertions. 364*78c4dd6aSAndroid Build Coastguard Worker| Boolean | Returns `true` if the validation is successful. Note that the fail fast option is turned on by default for this output format. 365*78c4dd6aSAndroid Build Coastguard Worker| Flag | Returns an `OutputFlag` object with `valid` having `true` if the validation is successful. Note that the fail fast option is turned on by default for this output format. 366*78c4dd6aSAndroid Build Coastguard Worker| List | Returns an `OutputUnit` object with `details` with a list of `OutputUnit` objects with the assertions and annotations. Note that annotations are not collected by default and it has to be enabled as it will impact performance. 367*78c4dd6aSAndroid Build Coastguard Worker| Hierarchical | Returns an `OutputUnit` object with a hierarchy of `OutputUnit` objects for the evaluation path with the assertions and annotations. Note that annotations are not collected by default and it has to be enabled as it will impact performance. 368*78c4dd6aSAndroid Build Coastguard Worker 369*78c4dd6aSAndroid Build Coastguard WorkerThe following example shows how to generate the hierarchical output format with annotation collection and reporting turned on and format assertions turned on. 370*78c4dd6aSAndroid Build Coastguard Worker 371*78c4dd6aSAndroid Build Coastguard Worker```java 372*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); 373*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig(); 374*78c4dd6aSAndroid Build Coastguard Workerconfig.setPathType(PathType.JSON_POINTER); 375*78c4dd6aSAndroid Build Coastguard Workerconfig.setFormatAssertionsEnabled(true); 376*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(SchemaLocation.of("https://json-schema.org/schemas/example"), config); 377*78c4dd6aSAndroid Build Coastguard Worker 378*78c4dd6aSAndroid Build Coastguard WorkerOutputUnit outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> { 379*78c4dd6aSAndroid Build Coastguard Worker executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true); 380*78c4dd6aSAndroid Build Coastguard Worker executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); 381*78c4dd6aSAndroid Build Coastguard Worker}); 382*78c4dd6aSAndroid Build Coastguard Worker``` 383*78c4dd6aSAndroid Build Coastguard WorkerThe following is sample output from the Hierarchical format. 384*78c4dd6aSAndroid Build Coastguard Worker 385*78c4dd6aSAndroid Build Coastguard Worker```json 386*78c4dd6aSAndroid Build Coastguard Worker{ 387*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 388*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "", 389*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#", 390*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "", 391*78c4dd6aSAndroid Build Coastguard Worker "droppedAnnotations" : { 392*78c4dd6aSAndroid Build Coastguard Worker "properties" : [ "foo", "bar" ], 393*78c4dd6aSAndroid Build Coastguard Worker "title" : "root" 394*78c4dd6aSAndroid Build Coastguard Worker }, 395*78c4dd6aSAndroid Build Coastguard Worker "details" : [ { 396*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 397*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "/properties/foo/allOf/0", 398*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#/properties/foo/allOf/0", 399*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "/foo", 400*78c4dd6aSAndroid Build Coastguard Worker "errors" : { 401*78c4dd6aSAndroid Build Coastguard Worker "required" : "required property 'unspecified-prop' not found" 402*78c4dd6aSAndroid Build Coastguard Worker } 403*78c4dd6aSAndroid Build Coastguard Worker }, { 404*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 405*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "/properties/foo/allOf/1", 406*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#/properties/foo/allOf/1", 407*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "/foo", 408*78c4dd6aSAndroid Build Coastguard Worker "droppedAnnotations" : { 409*78c4dd6aSAndroid Build Coastguard Worker "properties" : [ "foo-prop" ], 410*78c4dd6aSAndroid Build Coastguard Worker "title" : "foo-title", 411*78c4dd6aSAndroid Build Coastguard Worker "additionalProperties" : [ "foo-prop", "other-prop" ] 412*78c4dd6aSAndroid Build Coastguard Worker }, 413*78c4dd6aSAndroid Build Coastguard Worker "details" : [ { 414*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 415*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "/properties/foo/allOf/1/properties/foo-prop", 416*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#/properties/foo/allOf/1/properties/foo-prop", 417*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "/foo/foo-prop", 418*78c4dd6aSAndroid Build Coastguard Worker "errors" : { 419*78c4dd6aSAndroid Build Coastguard Worker "const" : "must be a constant value 1" 420*78c4dd6aSAndroid Build Coastguard Worker }, 421*78c4dd6aSAndroid Build Coastguard Worker "droppedAnnotations" : { 422*78c4dd6aSAndroid Build Coastguard Worker "title" : "foo-prop-title" 423*78c4dd6aSAndroid Build Coastguard Worker } 424*78c4dd6aSAndroid Build Coastguard Worker } ] 425*78c4dd6aSAndroid Build Coastguard Worker }, { 426*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 427*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "/properties/bar/$ref", 428*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#/$defs/bar", 429*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "/bar", 430*78c4dd6aSAndroid Build Coastguard Worker "droppedAnnotations" : { 431*78c4dd6aSAndroid Build Coastguard Worker "properties" : [ "bar-prop" ], 432*78c4dd6aSAndroid Build Coastguard Worker "title" : "bar-title" 433*78c4dd6aSAndroid Build Coastguard Worker }, 434*78c4dd6aSAndroid Build Coastguard Worker "details" : [ { 435*78c4dd6aSAndroid Build Coastguard Worker "valid" : false, 436*78c4dd6aSAndroid Build Coastguard Worker "evaluationPath" : "/properties/bar/$ref/properties/bar-prop", 437*78c4dd6aSAndroid Build Coastguard Worker "schemaLocation" : "https://json-schema.org/schemas/example#/$defs/bar/properties/bar-prop", 438*78c4dd6aSAndroid Build Coastguard Worker "instanceLocation" : "/bar/bar-prop", 439*78c4dd6aSAndroid Build Coastguard Worker "errors" : { 440*78c4dd6aSAndroid Build Coastguard Worker "minimum" : "must have a minimum value of 10" 441*78c4dd6aSAndroid Build Coastguard Worker }, 442*78c4dd6aSAndroid Build Coastguard Worker "droppedAnnotations" : { 443*78c4dd6aSAndroid Build Coastguard Worker "title" : "bar-prop-title" 444*78c4dd6aSAndroid Build Coastguard Worker } 445*78c4dd6aSAndroid Build Coastguard Worker } ] 446*78c4dd6aSAndroid Build Coastguard Worker } ] 447*78c4dd6aSAndroid Build Coastguard Worker} 448*78c4dd6aSAndroid Build Coastguard Worker``` 449*78c4dd6aSAndroid Build Coastguard Worker 450*78c4dd6aSAndroid Build Coastguard Worker## Configuration 451*78c4dd6aSAndroid Build Coastguard Worker 452*78c4dd6aSAndroid Build Coastguard Worker### Execution Configuration 453*78c4dd6aSAndroid Build Coastguard Worker 454*78c4dd6aSAndroid Build Coastguard Worker| Name | Description | Default Value 455*78c4dd6aSAndroid Build Coastguard Worker|--------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------- 456*78c4dd6aSAndroid Build Coastguard Worker| `annotationCollectionEnabled` | Controls whether annotations are collected during processing. Note that collecting annotations will adversely affect performance. | `false` 457*78c4dd6aSAndroid Build Coastguard Worker| `annotationCollectionFilter` | The predicate used to control which keyword to collect and report annotations for. This requires `annotationCollectionEnabled` to be `true`. | `keyword -> false` 458*78c4dd6aSAndroid Build Coastguard Worker| `locale` | The locale to use for generating messages in the `ValidationMessage`. Note that this value is copied from `SchemaValidatorsConfig` for each execution. | `Locale.getDefault()` 459*78c4dd6aSAndroid Build Coastguard Worker| `failFast` | Whether to return failure immediately when an assertion is generated. Note that this value is copied from `SchemaValidatorsConfig` for each execution but is automatically set to `true` for the Boolean and Flag output formats. | `false` 460*78c4dd6aSAndroid Build Coastguard Worker| `formatAssertionsEnabled` | The default is to generate format assertions from Draft 4 to Draft 7 and to only generate annotations from Draft 2019-09. Setting to `true` or `false` will override the default behavior. | `null` 461*78c4dd6aSAndroid Build Coastguard Worker 462*78c4dd6aSAndroid Build Coastguard Worker### Schema Validators Configuration 463*78c4dd6aSAndroid Build Coastguard Worker 464*78c4dd6aSAndroid Build Coastguard Worker| Name | Description | Default Value 465*78c4dd6aSAndroid Build Coastguard Worker|--------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------- 466*78c4dd6aSAndroid Build Coastguard Worker| `pathType` | The path type to use for reporting the instance location and evaluation path. Set to `PathType.JSON_POINTER` to use JSON Pointer. | `PathType.DEFAULT` 467*78c4dd6aSAndroid Build Coastguard Worker| `ecma262Validator` | Whether to use the ECMA 262 `joni` library to validate the `pattern` keyword. This requires the dependency to be manually added to the project or a `ClassNotFoundException` will be thrown. | `false` 468*78c4dd6aSAndroid Build Coastguard Worker| `executionContextCustomizer` | This can be used to customize the `ExecutionContext` generated by the `JsonSchema` for each validation run. | `null` 469*78c4dd6aSAndroid Build Coastguard Worker| `schemaIdValidator` | This is used to customize how the `$id` values are validated. Note that the default implementation allows non-empty fragments where no base IRI is specified and also allows non-absolute IRI `$id` values in the root schema. | `JsonSchemaIdValidator.DEFAULT` 470*78c4dd6aSAndroid Build Coastguard Worker| `messageSource` | This is used to retrieve the locale specific messages. | `DefaultMessageSource.getInstance()` 471*78c4dd6aSAndroid Build Coastguard Worker| `locale` | The locale to use for generating messages in the `ValidationMessage`. | `Locale.getDefault()` 472*78c4dd6aSAndroid Build Coastguard Worker| `failFast` | Whether to return failure immediately when an assertion is generated. | `false` 473*78c4dd6aSAndroid Build Coastguard Worker| `formatAssertionsEnabled` | The default is to generate format assertions from Draft 4 to Draft 7 and to only generate annotations from Draft 2019-09. Setting to `true` or `false` will override the default behavior. | `null` 474*78c4dd6aSAndroid Build Coastguard Worker 475*78c4dd6aSAndroid Build Coastguard Worker## Performance Considerations 476*78c4dd6aSAndroid Build Coastguard Worker 477*78c4dd6aSAndroid Build Coastguard WorkerWhen the library creates a schema from the schema factory, it creates a distinct validator instance for each location on the evaluation path. This means if there are different `$ref` that reference the same schema location, different validator instances are created for each evaluation path. 478*78c4dd6aSAndroid Build Coastguard Worker 479*78c4dd6aSAndroid Build Coastguard WorkerWhen the schema is created, the library will automatically preload all the validators needed and resolve references. At this point, no exceptions will be thrown if a reference cannot be resolved. If there are references that are cyclic, only the first cycle will be preloaded. If you wish to ensure that remote references can all be resolved, the `initializeValidators` method needs to be called on the `JsonSchema` which will throw an exception if there are references that cannot be resolved. 480*78c4dd6aSAndroid Build Coastguard Worker 481*78c4dd6aSAndroid Build Coastguard WorkerThe `JsonSchema` created from the factory should be cached and reused. Not reusing the `JsonSchema` means that the schema data needs to be repeated parsed with validator instances created and references resolved. 482*78c4dd6aSAndroid Build Coastguard Worker 483*78c4dd6aSAndroid Build Coastguard WorkerCollecting annotations will adversely affect validation performance. 484*78c4dd6aSAndroid Build Coastguard Worker 485*78c4dd6aSAndroid Build Coastguard WorkerThe earlier draft specifications contain less keywords that can potentially impact performance. 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. 486*78c4dd6aSAndroid Build Coastguard Worker 487*78c4dd6aSAndroid Build Coastguard WorkerThis does not mean that using a schema with a later draft specification will automatically cause a performance impact. For instance, the `properties` validator will perform checks to determine if annotations need to be collected, and checks if the meta-schema contains the `unevaluatedProperties` keyword and whether the `unevaluatedProperties` keyword exists adjacent the evaluation path. 488*78c4dd6aSAndroid Build Coastguard Worker 489*78c4dd6aSAndroid Build Coastguard Worker 490*78c4dd6aSAndroid Build Coastguard Worker## [Quick Start](doc/quickstart.md) 491*78c4dd6aSAndroid Build Coastguard Worker 492*78c4dd6aSAndroid Build Coastguard Worker## [Customizing Schema Retrieval](doc/schema-retrieval.md) 493*78c4dd6aSAndroid Build Coastguard Worker 494*78c4dd6aSAndroid Build Coastguard Worker## [Customizing Meta-Schemas, Vocabularies, Keywords and Formats](doc/custom-meta-schema.md) 495*78c4dd6aSAndroid Build Coastguard Worker 496*78c4dd6aSAndroid Build Coastguard Worker## [Validators](doc/validators.md) 497*78c4dd6aSAndroid Build Coastguard Worker 498*78c4dd6aSAndroid Build Coastguard Worker## [Configuration](doc/config.md) 499*78c4dd6aSAndroid Build Coastguard Worker 500*78c4dd6aSAndroid Build Coastguard Worker## [Specification Version](doc/specversion.md) 501*78c4dd6aSAndroid Build Coastguard Worker 502*78c4dd6aSAndroid Build Coastguard Worker## [YAML Validation](doc/yaml.md) 503*78c4dd6aSAndroid Build Coastguard Worker 504*78c4dd6aSAndroid Build Coastguard Worker## [Collector Context](doc/collector-context.md) 505*78c4dd6aSAndroid Build Coastguard Worker 506*78c4dd6aSAndroid Build Coastguard Worker## [JSON Schema Walkers and WalkListeners](doc/walkers.md) 507*78c4dd6aSAndroid Build Coastguard Worker 508*78c4dd6aSAndroid Build Coastguard Worker## [ECMA-262 Regex](doc/ecma-262.md) 509*78c4dd6aSAndroid Build Coastguard Worker 510*78c4dd6aSAndroid Build Coastguard Worker## [Custom Message](doc/cust-msg.md) 511*78c4dd6aSAndroid Build Coastguard Worker 512*78c4dd6aSAndroid Build Coastguard Worker## [Multiple Language](doc/multiple-language.md) 513*78c4dd6aSAndroid Build Coastguard Worker 514*78c4dd6aSAndroid Build Coastguard Worker## [MetaSchema Validation](doc/metaschema-validation.md) 515*78c4dd6aSAndroid Build Coastguard Worker 516*78c4dd6aSAndroid Build Coastguard Worker## [Validating RFC 3339 durations](doc/duration.md) 517*78c4dd6aSAndroid Build Coastguard Worker 518*78c4dd6aSAndroid Build Coastguard Worker## Projects 519*78c4dd6aSAndroid Build Coastguard Worker 520*78c4dd6aSAndroid Build Coastguard WorkerThe [light-rest-4j](https://github.com/networknt/light-rest-4j), [light-graphql-4j](https://github.com/networknt/light-graphql-4j) and [light-hybrid-4j](https://github.com/networknt/light-hybrid-4j) use this library to validate the request and response based on the specifications. If you are using other frameworks like Spring Boot, you can use the [OpenApiValidator](https://github.com/mservicetech/openapi-schema-validation), a generic OpenAPI 3.0 validator based on the OpenAPI 3.0 specification. 521*78c4dd6aSAndroid Build Coastguard Worker 522*78c4dd6aSAndroid Build Coastguard WorkerIf you have a project using this library, please submit a PR to add your project below. 523*78c4dd6aSAndroid Build Coastguard Worker 524*78c4dd6aSAndroid Build Coastguard Worker## Contributors 525*78c4dd6aSAndroid Build Coastguard Worker 526*78c4dd6aSAndroid Build Coastguard WorkerThanks to the following people who have contributed to this project. If you are using this library, please consider to be a sponsor for one of the contributors. 527*78c4dd6aSAndroid Build Coastguard Worker 528*78c4dd6aSAndroid Build Coastguard Worker[@stevehu](https://github.com/sponsors/stevehu) 529*78c4dd6aSAndroid Build Coastguard Worker 530*78c4dd6aSAndroid Build Coastguard Worker[@prashanth-chaitanya](https://github.com/prashanth-chaitanya) 531*78c4dd6aSAndroid Build Coastguard Worker 532*78c4dd6aSAndroid Build Coastguard Worker[@fdutton](https://github.com/fdutton) 533*78c4dd6aSAndroid Build Coastguard Worker 534*78c4dd6aSAndroid Build Coastguard Worker[@valfirst](https://github.com/valfirst) 535*78c4dd6aSAndroid Build Coastguard Worker 536*78c4dd6aSAndroid Build Coastguard Worker[@BalloonWen](https://github.com/BalloonWen) 537*78c4dd6aSAndroid Build Coastguard Worker 538*78c4dd6aSAndroid Build Coastguard Worker[@jiachen1120](https://github.com/jiachen1120) 539*78c4dd6aSAndroid Build Coastguard Worker 540*78c4dd6aSAndroid Build Coastguard Worker[@ddobrin](https://github.com/ddobrin) 541*78c4dd6aSAndroid Build Coastguard Worker 542*78c4dd6aSAndroid Build Coastguard Worker[@eskabetxe](https://github.com/eskabetxe) 543*78c4dd6aSAndroid Build Coastguard Worker 544*78c4dd6aSAndroid Build Coastguard Worker[@ehrmann](https://github.com/ehrmann) 545*78c4dd6aSAndroid Build Coastguard Worker 546*78c4dd6aSAndroid Build Coastguard Worker[@prashanthjos](https://github.com/prashanthjos) 547*78c4dd6aSAndroid Build Coastguard Worker 548*78c4dd6aSAndroid Build Coastguard Worker[@Subhajitdas298](https://github.com/Subhajitdas298) 549*78c4dd6aSAndroid Build Coastguard Worker 550*78c4dd6aSAndroid Build Coastguard Worker[@FWiesner](https://github.com/FWiesner) 551*78c4dd6aSAndroid Build Coastguard Worker 552*78c4dd6aSAndroid Build Coastguard Worker[@rhwood](https://github.com/rhwood) 553*78c4dd6aSAndroid Build Coastguard Worker 554*78c4dd6aSAndroid Build Coastguard Worker[@jawaff](https://github.com/jawaff) 555*78c4dd6aSAndroid Build Coastguard Worker 556*78c4dd6aSAndroid Build Coastguard Worker[@nitin1891](https://github.com/nitin1891) 557*78c4dd6aSAndroid Build Coastguard Worker 558*78c4dd6aSAndroid Build Coastguard Worker 559*78c4dd6aSAndroid Build Coastguard WorkerFor all contributors, please visit https://github.com/networknt/json-schema-validator/graphs/contributors 560*78c4dd6aSAndroid Build Coastguard Worker 561*78c4dd6aSAndroid Build Coastguard WorkerIf you are a contributor, please join the [GitHub Sponsors](https://github.com/sponsors) and switch the link to your sponsors dashboard via a PR. 562*78c4dd6aSAndroid Build Coastguard Worker 563*78c4dd6aSAndroid Build Coastguard Worker## Sponsors 564*78c4dd6aSAndroid Build Coastguard Worker 565*78c4dd6aSAndroid Build Coastguard Worker 566*78c4dd6aSAndroid Build Coastguard Worker### Individual Sponsors 567*78c4dd6aSAndroid Build Coastguard Worker 568*78c4dd6aSAndroid Build Coastguard Worker 569*78c4dd6aSAndroid Build Coastguard Worker### Corporation Sponsors 570*78c4dd6aSAndroid Build Coastguard Worker 571*78c4dd6aSAndroid Build Coastguard Worker 572*78c4dd6aSAndroid Build Coastguard Worker 573