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
customMessage, errorMessageType, failFast, parentSchema, resourceBundle, schemaPath, validatorType
-
-
Constructor Summary
Constructors Constructor Description JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parent)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode, boolean suppressSubSchemaRetrieval)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonSchema
createChildSchema(String schemaPath, com.fasterxml.jackson.databind.JsonNode schemaNode)
ExecutionContext
createExecutionContext()
Creates an execution context.JsonSchema
findAncestor()
URI
getCurrentUri()
com.fasterxml.jackson.databind.JsonNode
getRefSchemaNode(String ref)
Find the schema node for $ref attribute.JsonValidator
getRequiredValidator()
TypeValidator
getTypeValidator()
boolean
hasRequiredValidator()
boolean
hasTypeValidator()
void
initializeValidators()
Initializes the validators'JsonSchema
instances.boolean
isDynamicAnchor()
String
toString()
Set<ValidationMessage>
validate(com.fasterxml.jackson.databind.JsonNode rootNode)
Validate 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, String at)
START OF VALIDATE METHODSValidationResult
validateAndCollect(com.fasterxml.jackson.databind.JsonNode node)
ValidationResult
validateAndCollect(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node)
ValidationResult
walk(com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)
ValidationResult
walk(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, String at, boolean shouldValidateSchema)
This is default implementation of walk method.ValidationResult
walkAtNode(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, String at, boolean shouldValidateSchema)
-
Methods inherited from class com.networknt.schema.BaseJsonValidator
atPath, atPath, atRoot, checkDiscriminatorMatch, debug, equals, fetchSubSchemaNode, getNodeFieldType, getParentSchema, getPathType, getSchemaNode, getSchemaPath, preloadJsonSchemas, registerAndMergeDiscriminator, validate
-
Methods inherited from class com.networknt.schema.ValidationMessageHandler
buildValidationMessage, constructValidationMessage, getErrorMessageType, getValidatorType, isPartOfOneOfMultipleType, parseErrorCode, 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
@Deprecated public JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
- Parameters:
validationContext
- validation contextbaseUri
- base URLschemaNode
- schema node
-
JsonSchema
@Deprecated public JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parent)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
- Parameters:
validationContext
- validation contextschemaPath
- schema pathcurrentUri
- current URIschemaNode
- schema nodeparent
- parent schema
-
JsonSchema
@Deprecated public JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode, boolean suppressSubSchemaRetrieval)
Deprecated.UseJsonSchemaFactory#create(ValidationContext, String, JsonNode, JsonSchema)
- Parameters:
validationContext
- validation contextbaseUri
- base URIschemaNode
- schema nodesuppressSubSchemaRetrieval
- suppress sub schema retrieval
-
-
Method Detail
-
createChildSchema
public JsonSchema createChildSchema(String schemaPath, com.fasterxml.jackson.databind.JsonNode schemaNode)
-
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
-
findAncestor
public JsonSchema findAncestor()
-
validate
public Set<ValidationMessage> validate(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.databind.JsonNode rootNode, String at)
START OF VALIDATE METHODS- Parameters:
executionContext
- ExecutionContextjsonNode
- JsonNoderootNode
- JsonNodeat
- String- 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.
-
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, String at, boolean shouldValidateSchema)
-
walk
public Set<ValidationMessage> walk(ExecutionContext executionContext, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, String at, boolean shouldValidateSchema)
Description copied from interface:JsonValidator
This is default implementation of walk method. Its job is to call the validate method if shouldValidateSchema is enabled.- Parameters:
executionContext
- ExecutionContextnode
- JsonNoderootNode
- JsonNodeat
- StringshouldValidateSchema
- boolean- Returns:
- a set of validation messages if shouldValidateSchema is true.
-
hasRequiredValidator
public boolean hasRequiredValidator()
-
getRequiredValidator
public JsonValidator getRequiredValidator()
-
hasTypeValidator
public boolean hasTypeValidator()
-
getTypeValidator
public TypeValidator getTypeValidator()
-
initializeValidators
public void initializeValidators()
Initializes the validators'JsonSchema
instances. For avoiding issues with concurrency, in 1.0.49 theJsonSchema
instances 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
$ref
resolution at instantiation fromJsonSchemaFactory
.By calling
initializeValidators
you can enforce preloading of theJsonSchema
instances of the validators.
-
isDynamicAnchor
public boolean isDynamicAnchor()
-
createExecutionContext
public ExecutionContext createExecutionContext()
Creates an execution context.- Returns:
- the execution context
-
-