Class JsonSchemaFactory
- java.lang.Object
-
- com.networknt.schema.JsonSchemaFactory
-
public class JsonSchemaFactory extends java.lang.ObjectFactory for buildingJsonSchemainstances. The factory should be typically be created usinggetInstance(VersionFlag, Consumer)and should be cached for performance.JsonSchemaFactory instances are thread-safe provided its configuration is not modified.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJsonSchemaFactory.Builder
-
Field Summary
Fields Modifier and Type Field Description private static java.util.List<SchemaLoader>DEFAULT_SCHEMA_LOADERSprivate static java.util.List<SchemaMapper>DEFAULT_SCHEMA_MAPPERSprivate java.lang.StringdefaultMetaSchemaIriprivate booleanenableSchemaCacheprivate com.fasterxml.jackson.databind.ObjectMapperjsonMapperprivate JsonNodeReaderjsonNodeReaderprivate static org.slf4j.Loggerloggerprivate JsonMetaSchemaFactorymetaSchemaFactoryprivate java.util.concurrent.ConcurrentMap<java.lang.String,JsonMetaSchema>metaSchemasprivate java.util.concurrent.ConcurrentMap<SchemaLocation,JsonSchema>schemaCacheprivate SchemaLoaderschemaLoaderprivate SchemaLoaders.BuilderschemaLoadersBuilderprivate SchemaMappers.BuilderschemaMappersBuilderprivate com.fasterxml.jackson.databind.ObjectMapperyamlMapper
-
Constructor Summary
Constructors Modifier Constructor Description privateJsonSchemaFactory(com.fasterxml.jackson.databind.ObjectMapper jsonMapper, com.fasterxml.jackson.databind.ObjectMapper yamlMapper, JsonNodeReader jsonNodeReader, java.lang.String defaultMetaSchemaIri, SchemaLoaders.Builder schemaLoadersBuilder, SchemaMappers.Builder schemaMappersBuilder, java.util.concurrent.ConcurrentMap<java.lang.String,JsonMetaSchema> metaSchemas, boolean enableSchemaCache, JsonMetaSchemaFactory metaSchemaFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static JsonSchemaFactory.Builderbuilder()Builder without keywords or formats.static JsonSchemaFactory.Builderbuilder(JsonSchemaFactory blueprint)Builder from an existingJsonSchemaFactory.static JsonSchemaVersioncheckVersion(SpecVersion.VersionFlag versionFlag)Gets the json schema version to get the meta schema.JsonSchemacreate(ValidationContext validationContext, SchemaLocation schemaLocation, JsonNodePath evaluationPath, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parentSchema)protected SchemaValidatorsConfigcreateSchemaValidatorsConfig()Creates a schema validators config.protected ValidationContextcreateValidationContext(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)private JsonSchemadoCreate(ValidationContext validationContext, SchemaLocation schemaLocation, JsonNodePath evaluationPath, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parentSchema, boolean suppressSubSchemaRetrieval)static JsonSchemaFactorygetInstance(SpecVersion.VersionFlag versionFlag)Creates a factory with a default schema dialect.static JsonSchemaFactorygetInstance(SpecVersion.VersionFlag versionFlag, java.util.function.Consumer<JsonSchemaFactory.Builder> customizer)Creates a factory with a default schema dialect.(package private) com.fasterxml.jackson.databind.ObjectMappergetJsonMapper()protected JsonSchemagetMappedSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)private JsonMetaSchemagetMetaSchema(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)JsonMetaSchemagetMetaSchema(java.lang.String iri, SchemaValidatorsConfig config)Gets the meta-schema that is available to the factory.private JsonMetaSchemagetMetaSchemaOrDefault(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)(package private) com.fasterxml.jackson.databind.ObjectMappergetObjectMapper(InputFormat inputFormat)JsonSchemagetSchema(com.fasterxml.jackson.databind.JsonNode jsonNode)Gets the schema.JsonSchemagetSchema(com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(SchemaLocation schemaUri)Gets the schema.JsonSchemagetSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode)Gets the schema.JsonSchemagetSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(java.io.InputStream schemaStream)Gets the schema.JsonSchemagetSchema(java.io.InputStream schemaStream, InputFormat inputFormat, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(java.io.InputStream schemaStream, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(java.lang.String schema)Gets the schema.JsonSchemagetSchema(java.lang.String schema, InputFormat inputFormat)Gets the schema.JsonSchemagetSchema(java.lang.String schema, InputFormat inputFormat, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(java.lang.String schema, SchemaValidatorsConfig config)Gets the schema.JsonSchemagetSchema(java.net.URI schemaUri)Gets the schema.JsonSchemagetSchema(java.net.URI schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode)Gets the schema.JsonSchemagetSchema(java.net.URI schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)Gets the schema.SchemaLoadergetSchemaLoader()protected SchemaLocationgetSchemaLocation(SchemaLocation schemaLocation)Gets the base IRI from the schema retrieval IRI if present otherwise return one with a null base IRI.(package private) com.fasterxml.jackson.databind.ObjectMappergetYamlMapper()private booleanisYaml(SchemaLocation schemaUri)protected JsonMetaSchemaloadMetaSchema(java.lang.String iri, SchemaValidatorsConfig config)Loads the meta-schema from the configured meta-schema factory.protected JsonSchemaloadSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)Loads the schema.protected JsonSchemanewJsonSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)Creates a json schema from initial input.protected static java.lang.StringnormalizeMetaSchemaUri(java.lang.String id)Normalizes the standard JSON schema dialects.private voidpreload(JsonSchema jsonSchema, SchemaValidatorsConfig config)Preloads the json schema if the configuration option is set.(package private) com.fasterxml.jackson.databind.JsonNodereadTree(java.io.InputStream content, InputFormat inputFormat)(package private) com.fasterxml.jackson.databind.JsonNodereadTree(java.lang.String content, InputFormat inputFormat)private ValidationContextwithMetaSchema(ValidationContext validationContext, com.fasterxml.jackson.databind.JsonNode schemaNode)Determines the validation context to use for the schema given the parent validation context.
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
-
jsonMapper
private final com.fasterxml.jackson.databind.ObjectMapper jsonMapper
-
yamlMapper
private final com.fasterxml.jackson.databind.ObjectMapper yamlMapper
-
jsonNodeReader
private final JsonNodeReader jsonNodeReader
-
defaultMetaSchemaIri
private final java.lang.String defaultMetaSchemaIri
-
schemaLoadersBuilder
private final SchemaLoaders.Builder schemaLoadersBuilder
-
schemaMappersBuilder
private final SchemaMappers.Builder schemaMappersBuilder
-
schemaLoader
private final SchemaLoader schemaLoader
-
metaSchemas
private final java.util.concurrent.ConcurrentMap<java.lang.String,JsonMetaSchema> metaSchemas
-
schemaCache
private final java.util.concurrent.ConcurrentMap<SchemaLocation,JsonSchema> schemaCache
-
enableSchemaCache
private final boolean enableSchemaCache
-
metaSchemaFactory
private final JsonMetaSchemaFactory metaSchemaFactory
-
DEFAULT_SCHEMA_LOADERS
private static final java.util.List<SchemaLoader> DEFAULT_SCHEMA_LOADERS
-
DEFAULT_SCHEMA_MAPPERS
private static final java.util.List<SchemaMapper> DEFAULT_SCHEMA_MAPPERS
-
-
Constructor Detail
-
JsonSchemaFactory
private JsonSchemaFactory(com.fasterxml.jackson.databind.ObjectMapper jsonMapper, com.fasterxml.jackson.databind.ObjectMapper yamlMapper, JsonNodeReader jsonNodeReader, java.lang.String defaultMetaSchemaIri, SchemaLoaders.Builder schemaLoadersBuilder, SchemaMappers.Builder schemaMappersBuilder, java.util.concurrent.ConcurrentMap<java.lang.String,JsonMetaSchema> metaSchemas, boolean enableSchemaCache, JsonMetaSchemaFactory metaSchemaFactory)
-
-
Method Detail
-
getSchemaLoader
public SchemaLoader getSchemaLoader()
-
builder
public static JsonSchemaFactory.Builder builder()
Builder without keywords or formats. Typicallybuilder(JsonSchemaFactory)is what is required.- Returns:
- a builder instance without any keywords or formats - usually not what one needs.
-
getInstance
public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag)
Creates a factory with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.- Parameters:
versionFlag- the default dialect- Returns:
- the factory
-
getInstance
public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag, java.util.function.Consumer<JsonSchemaFactory.Builder> customizer)
Creates a factory with a default schema dialect. The schema dialect will only be used if the input does not specify a $schema.- Parameters:
versionFlag- the default dialectcustomizer- to customize the factory- Returns:
- the factory
-
checkVersion
public static JsonSchemaVersion checkVersion(SpecVersion.VersionFlag versionFlag)
Gets the json schema version to get the meta schema.This throws an
IllegalArgumentExceptionfor an unsupported value.- Parameters:
versionFlag- the schema dialect- Returns:
- the version
-
builder
public static JsonSchemaFactory.Builder builder(JsonSchemaFactory blueprint)
Builder from an existingJsonSchemaFactory.JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909));- Parameters:
blueprint- the existing factory- Returns:
- the builder
-
newJsonSchema
protected JsonSchema newJsonSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)
Creates a json schema from initial input.- Parameters:
schemaUri- the schema locationschemaNode- the schema data nodeconfig- the config to use- Returns:
- the schema
-
preload
private void preload(JsonSchema jsonSchema, SchemaValidatorsConfig config)
Preloads the json schema if the configuration option is set.- Parameters:
jsonSchema- the schema to preloadconfig- containing the configuration option
-
create
public JsonSchema create(ValidationContext validationContext, SchemaLocation schemaLocation, JsonNodePath evaluationPath, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parentSchema)
-
doCreate
private JsonSchema doCreate(ValidationContext validationContext, SchemaLocation schemaLocation, JsonNodePath evaluationPath, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parentSchema, boolean suppressSubSchemaRetrieval)
-
withMetaSchema
private ValidationContext withMetaSchema(ValidationContext validationContext, com.fasterxml.jackson.databind.JsonNode schemaNode)
Determines the validation context to use for the schema given the parent validation context.This is typically the same validation context unless the schema has a different $schema from the parent.
If the schema does not define a $schema, the parent should be used.
- Parameters:
validationContext- the parent validation contextschemaNode- the schema node- Returns:
- the validation context to use
-
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 JsonSchema constructor.
- Parameters:
schemaLocation- the schema retrieval uri- Returns:
- the schema location
-
createValidationContext
protected ValidationContext createValidationContext(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)
-
getMetaSchema
private JsonMetaSchema getMetaSchema(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)
-
getMetaSchemaOrDefault
private JsonMetaSchema getMetaSchemaOrDefault(com.fasterxml.jackson.databind.JsonNode schemaNode, SchemaValidatorsConfig config)
-
getMetaSchema
public JsonMetaSchema getMetaSchema(java.lang.String iri, SchemaValidatorsConfig config)
Gets the meta-schema that is available to the factory.- Parameters:
iri- the IRI of the meta-schemaconfig- the schema validators config- Returns:
- the meta-schema
-
loadMetaSchema
protected JsonMetaSchema loadMetaSchema(java.lang.String iri, SchemaValidatorsConfig config)
Loads the meta-schema from the configured meta-schema factory.- Parameters:
iri- the IRI of the meta-schemaconfig- the schema validators config- Returns:
- the meta-schema
-
readTree
com.fasterxml.jackson.databind.JsonNode readTree(java.lang.String content, InputFormat inputFormat) throws java.io.IOException- Throws:
java.io.IOException
-
readTree
com.fasterxml.jackson.databind.JsonNode readTree(java.io.InputStream content, InputFormat inputFormat) throws java.io.IOException- Throws:
java.io.IOException
-
getObjectMapper
com.fasterxml.jackson.databind.ObjectMapper getObjectMapper(InputFormat inputFormat)
-
getSchema
public JsonSchema getSchema(java.lang.String schema, SchemaValidatorsConfig config)
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 stringconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.lang.String schema, InputFormat inputFormat, SchemaValidatorsConfig config)
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 stringinputFormat- input formatconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.lang.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 JsonSchema getSchema(java.lang.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 stringinputFormat- input format- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.io.InputStream schemaStream, SchemaValidatorsConfig config)
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 dataconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.io.InputStream schemaStream, InputFormat inputFormat, SchemaValidatorsConfig config)
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 datainputFormat- input formatconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.io.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 JsonSchema getSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)
Gets the schema.- Parameters:
schemaUri- the absolute IRI of the schema which can map to the retrieval IRI.config- the config- Returns:
- the schema
-
loadSchema
protected JsonSchema loadSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)
Loads the schema.- Parameters:
schemaUri- the absolute IRI of the schema which can map to the retrieval IRI.config- the config- Returns:
- the schema
-
getYamlMapper
com.fasterxml.jackson.databind.ObjectMapper getYamlMapper()
-
getJsonMapper
com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()
-
createSchemaValidatorsConfig
protected SchemaValidatorsConfig createSchemaValidatorsConfig()
Creates a schema validators config.- Returns:
- the schema validators config
-
getMappedSchema
protected JsonSchema getMappedSchema(SchemaLocation schemaUri, SchemaValidatorsConfig config)
-
getSchema
public JsonSchema getSchema(java.net.URI schemaUri)
Gets the schema.- Parameters:
schemaUri- the absolute IRI of the schema which can map to the retrieval IRI.- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.net.URI schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)
Gets the schema.- Parameters:
schemaUri- the absolute IRI of the schema which can map to the retrieval IRI.jsonNode- the nodeconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(java.net.URI schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode)
Gets the schema.- Parameters:
schemaUri- the absolute IRI of the schema which can map to the retrieval IRI.jsonNode- the node- Returns:
- the schema
-
getSchema
public JsonSchema 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 JsonSchema getSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)
Gets the schema.- Parameters:
schemaUri- the base absolute IRIjsonNode- the nodeconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(SchemaLocation schemaUri, com.fasterxml.jackson.databind.JsonNode jsonNode)
Gets the schema.- Parameters:
schemaUri- the base absolute IRIjsonNode- the node- Returns:
- the schema
-
getSchema
public JsonSchema getSchema(com.fasterxml.jackson.databind.JsonNode jsonNode, SchemaValidatorsConfig config)
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, SchemaValidatorsConfig)instead to ensure the base IRI if no id is present.- Parameters:
jsonNode- the nodeconfig- the config- Returns:
- the schema
-
getSchema
public JsonSchema 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
-
isYaml
private boolean isYaml(SchemaLocation schemaUri)
-
normalizeMetaSchemaUri
protected static java.lang.String normalizeMetaSchemaUri(java.lang.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
-
-