Package com.networknt.schema
Class JsonSchema
- java.lang.Object
-
- 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, failFast, parentSchema, schemaNode, schemaPath, validationContext
-
Fields inherited from interface com.networknt.schema.JsonValidator
AT_ROOT
-
-
Constructor Summary
Constructors Constructor Description JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parent)JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode)JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode, boolean suppressSubSchemaRetrieval)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonSchemafindAncestor()CollectorContextgetCollectorContext()URIgetCurrentUri()com.fasterxml.jackson.databind.JsonNodegetRefSchemaNode(String ref)Find the schema node for $ref attribute.JsonValidatorgetRequiredValidator()Map<String,JsonValidator>getValidators()booleanhasRequiredValidator()voidinitializeValidators()Initializes the validators'JsonSchemainstances.StringtoString()Set<ValidationMessage>validate(com.fasterxml.jackson.databind.JsonNode node)START OF VALIDATE METHODSSet<ValidationMessage>validate(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.databind.JsonNode rootNode, String at)Validate the given JsonNode, the given node is the child node of the root node at given data path.ValidationResultvalidateAndCollect(com.fasterxml.jackson.databind.JsonNode node)ValidationResultwalk(com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)Walk the JSON nodeSet<ValidationMessage>walk(com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, String at, boolean shouldValidateSchema)This is default implementation of walk method.-
Methods inherited from class com.networknt.schema.BaseJsonValidator
buildValidationMessage, checkDiscriminatorMatch, debug, equals, fetchSubSchemaNode, getNodeFieldType, getParentSchema, getSchemaNode, getSchemaPath, getValidatorType, greaterThan, isPartOfOneOfMultipleType, lessThan, parseErrorCode, preloadJsonSchemas, registerAndMergeDiscriminator
-
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
public JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode)
-
JsonSchema
public JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, com.fasterxml.jackson.databind.JsonNode schemaNode, JsonSchema parent)
-
JsonSchema
public JsonSchema(ValidationContext validationContext, URI baseUri, com.fasterxml.jackson.databind.JsonNode schemaNode, boolean suppressSubSchemaRetrieval)
-
-
Method Detail
-
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(com.fasterxml.jackson.databind.JsonNode node)
START OF VALIDATE METHODS- Specified by:
validatein interfaceJsonValidator- Overrides:
validatein classBaseJsonValidator- Parameters:
node- JsonNode- 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 jsonNode, com.fasterxml.jackson.databind.JsonNode rootNode, String at)
Description copied from interface:JsonValidatorValidate the given JsonNode, the given node is the child node of the root node at given data path.- Parameters:
jsonNode- JsonNoderootNode- JsonNodeat- String- Returns:
- A list of ValidationMessage if there is any validation error, or an empty list if there is no error.
-
validateAndCollect
public ValidationResult validateAndCollect(com.fasterxml.jackson.databind.JsonNode node)
-
walk
public ValidationResult walk(com.fasterxml.jackson.databind.JsonNode node, boolean shouldValidateSchema)
Walk the JSON node- Parameters:
node- JsonNodeshouldValidateSchema- indicator on validation- Returns:
- result of ValidationResult
-
walk
public Set<ValidationMessage> walk(com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.JsonNode rootNode, String at, boolean shouldValidateSchema)
Description copied from class:BaseJsonValidatorThis is default implementation of walk method. Its job is to call the validate method if shouldValidateSchema is enabled.- Specified by:
walkin interfaceJsonSchemaWalker- Overrides:
walkin classBaseJsonValidator- Parameters:
node- JsonNoderootNode- JsonNodeat- StringshouldValidateSchema- boolean- Returns:
- a set of validation messages if shouldValidateSchema is true.
-
getCollectorContext
public CollectorContext getCollectorContext()
-
hasRequiredValidator
public boolean hasRequiredValidator()
-
getRequiredValidator
public JsonValidator getRequiredValidator()
-
getValidators
public Map<String,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.
-
-