Package org.everit.json.schema
Class ValidationException
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- java.lang.RuntimeException
-
- org.everit.json.schema.ValidationException
-
- All Implemented Interfaces:
Serializable
public class ValidationException extends RuntimeException
Thrown bySchema
subclasses on validation failure.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ValidationException(Class<?> expectedType, Object actualValue)
Deprecated.ValidationException(String message)
Deprecated.ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue)
Constructor, creates an instance withkeyword="type"
.ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue, String keyword)
Deprecated.ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue, String keyword, String schemaLocation)
Constructor for type-mismatch failures.ValidationException(Schema violatedSchema, String message)
Deprecated.use one of the constructors which explicitly specify the violated keyword insteadValidationException(Schema violatedSchema, String message, String keyword)
Deprecated.ValidationException(Schema violatedSchema, String message, String keyword, String schemaLocation)
Constructor.ValidationException(Schema violatedSchema, String message, List<ValidationException> causingExceptions)
Deprecated.use one of the constructors which explicitly specify the keyword instead
-
Method Summary
Modifier and Type Method Description boolean
equals(Object o)
List<String>
getAllMessages()
Returns all messages collected from all violations, including nested causing exceptions.List<ValidationException>
getCausingExceptions()
String
getErrorMessage()
Returns a programmer-readable error description.String
getKeyword()
String
getMessage()
Returns a programmer-readable error description prepended bythe pointer to the violating fragment
of the JSON document.String
getPointerToViolation()
A JSON pointer denoting the part of the document which violates the schema.String
getSchemaLocation()
Schema
getViolatedSchema()
int
getViolationCount()
int
hashCode()
ValidationException
prepend(String fragment)
Creates a newViolationException
instance based on this one, but with changedJSON pointer
.ValidationException
prepend(String fragment, Schema violatedSchema)
Creates a newViolationException
instance based on this one, but with changedJSON pointer
and {linkviolated schema
.static void
throwFor(Schema rootFailingSchema, List<ValidationException> failures)
Sort of static factory method.org.json.JSONObject
toJSON()
Creates a JSON representation of the failure.-
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
-
-
-
Constructor Detail
-
ValidationException
@Deprecated public ValidationException(Class<?> expectedType, Object actualValue)
Deprecated.Deprecated, useValidationException(Schema, Class<?>, Object)
instead.- Parameters:
expectedType
- the expected typeactualValue
- the violating value
-
ValidationException
public ValidationException(Schema violatedSchema, Class<?> expectedType, Object actualValue)
Constructor, creates an instance withkeyword="type"
.- Parameters:
violatedSchema
- the schema instance which detected the schema violationexpectedType
- the expected typeactualValue
- 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 useValidationException(Schema, Class, Object)
instead.- Parameters:
violatedSchema
- the schema instance which detected the schema violationexpectedType
- the expected typeactualValue
- the violating valuekeyword
- 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 useValidationException(Schema, Class, Object)
instead.- Parameters:
violatedSchema
- the schema instance which detected the schema violationexpectedType
- the expected typeactualValue
- the violating valuekeyword
- the violating keywordschemaLocation
- 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 insteadConstructor.- Parameters:
violatedSchema
- the schema instance which detected the schema violationmessage
- 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 violationmessage
- the readable exception messagekeyword
- the violated keyword
-
ValidationException
public ValidationException(Schema violatedSchema, String message, String keyword, String schemaLocation)
Constructor.- Parameters:
violatedSchema
- the schama instance which detected the schema violationmessage
- the readable exception messagekeyword
- the violated keywordschemaLocation
- the path to the violated schema fragment (from the schema root)
-
ValidationException
@Deprecated public ValidationException(String message)
Deprecated.Deprecated, useValidationException(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 insteadConstructor.- Parameters:
violatedSchema
- the schema instance which detected the schema violationmessage
- the readable exception messagecausingExceptions
- a (possibly empty) list of validation failures. It is used if multiple schema violations are found by violatedSchema
-
-
Method Detail
-
throwFor
public static void throwFor(Schema rootFailingSchema, List<ValidationException> failures)
Sort of static factory method. It is used byObjectSchema
andArraySchema
to createValidationException
s, 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 berootFailingSchema
, and itscausing exceptions
will be thefailures
list
- Parameters:
rootFailingSchema
- the schema which detected thefailures
failures
- list containing validation failures to be thrown by this method
- If
-
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 bythe pointer to the violating fragment
of the JSON document.- Overrides:
getMessage
in classThrowable
- Returns:
- the error description
-
getErrorMessage
public String getErrorMessage()
Returns a programmer-readable error description. UnlikegetMessage()
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 newViolationException
instance based on this one, but with changedJSON 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 newViolationException
instance based on this one, but with changedJSON pointer
and {linkviolated schema
.- Parameters:
fragment
- the fragment of the JSON pointer to be prepended to existing pointersviolatedSchema
- the violated schema, which may not be the same asgetViolatedSchema()
- 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. SeegetPointerToViolation()
"causingExceptions"
: is a (possibly empty) array of violations which caused this exception. SeegetCausingExceptions()
"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
-
-