Class ValidationException

All Implemented Interfaces:
Serializable

public class ValidationException extends RuntimeException
Thrown by Schema subclasses on validation failure.
See Also:
  • Constructor Details

    • ValidationException

      @Deprecated public ValidationException(Class<?> expectedType, Object actualValue)
      Deprecated.
      Deprecated, use ValidationException(Schema, Class<?>, Object) instead.
      Parameters:
      expectedType - the expected type
      actualValue - the violating value
    • ValidationException

      public ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue)
      Constructor, creates an instance with keyword="type".
      Parameters:
      violatedSchema - the schema instance which detected the schema violation
      expectedType - the expected type
      actualValue - the violating value
    • ValidationException

      @Deprecated public ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue, String keyword)
      Deprecated.
      Constructor for type-mismatch failures. It is usually more convenient to use ValidationException(Schema, Class, Object) instead.
      Parameters:
      violatedSchema - the schema instance which detected the schema violation
      expectedType - the expected type
      actualValue - the violating value
      keyword - the violating keyword
    • ValidationException

      public ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue, String keyword, String schemaLocation)
      Constructor for type-mismatch failures. It is usually more convenient to use ValidationException(Schema, Class, Object) instead.
      Parameters:
      violatedSchema - the schema instance which detected the schema violation
      expectedType - the expected type
      actualValue - the violating value
      keyword - the violating keyword
      schemaLocation - a path denoting the location of the violated keyword in the schema JSON
    • ValidationException

      @Deprecated public ValidationException(Schema violatedSchema, String message)
      Deprecated.
      use one of the constructors which explicitly specify the violated keyword instead
      Constructor.
      Parameters:
      violatedSchema - the schema instance which detected the schema violation
      message - the readable exception message
    • ValidationException

      @Deprecated public ValidationException(Schema violatedSchema, String message, String keyword)
      Deprecated.
      Constructor.
      Parameters:
      violatedSchema - the schama instance which detected the schema violation
      message - the readable exception message
      keyword - the violated keyword
    • ValidationException

      public ValidationException(Schema violatedSchema, String message, String keyword, String schemaLocation)
      Constructor.
      Parameters:
      violatedSchema - the schama instance which detected the schema violation
      message - the readable exception message
      keyword - the violated keyword
      schemaLocation - the path to the violated schema fragment (from the schema root)
    • ValidationException

      @Deprecated public ValidationException(String message)
      Deprecated.
      Deprecated, use ValidationException(Schema, String) instead.
      Parameters:
      message - readable exception message
    • ValidationException

      @Deprecated public ValidationException(Schema violatedSchema, String message, List<ValidationException> causingExceptions)
      Deprecated.
      use one of the constructors which explicitly specify the keyword instead
      Constructor.
      Parameters:
      violatedSchema - the schema instance which detected the schema violation
      message - the readable exception message
      causingExceptions - a (possibly empty) list of validation failures. It is used if multiple schema violations are found by violatedSchema
  • Method Details

    • throwFor

      public static void throwFor(Schema rootFailingSchema, List<ValidationException> failures)
      Sort of static factory method. It is used by ObjectSchema and ArraySchema to create ValidationExceptions, handling the case of multiple violations occuring during validation.
      • If failures is empty, then it doesn't do anything
      • If failures contains 1 exception instance, then that will be thrown
      • Otherwise a new exception instance will be created, its violated schema will be rootFailingSchema, and its causing exceptions will be the failures list
      Parameters:
      rootFailingSchema - the schema which detected the failures
      failures - list containing validation failures to be thrown by this method
    • getCausingExceptions

      public List<ValidationException> getCausingExceptions()
    • getAllMessages

      public List<String> getAllMessages()
      Returns all messages collected from all violations, including nested causing exceptions.
      Returns:
      all messages
    • getMessage

      public String getMessage()
      Returns a programmer-readable error description prepended by the pointer to the violating fragment of the JSON document.
      Overrides:
      getMessage in class Throwable
      Returns:
      the error description
    • getErrorMessage

      public String getErrorMessage()
      Returns a programmer-readable error description. Unlike getMessage() this doesn't contain the JSON pointer denoting the violating document fragment.
      Returns:
      the error description
    • getPointerToViolation

      public String getPointerToViolation()
      A JSON pointer denoting the part of the document which violates the schema. It always points from the root of the document to the violating data fragment, therefore it always starts with #.
      Returns:
      the JSON pointer
    • getViolatedSchema

      public Schema getViolatedSchema()
    • prepend

      public ValidationException prepend(String fragment)
      Creates a new ViolationException instance based on this one, but with changed JSON pointer.
      Parameters:
      fragment - the fragment of the JSON pointer to be prepended to existing pointers
      Returns:
      the new instance
    • prepend

      public ValidationException prepend(String fragment, Schema violatedSchema)
      Creates a new ViolationException instance based on this one, but with changed JSON pointer and {link violated schema.
      Parameters:
      fragment - the fragment of the JSON pointer to be prepended to existing pointers
      violatedSchema - the violated schema, which may not be the same as getViolatedSchema()
      Returns:
      the new ViolationException instance
    • getViolationCount

      public int getViolationCount()
    • getKeyword

      public String getKeyword()
    • toJSON

      public org.json.JSONObject toJSON()
      Creates a JSON representation of the failure.

      The returned JSONObject contains the following keys:

      • "message": a programmer-friendly exception message. This value is a non-nullable string.
      • "keyword": a JSON Schema keyword which was used in the schema and violated by the input JSON. This value is a nullable string.
      • "pointerToViolation": a JSON Pointer denoting the path from the root of the document to the invalid fragment of it. This value is a non-nullable string. See getPointerToViolation()
      • "causingExceptions": is a (possibly empty) array of violations which caused this exception. See getCausingExceptions()
      • "schemaLocation": a string denoting the path to the violated schema keyword in the schema JSON (since version 1.6.0)
      Returns:
      a JSON description of the validation error
    • getSchemaLocation

      public String getSchemaLocation()
      Returns:
      a path denoting the location of the violated keyword in the schema
      Since:
      1.6.0
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object