Interface JsonSchema

  • All Known Subinterfaces:
    ObjectJsonSchema

    public interface JsonSchema
    An immutable JSON schema.

    A JSON schema can be read from InputStream or Reader. The following example shows how to read a JSON schema from a StringReader:

     
     JsonValidationService service = JsonValidationService.newInstance();
     StringReader reader = new StringReader("{\"type\": \"integer\"}");
     JsonSchema schema = service.readSchema(reader);
     
     

    Alternatively, a JSON schema can be built programmatically with JsonSchemaBuilder as follows.

     
     JsonSchemaBuilderFactory factory = service.createSchemaBuilderFactory();
     JsonSchemaBuilder builder = factory.createBuilder();
     JsonSchema schema = builder.withType(InstanceType.INTEGER).build();
     
     

    All instances of this type except TRUE and FALSE can be casted to ObjectJsonSchema.

    Any instance of this class is safe for use by multiple concurrent threads after schema reading or schema building once completed.

    Author:
    leadpony
    • Field Detail

      • TRUE

        static final JsonSchema TRUE
        The JSON schema which is always evaluated as true. Any JSON instance satisfy this schema.
      • FALSE

        static final JsonSchema FALSE
        The JSON schema which is always evaluated as false. Any JSON instance does not satisfy this schema.
      • EMPTY

        static final ObjectJsonSchema EMPTY
        The JSON schema represented by an empty JSON object. This schema is always evaluated as true.
    • Method Detail

      • hasId

        default boolean hasId()
        Checks if this schema has an identifier assigned or not.
        Returns:
        true if this schema has an identifier. false if this schema does not have an identifier.
      • hasAbsoluteId

        default boolean hasAbsoluteId()
        Checks if this schema has an identifier and the identifier is absolute.
        Returns:
        true if this schema has an absolute identifier. false if this schema does not have an absolute identifier.
        See Also:
        hasId()
      • id

        default URI id()
        Returns the identifier of this schema, which is supplied by "$id" keyword.
        Returns:
        the identifier of this schema, or null if not exists.
      • schema

        default URI schema()
        Returns the version identifier of this schema, which is supplied by "$schema" keyword.
        Returns:
        the version identifier of this schema, or null if not exists.
      • comment

        default String comment()
        Returns the comment for this schema, which is supplied by "$comment" keyword.
        Returns:
        the comment for this schema, or null if not exists.
      • title

        default String title()
        Returns the title of this schema, which is supplied by "title" keyword.
        Returns:
        the title of this schema, or null if not exists.
      • description

        default String description()
        Returns the description of this schema, which is supplied by "description" keyword.
        Returns:
        the description of this schema, or null if not exists.
      • defaultValue

        default JsonValue defaultValue()
        Returns the default value of this schema, which is supplied by "default" keyword.
        Returns:
        the default value of this schema. or null if not exists.
      • isBoolean

        boolean isBoolean()
        Checks if this schema is a boolean schema.
        Returns:
        true if this schema is a boolean schema, false otherwise.
      • asObjectJsonSchema

        default ObjectJsonSchema asObjectJsonSchema()
        Returns this JSON schema as a ObjectJsonSchema.
        Returns:
        the instance of ObjectJsonSchema.
        Throws:
        ClassCastException - if this instance is not a ObjectJsonSchema.
      • containsKeyword

        default boolean containsKeyword​(String keyword)
        Checks if this schema contains the specified keyword or not.
        Parameters:
        keyword - the name of the keyword.
        Returns:
        true if this schema contains the specified keyword, false otherwise.
        Throws:
        NullPointerException - if the specified keyword is null.
      • getKeywordValue

        default JsonValue getKeywordValue​(String keyword)
        Returns the value of the specified schema keyword as a JsonValue.
        Parameters:
        keyword - the name of the schema keyword, such as "type".
        Returns:
        keyword value if this schema contains the keyword, or null if this schema does not contain the keyword.
        Throws:
        NullPointerException - if the specified keyword is null.
      • getKeywordValue

        default JsonValue getKeywordValue​(String keyword,
                                          JsonValue defaultValue)
        Returns the value of the specified schema keyword as a JsonValue.
        Parameters:
        keyword - the name of the schema keyword, such as "type".
        defaultValue - the value to be returned if this schema does not contain the keyword. This value can be null.
        Returns:
        keyword value if this schema contains the keyword, or defaultValue if this schema does not contain the keyword.
        Throws:
        NullPointerException - if the specified keyword is null.
      • getSubschemas

        default Stream<JsonSchema> getSubschemas()
        Returns all of the subschemas contained in this schema.
        Returns:
        the stream of subschemas contained in this schema.
      • getInPlaceSubschemas

        default Stream<JsonSchema> getInPlaceSubschemas()
        Returns all of the subschemas of this schema which will be applied to the same instance location as this one.
        Returns:
        the stream of subschemas.
      • getSubschemaAt

        default JsonSchema getSubschemaAt​(String jsonPointer)
        Returns the subschema at the location specified with a JSON pointer.
        Parameters:
        jsonPointer - the valid escaped JSON Pointer string. It must be an empty string or a sequence of '/' prefixed tokens.
        Returns:
        the subschema found or null if the specified subschema does not exist.
        Throws:
        NullPointerException - if the specified jsonPointer is null.
        JsonException - if the specified jsonPointer is not a valid JSON Pointer.
      • createEvaluator

        Evaluator createEvaluator​(EvaluatorContext context,
                                  InstanceType type)
        Creates an evaluator of this schema.

        Note that this method is not intended to be used directly by end users.

        Parameters:
        context - the context where the evaluator will reside.
        type - the type of the JSON instance against which this schema will be evaluated. For integers, InstanceType.NUMBER will be passed instead of InstanceType.INTEGER.
        Returns:
        newly created evaluator. It must not be null.
        Throws:
        NullPointerException - if the specified type is null.
      • createNegatedEvaluator

        Evaluator createNegatedEvaluator​(EvaluatorContext context,
                                         InstanceType type)
        Creates an evaluator of the negated version of this schema.

        Note that this method is not intended to be used directly by end users.

        Parameters:
        context - the context where the evaluator will reside.
        type - the type of the JSON instance against which this schema will be evaluated. For integers, InstanceType.NUMBER will be passed instead of InstanceType.INTEGER.
        Returns:
        newly created evaluator. It must not be null.
        Throws:
        NullPointerException - if the specified type is null.
      • getJsonValueType

        JsonValue.ValueType getJsonValueType()
        Returns the value type of JSON representing this schema.
        Returns:
        the value type of JSON.
      • toJson

        JsonValue toJson()
        Returns the JSON representation of this schema.
        Returns:
        the JSON representation of this schema, never be null.
      • toString

        String toString()
        Returns the string representation of this schema.
        Overrides:
        toString in class Object
        Returns:
        the string representation of this schema, never be null.
        Throws:
        JsonException - if an error occurred while generating a JSON.
      • valueOf

        static JsonSchema valueOf​(boolean value)
        Returns a boolean schema instance representing the specified boolean value.
        Parameters:
        value - the boolean value.
        Returns:
        TRUE if the specified value is true, or FALSE if the specified value is false.