Class JsonSchemaEngine
java.lang.Object
io.micronaut.jsonschema.validation.engine.JsonSchemaEngine
A JSON Schema validator operating on Micronaut's
JsonNode tree.
Supports the draft-04, draft-06, draft-07, 2019-09 and 2020-12 dialects. Schemas are compiled once
into an immutable graph that can be evaluated concurrently. Referenced documents are loaded through the
SchemaRetriever the first time they are needed and cached for the lifetime of the engine. The
meta-schemas of the supported dialects are bundled and resolved without network access.
- Since:
- 2.3.2
- Author:
- Graeme Rocher
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe base URI used for schemas that do not declare$idwhen no base URI was configured. -
Constructor Summary
ConstructorsConstructorDescriptionJsonSchemaEngine(io.micronaut.json.JsonMapper jsonMapper, SchemaRetriever retriever, Dialect defaultDialect, @Nullable String defaultBaseUri) Creates an engine that asserts formats.JsonSchemaEngine(io.micronaut.json.JsonMapper jsonMapper, SchemaRetriever retriever, Dialect defaultDialect, @Nullable String defaultBaseUri, boolean assertFormats) Creates an engine. -
Method Summary
Modifier and TypeMethodDescriptioncompile(io.micronaut.json.tree.JsonNode schema) Compiles a schema whose base URI is the engine's default base URI.Compiles a schema.booleanisValid(CompiledJsonSchema schema, io.micronaut.json.tree.JsonNode instance) Checks whether an instance is valid, stopping at the first error.validate(CompiledJsonSchema schema, io.micronaut.json.tree.JsonNode instance) Validates an instance.
-
Field Details
-
DEFAULT_BASE_URI
The base URI used for schemas that do not declare$idwhen no base URI was configured.- See Also:
-
-
Constructor Details
-
JsonSchemaEngine
public JsonSchemaEngine(io.micronaut.json.JsonMapper jsonMapper, SchemaRetriever retriever, Dialect defaultDialect, @Nullable String defaultBaseUri) Creates an engine that asserts formats.- Parameters:
jsonMapper- The mapper used to parse the bundled meta-schemasretriever- The retriever used to load referenced schemasdefaultDialect- The dialect used for schemas that do not declare$schemadefaultBaseUri- The base URI used for schemas that do not declare$id, or null forDEFAULT_BASE_URI
-
JsonSchemaEngine
public JsonSchemaEngine(io.micronaut.json.JsonMapper jsonMapper, SchemaRetriever retriever, Dialect defaultDialect, @Nullable String defaultBaseUri, boolean assertFormats) Creates an engine.- Parameters:
jsonMapper- The mapper used to parse the bundled meta-schemasretriever- The retriever used to load referenced schemasdefaultDialect- The dialect used for schemas that do not declare$schemadefaultBaseUri- The base URI used for schemas that do not declare$id, or null forDEFAULT_BASE_URIassertFormats- Whether theformatkeyword is an assertion. When false, formats are only asserted for schemas whose meta-schema enables the 2020-12 format-assertion vocabulary
-
-
Method Details
-
compile
Compiles a schema whose base URI is the engine's default base URI.- Parameters:
schema- The schema- Returns:
- The compiled schema
- Throws:
IllegalArgumentException- If the schema is invalid
-
compile
Compiles a schema.- Parameters:
schema- The schemabaseUri- The base URI the schema was retrieved from, used to resolve relative identifiers and references- Returns:
- The compiled schema
- Throws:
IllegalArgumentException- If the schema is invalid
-
validate
public List<ValidationMessage> validate(CompiledJsonSchema schema, io.micronaut.json.tree.JsonNode instance) Validates an instance.- Parameters:
schema- The compiled schemainstance- The instance- Returns:
- The validation errors, empty if the instance is valid
-
isValid
Checks whether an instance is valid, stopping at the first error.- Parameters:
schema- The compiled schemainstance- The instance- Returns:
- true if valid
-