Class JsonSchema

  • All Implemented Interfaces:
    JsonValidator, JsonSchemaWalker

    public class JsonSchema
    extends BaseJsonValidator
    Used for creating a schema with validators for validating inputs. This is created using JsonSchemaFactory.getInstance(VersionFlag, Consumer) and should be cached for performance.

    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.

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

    • Constructor Detail

      • JsonSchema

        protected JsonSchema​(List<JsonValidator> validators,
                             boolean validatorsLoaded,
                             boolean recursiveAnchor,
                             TypeValidator typeValidator,
                             String id,
                             boolean suppressSubSchemaRetrieval,
                             com.fasterxml.jackson.databind.JsonNode schemaNode,
                             ValidationContext validationContext,
                             ErrorMessageType errorMessageType,
                             String errorMessageKeyword,
                             MessageSource messageSource,
                             Keyword keyword,
                             JsonSchema parentSchema,
                             SchemaLocation schemaLocation,
                             JsonNodePath evaluationPath,
                             JsonSchema evaluationParentSchema,
                             Map<String,​String> errorMessage)
        Constructor to create a copy using fields.
        Parameters:
        validators - the validators
        validatorsLoaded - whether the validators are preloaded
        recursiveAnchor - whether this is has a recursive anchor
        typeValidator - the type validator
        id - the id
        suppressSubSchemaRetrieval - to suppress sub schema retrieval
        schemaNode - the schema node
        validationContext - the validation context
        errorMessageType - the error message type
        errorMessageKeyword - the error message keyword
        messageSource - the message source
        keyword - the keyword
        parentSchema - the parent schema
        schemaLocation - the schema location
        evaluationPath - the evaluation path
        evaluationParentSchema - the evaluation parent schema
        errorMessage - the error message
    • 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 ref
        refEvaluationPath - the ref evaluation path
        Returns:
        the schema
      • getRefSchemaNode

        public com.fasterxml.jackson.databind.JsonNode getRefSchemaNode​(String ref)
        Find the schema node for $ref attribute.
        Parameters:
        ref - String
        Returns:
        JsonNode
      • getSubSchema

        public JsonSchema getSubSchema​(JsonNodePath fragment)
        Gets the sub schema given the json pointer fragment.
        Parameters:
        fragment - the json pointer fragment
        Returns:
        the schema
      • getNode

        protected com.fasterxml.jackson.databind.JsonNode getNode​(Object propertyOrIndex)
      • getNode

        protected com.fasterxml.jackson.databind.JsonNode getNode​(com.fasterxml.jackson.databind.JsonNode node,
                                                                  Object propertyOrIndex)
      • 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()
      • 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 - ExecutionContext
        jsonNode - JsonNode
        rootNode - JsonNode
        instanceLocation - 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.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Parameters:
        rootNode - the root node
        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,
                                               ExecutionContextCustomizer executionCustomizer)
        Validate the given root JsonNode, starting at the root of the data path.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Parameters:
        rootNode - the root node
        executionCustomizer - the execution customizer
        Returns:
        the assertions
      • validate

        public Set<ValidationMessage> validate​(com.fasterxml.jackson.databind.JsonNode rootNode,
                                               Consumer<ExecutionContext> executionCustomizer)
        Validate the given root JsonNode, starting at the root of the data path.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Parameters:
        rootNode - the root node
        executionCustomizer - the execution customizer
        Returns:
        the assertions
      • 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.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        rootNode - the root node
        format - the formatter
        Returns:
        the result
      • validate

        public <T> T validate​(com.fasterxml.jackson.databind.JsonNode rootNode,
                              OutputFormat<T> format,
                              ExecutionContextCustomizer executionCustomizer)
        Validates the given root JsonNode, starting at the root of the data path. The output will be formatted using the formatter specified.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        rootNode - the root node
        format - the formatter
        executionCustomizer - the execution customizer
        Returns:
        the result
      • validate

        public <T> T validate​(com.fasterxml.jackson.databind.JsonNode rootNode,
                              OutputFormat<T> format,
                              Consumer<ExecutionContext> executionCustomizer)
        Validates the given root JsonNode, starting at the root of the data path. The output will be formatted using the formatter specified.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        rootNode - the root node
        format - the formatter
        executionCustomizer - the execution customizer
        Returns:
        the result
      • validate

        public Set<ValidationMessage> validate​(String input,
                                               InputFormat inputFormat)
        Validate the given input string using the input format, starting at the root of the data path.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Parameters:
        input - the input
        inputFormat - the inputFormat
        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​(String input,
                              InputFormat inputFormat,
                              OutputFormat<T> format)
        Validates the given input string using the input format, starting at the root of the data path. The output will be formatted using the formatter specified.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        input - the input
        inputFormat - the inputFormat
        format - the formatter
        Returns:
        the result
      • validate

        public <T> T validate​(String input,
                              InputFormat inputFormat,
                              OutputFormat<T> format,
                              ExecutionContextCustomizer executionCustomizer)
        Validates the given input string using the input format, starting at the root of the data path. The output will be formatted using the formatter specified.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        input - the input
        inputFormat - the inputFormat
        format - the formatter
        executionCustomizer - the execution customizer
        Returns:
        the result
      • validate

        public <T> T validate​(String input,
                              InputFormat inputFormat,
                              OutputFormat<T> format,
                              Consumer<ExecutionContext> executionCustomizer)
        Validates the given input string using the input format, starting at the root of the data path. The output will be formatted using the formatter specified.

        Note that since Draft 2019-09 by default format generates only annotations and not assertions.

        Use ExecutionConfig.setFormatAssertionsEnabled(Boolean) to override the default.

        Type Parameters:
        T - the result type
        Parameters:
        input - the input
        inputFormat - the inputFormat
        format - the formatter
        executionCustomizer - the execution customizer
        Returns:
        the result
      • validate

        public <T> T validate​(ExecutionContext executionContext,
                              com.fasterxml.jackson.databind.JsonNode node,
                              OutputFormat<T> format)
        Validates to a format.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        node - the node
        format - the format
        Returns:
        the result
      • validate

        public <T> T validate​(ExecutionContext executionContext,
                              com.fasterxml.jackson.databind.JsonNode node,
                              OutputFormat<T> format,
                              ExecutionContextCustomizer executionCustomizer)
        Validates to a format.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        node - the node
        format - the format
        executionCustomizer - the customizer
        Returns:
        the result
      • validateAndCollect

        @Deprecated
        public ValidationResult validateAndCollect​(ExecutionContext executionContext,
                                                   com.fasterxml.jackson.databind.JsonNode node)
        Deprecated.
        Deprecated. Initialize the CollectorContext externally and call loadCollectors when done.

        Parameters:
        executionContext - ExecutionContext
        node - JsonNode
        Returns:
        ValidationResult
      • validateAndCollect

        @Deprecated
        public ValidationResult validateAndCollect​(com.fasterxml.jackson.databind.JsonNode node)
        Deprecated.
        Deprecated. Initialize the CollectorContext externally and call loadCollectors when done.
        Parameters:
        node - JsonNode
        Returns:
        ValidationResult
      • walk

        public ValidationResult walk​(ExecutionContext executionContext,
                                     com.fasterxml.jackson.databind.JsonNode node,
                                     boolean validate,
                                     ExecutionContextCustomizer executionCustomizer)
        Walk the JSON node.
        Parameters:
        executionContext - the execution context
        node - the input
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public <T> T walk​(ExecutionContext executionContext,
                          com.fasterxml.jackson.databind.JsonNode node,
                          OutputFormat<T> outputFormat,
                          boolean validate,
                          ExecutionContextCustomizer executionCustomizer)
        Walk the JSON node.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        node - the input
        outputFormat - the output format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(ExecutionContext executionContext,
                                     com.fasterxml.jackson.databind.JsonNode node,
                                     boolean validate,
                                     Consumer<ExecutionContext> executionCustomizer)
        Walk the JSON node.
        Parameters:
        executionContext - the execution context
        node - the input
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public <T> T walk​(ExecutionContext executionContext,
                          com.fasterxml.jackson.databind.JsonNode node,
                          OutputFormat<T> outputFormat,
                          boolean validate,
                          Consumer<ExecutionContext> executionCustomizer)
        Walk the JSON node.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        node - the input
        outputFormat - the output format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(ExecutionContext executionContext,
                                     com.fasterxml.jackson.databind.JsonNode node,
                                     boolean validate)
        Walk the JSON node.
        Parameters:
        executionContext - the execution context
        node - the input
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(ExecutionContext executionContext,
                                     String input,
                                     InputFormat inputFormat,
                                     boolean validate)
        Walk the input.
        Parameters:
        executionContext - the execution context
        input - the input
        inputFormat - the input format
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • walk

        public <T> T walk​(ExecutionContext executionContext,
                          String input,
                          InputFormat inputFormat,
                          OutputFormat<T> outputFormat,
                          boolean validate)
        Walk the input.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        input - the input
        inputFormat - the input format
        outputFormat - the output format
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(ExecutionContext executionContext,
                                     String input,
                                     InputFormat inputFormat,
                                     boolean validate,
                                     ExecutionContextCustomizer executionCustomizer)
        Walk the input.
        Parameters:
        executionContext - the execution context
        input - the input
        inputFormat - the input format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public <T> T walk​(ExecutionContext executionContext,
                          String input,
                          InputFormat inputFormat,
                          OutputFormat<T> outputFormat,
                          boolean validate,
                          ExecutionContextCustomizer executionCustomizer)
        Walk the input.
        Type Parameters:
        T - the result type
        Parameters:
        executionContext - the execution context
        input - the input
        inputFormat - the input format
        outputFormat - the output format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(com.fasterxml.jackson.databind.JsonNode node,
                                     boolean validate)
        Walk the JSON node.
        Parameters:
        node - the input
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • walk

        public <T> T walk​(com.fasterxml.jackson.databind.JsonNode node,
                          OutputFormat<T> outputFormat,
                          boolean validate)
        Walk the JSON node.
        Type Parameters:
        T - the result type
        Parameters:
        node - the input
        validate - true to validate the input against the schema
        outputFormat - the output format
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(String input,
                                     InputFormat inputFormat,
                                     boolean validate)
        Walk the input.
        Parameters:
        input - the input
        inputFormat - the input format
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(String input,
                                     InputFormat inputFormat,
                                     boolean validate,
                                     ExecutionContextCustomizer executionCustomizer)
        Walk the input.
        Parameters:
        input - the input
        inputFormat - the input format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walk

        public ValidationResult walk​(String input,
                                     InputFormat inputFormat,
                                     boolean validate,
                                     Consumer<ExecutionContext> executionCustomizer)
        Walk the input.
        Parameters:
        input - the input
        inputFormat - the input format
        validate - true to validate the input against the schema
        executionCustomizer - the customizer
        Returns:
        the validation result
      • walkAtNode

        public ValidationResult walkAtNode​(ExecutionContext executionContext,
                                           com.fasterxml.jackson.databind.JsonNode node,
                                           com.fasterxml.jackson.databind.JsonNode rootNode,
                                           JsonNodePath instanceLocation,
                                           boolean validate)
        Walk at the node.
        Parameters:
        executionContext - the execution content
        node - the current node
        rootNode - the root node
        instanceLocation - the instance location
        validate - true to validate the input against the schema
        Returns:
        the validation result
      • 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: JsonValidator
        This is default implementation of walk method. Its job is to call the validate method if shouldValidateSchema is enabled.
        Parameters:
        executionContext - ExecutionContext
        node - JsonNode
        rootNode - JsonNode
        instanceLocation - JsonNodePath
        shouldValidateSchema - boolean
        Returns:
        a set of validation messages if shouldValidateSchema is true.
      • hasTypeValidator

        public boolean hasTypeValidator()
      • initializeValidators

        public void initializeValidators()
        Initializes the validators' JsonSchema instances. For avoiding issues with concurrency, in 1.0.49 the JsonSchema 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 from JsonSchemaFactory.

        By calling initializeValidators you can enforce preloading of the JsonSchema instances of the validators.

      • isRecursiveAnchor

        public boolean isRecursiveAnchor()
      • createExecutionContext

        public ExecutionContext createExecutionContext()
        Creates an execution context.
        Returns:
        the execution context