Class SchemaRegistry

java.lang.Object
com.networknt.schema.SchemaRegistry

public class SchemaRegistry extends Object
Registry for loading and registering Schema instances.

This can be created with withDefaultDialect(Version) or withDialect(Dialect).

The registry should be cached for performance.

A different registry should be used when loading unrelated schemas.

SchemaRegistry instances are thread-safe provided its configuration is not modified.

  • Method Details

    • builder

      public static SchemaRegistry.Builder builder()
      Builder without keywords or formats.

      Typically builder(SchemaRegistry) or withDefaultDialect(SpecificationVersion) or withDialect(Dialect) would be used instead.

      Returns:
      a builder instance without any keywords or formats - usually not what one needs.
    • withDefaultDialect

      public static SchemaRegistry withDefaultDialect(SpecificationVersion specificationVersion)
      Creates a new schema registry with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.

      This uses a dialect registry that contains all the supported standard specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.

      Parameters:
      specificationVersion - the default dialect id corresponding to the specification version used when the schema does not specify the $schema keyword
      Returns:
      the factory
    • withDefaultDialect

      public static SchemaRegistry withDefaultDialect(SpecificationVersion specificationVersion, Consumer<SchemaRegistry.Builder> customizer)
      Creates a new schema registry with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.

      This uses a dialect registry that contains all the supported standard specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.

      Parameters:
      specificationVersion - the default dialect id corresponding to the specification version used when the schema does not specify the $schema keyword
      customizer - to customize the registry
      Returns:
      the factory
    • withDefaultDialectId

      public static SchemaRegistry withDefaultDialectId(String dialectId, Consumer<SchemaRegistry.Builder> customizer)
      Creates a new schema registry with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.

      This uses a dialect registry that contains all the supported standard specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.

      Parameters:
      dialectId - the default dialect id used when the schema does not specify the $schema keyword
      customizer - to customize the registry
      Returns:
      the factory
    • withDefaultDialect

      public static SchemaRegistry withDefaultDialect(Dialect dialect)
      Creates a new schema registry with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.

      This uses a dialect registry that contains all the supported standard specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.

      Parameters:
      dialect - the default dialect used when the schema does not specify the $schema keyword
      Returns:
      the factory
    • withDefaultDialect

      public static SchemaRegistry withDefaultDialect(Dialect dialect, Consumer<SchemaRegistry.Builder> customizer)
      Creates a new schema registry with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.

      This uses a dialect registry that contains all the supported standard specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.

      Parameters:
      dialect - the default dialect used when the schema does not specify the $schema keyword
      customizer - to customize the registry
      Returns:
      the factory
    • withDialect

      public static SchemaRegistry withDialect(Dialect dialect)
      Gets a new schema registry that supports a specific dialect only.

      Schemas that do not specify dialect using $schema will use the dialect.

      This uses a dialect registry that only contains this dialect and will throw an exception for unknown dialects.

      Parameters:
      dialect - the dialect
      Returns:
      the schema registry
    • withDialect

      public static SchemaRegistry withDialect(Dialect dialect, Consumer<SchemaRegistry.Builder> customizer)
      Gets a new schema registry that supports a list of specific dialects only.

      Schemas that do not specify dialect using $schema will use the first dialect on the list.

      This uses a dialect registry that only contains the list of dialects and will throw an exception for unknown dialects.

      Parameters:
      dialect - the dialect
      customizer - to customize the registry
      Returns:
      the schema registry
    • withDialects

      public static SchemaRegistry withDialects(List<Dialect> dialects)
      Gets a new schema registry that supports a list of specific dialects only.

      Schemas that do not specify dialect using $schema will use the first dialect on the list.

      This uses a dialect registry that only contains the list of dialects and will throw an exception for unknown dialects.

      Parameters:
      dialects - the dialects with the first being the default dialect
      Returns:
      the schema registry
    • withDialects

      public static SchemaRegistry withDialects(List<Dialect> dialects, Consumer<SchemaRegistry.Builder> customizer)
      Gets a new schema registry that supports a specific dialect only.

      Schemas that do not specify dialect using $schema will use the dialect.

      This uses a dialect registry that only contains this dialect and will throw an exception for unknown dialects.

      Parameters:
      dialects - the dialects with the first being the default dialect
      customizer - to customize the registry
      Returns:
      the schema registry
    • builder

      public static SchemaRegistry.Builder builder(SchemaRegistry blueprint)
      Builder from an existing SchemaRegistry.

      SchemaRegistry.builder(SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2019_09));

      Parameters:
      blueprint - the existing factory
      Returns:
      the builder
    • getSchemaLoader

      public SchemaLoader getSchemaLoader()
      Gets the schema loader.
      Returns:
      the schema loader
    • newSchema

      protected Schema newSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode schemaNode)
      Creates a schema from initial input.
      Parameters:
      schemaUri - the schema location
      schemaNode - the schema data node
      Returns:
      the schema
    • create

      public Schema create(SchemaContext schemaContext, SchemaLocation schemaLocation, com.fasterxml.jackson.databind.JsonNode schemaNode, Schema parentSchema)
    • getSchemaLocation

      protected SchemaLocation getSchemaLocation(SchemaLocation schemaLocation)
      Gets the base IRI from the schema retrieval IRI if present otherwise return one with a null base IRI.

      Note that the resolving of the $id or id in the schema node will take place in the Schema constructor.

      Parameters:
      schemaLocation - the schema retrieval uri
      Returns:
      the schema location
    • createSchemaContext

      protected SchemaContext createSchemaContext(com.fasterxml.jackson.databind.JsonNode schemaNode)
    • getDialect

      public Dialect getDialect(String dialectId)
      Gets the dialect that is available to the registry.
      Parameters:
      dialectId - the IRI of the meta-schema
      Returns:
      the meta-schema
    • getSchema

      public Schema getSchema(String schema)
      Gets the schema.

      Using this is not recommended as there is potentially no base IRI for resolving references to the absolute IRI.

      Parameters:
      schema - the schema data as a string
      Returns:
      the schema
    • getSchema

      public Schema getSchema(String schema, InputFormat inputFormat)
      Gets the schema.

      Using this is not recommended as there is potentially no base IRI for resolving references to the absolute IRI.

      Parameters:
      schema - the schema data as a string
      inputFormat - input format
      Returns:
      the schema
    • getSchema

      public Schema getSchema(InputStream schemaStream)
      Gets the schema.

      Using this is not recommended as there is potentially no base IRI for resolving references to the absolute IRI.

      Parameters:
      schemaStream - the input stream with the schema data
      Returns:
      the schema
    • getSchema

      public Schema getSchema(InputStream schemaStream, InputFormat inputFormat)
      Gets the schema.

      Using this is not recommended as there is potentially no base IRI for resolving references to the absolute IRI.

      Parameters:
      schemaStream - the input stream with the schema data
      inputFormat - input format
      Returns:
      the schema
    • getSchema

      public Schema getSchema(SchemaLocation schemaUri)
      Gets the schema.
      Parameters:
      schemaUri - the absolute IRI of the schema which can map to the retrieval IRI.
      Returns:
      the schema
    • getSchema

      public Schema getSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode)
      Gets the schema.
      Parameters:
      schemaUri - the base absolute IRI
      jsonNode - the node
      Returns:
      the schema
    • getSchema

      public Schema getSchema(SchemaLocation schemaUri, String schema, InputFormat inputFormat)
      Gets the schema.
      Parameters:
      schemaUri - the base absolute IRI
      schema - the input schema data
      inputFormat - input format
      Returns:
      the schema
    • getSchema

      public Schema getSchema(SchemaLocation schemaUri, InputStream schemaStream, InputFormat inputFormat)
      Gets the schema.
      Parameters:
      schemaUri - the base absolute IRI
      schemaStream - the input schema data
      inputFormat - input format
      Returns:
      the schema
    • getSchema

      public Schema getSchema(com.fasterxml.jackson.databind.JsonNode jsonNode)
      Gets the schema.

      Using this is not recommended as there is potentially no base IRI for resolving references to the absolute IRI.

      Prefer getSchema(SchemaLocation, JsonNode) instead to ensure the base IRI if no id is present.

      Parameters:
      jsonNode - the node
      Returns:
      the schema
    • loadSchema

      public Schema loadSchema(SchemaLocation schemaUri)
      Loads the schema.
      Parameters:
      schemaUri - the absolute IRI of the schema which can map to the retrieval IRI.
      Returns:
      the schema
    • getMappedSchema

      protected Schema getMappedSchema(SchemaLocation schemaUri)
    • getSchemaRegistryConfig

      public SchemaRegistryConfig getSchemaRegistryConfig()
      Gets the schema registry config.
      Returns:
      the schema registry config
    • normalizeDialectId

      protected static String normalizeDialectId(String id)
      Normalizes the standard JSON schema dialects.

      This should not normalize any other unrecognized dialects.

      Parameters:
      id - the $schema identifier
      Returns:
      the normalized uri