Package com.networknt.schema
Class JsonSchema
- java.lang.Object
-
- com.networknt.schema.ValidationMessageHandler
-
- com.networknt.schema.BaseJsonValidator
-
- com.networknt.schema.JsonSchema
-
- All Implemented Interfaces:
JsonValidator,JsonSchemaWalker
public class JsonSchema extends BaseJsonValidator
This is the core of json constraint implementation. It parses json constraint file and generates JsonValidators. The class is thread safe, once it is constructed, it can be used to validate multiple json data concurrently.
-
-
Field Summary
-
Fields inherited from class com.networknt.schema.BaseJsonValidator
applyDefaultsStrategy, schemaNode, suppressSubSchemaRetrieval, validationContext
-
Fields inherited from class com.networknt.schema.ValidationMessageHandler
customErrorMessagesEnabled, errorMessage, errorMessageType, evaluationParentSchema, evaluationPath, failFast, keyword, messageSource, parentSchema, schemaLocation
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedJsonSchema(JsonSchema copy)Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ExecutionContextcreateExecutionContext()Creates an execution context.JsonSchemafindAncestor()JsonSchemafindLexicalRoot()JsonSchemafindSchemaResourceRoot()Finds the root of the schema resource.JsonSchemafromRef(JsonSchema refEvaluationParentSchema, JsonNodePath refEvaluationPath)Creates a schema using the current one as a template with the parent as the ref.URIgetCurrentUri()StringgetId()com.fasterxml.jackson.databind.JsonNodegetRefSchemaNode(String ref)Find the schema node for $ref attribute.JsonValidatorgetRequiredValidator()TypeValidatorgetTypeValidator()List<JsonValidator>getValidators()booleanhasRequiredValidator()booleanhasTypeValidator()voidinitializeValidators()Initializes the validators'JsonSchemainstances.booleanisDynamicAnchor()booleanisSchemaResourceRoot()Determines if this schema resource is a schema resource root.StringtoString()Set<ValidationMessage>validate(com.fasterxml.jackson.databind.JsonNode rootNode)Validate the given root JsonNode, starting at the root of the data path.<T> Tvalidate(com.fasterxml.jackson.databind.JsonNode rootNode, OutputFormat<T> format)Validates the given root JsonNode, starting at the root of the data path.<T> Tvalidate(com.fasterxml.jackson.databind.JsonNode rootNode, OutputFormat<T> format, com.networknt.schema.ExecutionCustomizer executionCustomizer)Validates the given root JsonNode, starting at the root of the data path.Set<ValidationMessage>validate(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation)START OF VALIDATE METHODSValidationResultvalidateAndCollect(com.fasterxml.jackson.databind.JsonNode node)ValidationResultvalidateAndCollect(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node)ValidationResultwalk(com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)ValidationResultwalk(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)Walk the JSON nodeSet<ValidationMessage>walk(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema)This is default implementation of walk method.ValidationResultwalkAtNode(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema)JsonSchemawithConfig(SchemaValidatorsConfig config)-
Methods inherited from class com.networknt.schema.BaseJsonValidator
atRoot, checkDiscriminatorMatch, debug, equals, fetchSubSchemaNode, getEvaluationParentSchema, getEvaluationPath, getKeyword, getNodeFieldType, getParentSchema, getSchemaLocation, getSchemaNode, preloadJsonSchemas, registerAndMergeDiscriminator, validate, validate, validate
-
Methods inherited from class com.networknt.schema.ValidationMessageHandler
getErrorCodeKey, getErrorMessage, getErrorMessageType, getMessageNode, isPartOfOneOfMultipleType, message, parseErrorCode, schemaLocationContains, updateErrorMessageType, updateKeyword, updateValidatorType
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.networknt.schema.JsonValidator
preloadJsonSchema
-
-
-
-
Constructor Detail
-
JsonSchema
protected JsonSchema(JsonSchema copy)
Copy constructor.- Parameters:
copy- to copy from
-
-
Method Detail
-
fromRef
public JsonSchema fromRef(JsonSchema refEvaluationParentSchema, JsonNodePath refEvaluationPath)
Creates a schema using the current one as a template with the parent as the ref.This is typically used if this schema is a schema resource that can be pointed to by various references.
- Parameters:
refEvaluationParentSchema- the parent refrefEvaluationPath- the ref evaluation path- Returns:
- the schema
-
withConfig
public JsonSchema withConfig(SchemaValidatorsConfig config)
-
getCurrentUri
public URI getCurrentUri()
-
getRefSchemaNode
public com.fasterxml.jackson.databind.JsonNode getRefSchemaNode(String ref)
Find the schema node for $ref attribute.- Parameters:
ref- String- Returns:
- JsonNode
-
findLexicalRoot
public JsonSchema findLexicalRoot()
-
findSchemaResourceRoot
public JsonSchema findSchemaResourceRoot()
Finds the root of the schema resource.This is either the schema document root or the subschema resource root.
- Returns:
- the root of the schema
-
isSchemaResourceRoot
public boolean isSchemaResourceRoot()
Determines if this schema resource is a schema resource root.This is either the schema document root or the subschema resource root.
- Returns:
- if this schema is a schema resource root
-
getId
public String getId()
-
findAncestor
public JsonSchema findAncestor()
-
validate
public Set<ValidationMessage> validate(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation)
START OF VALIDATE METHODS- Parameters:
executionContext- ExecutionContextjsonNode- JsonNoderootNode- JsonNodeinstanceLocation- JsonNodePath- Returns:
- A list of ValidationMessage if there is any validation error, or an empty list if there is no error.
-
validate
public Set<ValidationMessage> validate(com.fasterxml.jackson.databind.JsonNode rootNode)
Validate the given root JsonNode, starting at the root of the data path.- Parameters:
rootNode- JsonNode- Returns:
- A list of ValidationMessage if there is any validation error, or an empty list if there is no error.
-
validate
public <T> T validate(com.fasterxml.jackson.databind.JsonNode rootNode, OutputFormat<T> format)Validates the given root JsonNode, starting at the root of the data path. The output will be formatted using the formatter specified.- Type Parameters:
T- the result type- Parameters:
rootNode- the root noteformat- the formatter- Returns:
- the result
-
validate
public <T> T validate(com.fasterxml.jackson.databind.JsonNode rootNode, OutputFormat<T> format, com.networknt.schema.ExecutionCustomizer executionCustomizer)Validates the given root JsonNode, starting at the root of the data path. The output will be formatted using the formatter specified.- Type Parameters:
T- the result type- Parameters:
rootNode- the root noteformat- the formatterexecutionCustomizer- the execution customizer- Returns:
- the result
-
validateAndCollect
public ValidationResult validateAndCollect(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node)
-
validateAndCollect
public ValidationResult validateAndCollect(com.fasterxml.jackson.databind.JsonNode node)
-
walk
public ValidationResult walk(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)
Walk the JSON node- Parameters:
executionContext- ExecutionContextnode- JsonNodeshouldValidateSchema- indicator on validation- Returns:
- result of ValidationResult
-
walk
public ValidationResult walk(com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)
-
walkAtNode
public ValidationResult walkAtNode(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema)
-
walk
public Set<ValidationMessage> walk(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema)
Description copied from interface:JsonValidatorThis is default implementation of walk method. Its job is to call the validate method if shouldValidateSchema is enabled.- Parameters:
executionContext- ExecutionContextnode- JsonNoderootNode- JsonNodeinstanceLocation- JsonNodePathshouldValidateSchema- boolean- Returns:
- a set of validation messages if shouldValidateSchema is true.
-
toString
public String toString()
- Overrides:
toStringin classBaseJsonValidator
-
hasRequiredValidator
public boolean hasRequiredValidator()
-
getRequiredValidator
public JsonValidator getRequiredValidator()
-
hasTypeValidator
public boolean hasTypeValidator()
-
getTypeValidator
public TypeValidator getTypeValidator()
-
getValidators
public List<JsonValidator> getValidators()
-
initializeValidators
public void initializeValidators()
Initializes the validators'JsonSchemainstances. For avoiding issues with concurrency, in 1.0.49 theJsonSchemainstances affiliated with validators were modified to no more preload the schema and lazy loading is used instead.This comes with the issue that this way you cannot rely on validating important schema features, in particular
$refresolution at instantiation fromJsonSchemaFactory.By calling
initializeValidatorsyou can enforce preloading of theJsonSchemainstances of the validators.
-
isDynamicAnchor
public boolean isDynamicAnchor()
-
createExecutionContext
public ExecutionContext createExecutionContext()
Creates an execution context.- Returns:
- the execution context
-
-