Class JsonSchemaEngine

java.lang.Object
io.micronaut.jsonschema.validation.engine.JsonSchemaEngine

@Internal public final class JsonSchemaEngine extends Object
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 Details

    • DEFAULT_BASE_URI

      public static final String DEFAULT_BASE_URI
      The base URI used for schemas that do not declare $id when 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-schemas
      retriever - The retriever used to load referenced schemas
      defaultDialect - The dialect used for schemas that do not declare $schema
      defaultBaseUri - The base URI used for schemas that do not declare $id, or null for DEFAULT_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-schemas
      retriever - The retriever used to load referenced schemas
      defaultDialect - The dialect used for schemas that do not declare $schema
      defaultBaseUri - The base URI used for schemas that do not declare $id, or null for DEFAULT_BASE_URI
      assertFormats - Whether the format keyword is an assertion. When false, formats are only asserted for schemas whose meta-schema enables the 2020-12 format-assertion vocabulary
  • Method Details

    • compile

      public CompiledJsonSchema compile(io.micronaut.json.tree.JsonNode schema)
      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

      public CompiledJsonSchema compile(io.micronaut.json.tree.JsonNode schema, String baseUri)
      Compiles a schema.
      Parameters:
      schema - The schema
      baseUri - 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 schema
      instance - The instance
      Returns:
      The validation errors, empty if the instance is valid
    • isValid

      public boolean isValid(CompiledJsonSchema schema, io.micronaut.json.tree.JsonNode instance)
      Checks whether an instance is valid, stopping at the first error.
      Parameters:
      schema - The compiled schema
      instance - The instance
      Returns:
      true if valid