See: Description
This implementation is meant to be purely server-side if Java is your language of choice. You may, or may not, use it in your Java Web application; this library has no dependencies on anything Web-related.
This project uses the infrastructure provided by json-schema-core, which means you can use all the power in this library to include the processors provided in this package into your own custom chains.
What is more, you can define your own schemas, with dedicated keywords and/or format attributes.
This library has complete draft v3 and draft v4 validation support.
It also has three particular features making it stand apart:
There are code examples in package com.github.fge.jsonschema.examples
which you can use to get started.
The validation process is a five step process:
This is a critically important part of the validation process. You may encounter, during validation, what is called a JSON Reference. A JSON Reference is a JSON Object with one member named $ref, and the value of this member is a text value which embodies a URI. Implementations are required to follow JSON References until an actual final content is reached (and this content may, or may not, be a valid JSON Schema).
Also, consider this schema:
{ "$ref": "some://where/else", "minimum": 3 }
This is still a JSON Reference. Other schema keywords, such as minimum in this example, should be ignored. And this is what this implementation does.
Note that any failure in $ref validation is considered a fatal error.
The processor in charge of this step is RefResolver
. It is located in package
json-schema-core.
This is an equally important part of the validation process. One thing to note about the previous step is that it will only check that JSON Reference resolution ultimately leads to a JSON document, whatever that document is. Which means it may not even be a JSON Object, therefore not a JSON Schema. This basic check is done at that level.
After schema syntax checking is done, you are ensured that the schema is well formed: this simplifies later processing. Note however that syntax checking will not follow JSON References.
The processor in charge of this step is SyntaxProcessor
. It is located in
the json-schema-core package.
This step of the processing chain takes into account both the schema and the instance to validate. Its role is to check the instance type, pick the relevant keywords for that instance type into the current schema, and build digested forms of these keywords for the next step.
For instance, consider that you are validating a number instance, and the current schema reads:
{ "minItems": 3, "maximum": 3 }
minItems
does not apply to numbers, it will therefore be filtered out
here. But this is not all. Now consider those two schemas:
{ "additionalItems": true }
{ "additionalItems": { "type": "string" } }
They have exactly the same influence on the validation of the array itself (note: the array, not its elements). The digest for these two schemas when the instance to validate is an array will therefore be the same. This allows to prune a lot of duplicates out of keyword caching.
The processor in charge of this step is com.github.fge.jsonschema.processing.digest.SchemaDigester
.
At this step, the digested form of the current schema is grabbed and all relevant keywords are built (if they are not found in the cache). The real validation can now take place.
Again, all results are cached for future reuse. And as all keywords are context-free, a same result can be reused across different schemas.
The processor in charge of this step is ValidatorBuilder
.
This is the part which actually matters to end users: ensure that their data is valid.
This step of the processing consists of two distinct elements:
As some keywords require that this very same processor be called again, all
keyword validators keep a reference to it: this is what also allows keywords
such as anyOf
, allOf
and others to work correctly. In a similar
manner, this processor also needs a reference to the start of the chain (the
reference resolver processor).
The processor in charge of this step is ValidationProcessor
.
Copyright © 2014. All Rights Reserved.