Class DefaultJacksonValidators
There are three kinds of validators:
requireThat()
for method preconditions.assert that()
for class invariants, and method postconditions.checkIf()
for returning multiple validation failures.
Thread Safety: This class is thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends JsonNode>
JsonNodeValidator<T> checkIf
(T value) Validates the state of aJsonNode
.static <T extends JsonNode>
JsonNodeValidator<T> Validates the state of aJsonNode
.Returns the contextual information for validators created out by this factory.static GlobalConfiguration
Returns the global configuration shared by all validators.static JacksonValidators
removeContext
(String name) Removes the contextual information of validators created by this factory.static <T extends JsonNode>
JsonNodeValidator<T> requireThat
(T value, String name) Validates the state of aJsonNode
.static <T extends JsonNode>
JsonNodeValidator<T> that
(T value) Validates the state of aJsonNode
.static <T extends JsonNode>
JsonNodeValidator<T> Validates the state of aJsonNode
.static JacksonValidators
withContext
(Object value, String name) Sets the contextual information for validators created by this factory.
-
Method Details
-
requireThat
Validates the state of aJsonNode
.The returned validator throws an exception immediately if a validation fails.
- Type Parameters:
T
- the type of theJsonNode
- Parameters:
value
- the valuename
- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException
- ifname
is nullIllegalArgumentException
- ifname
contains whitespace, or is empty
-
that
Validates the state of aJsonNode
.The returned validator captures exceptions on validation failure rather than throwing them immediately. The exceptions are converted into an
AssertionError
and can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.This method is intended to be used with the
assert
keyword, like so:assert that(value, name)
.- Type Parameters:
T
- the type of theJsonNode
- Parameters:
value
- the valuename
- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException
- ifname
is nullIllegalArgumentException
- ifname
contains whitespace or is empty
-
that
Validates the state of aJsonNode
.The returned validator captures exceptions on validation failure rather than throwing them immediately. The exceptions are converted into an
AssertionError
and can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.This method is intended to be used with the
assert
keyword, like so:assert that(value, name)
.- Type Parameters:
T
- the type of theJsonNode
- Parameters:
value
- the value- Returns:
- a validator for the value
-
checkIf
Validates the state of aJsonNode
.The returned validator captures exceptions on validation failure rather than throwing them immediately. These exceptions can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.
- Type Parameters:
T
- the type of theJsonNode
- Parameters:
value
- the valuename
- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException
- if any of the mandatory arguments are nullIllegalArgumentException
- ifname
contains whitespace, or is empty
-
checkIf
Validates the state of aJsonNode
.The returned validator captures exceptions on validation failure rather than throwing them immediately. These exceptions can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.
- Type Parameters:
T
- the type of theJsonNode
- Parameters:
value
- the value- Returns:
- a validator for the value
-
getContext
Returns the contextual information for validators created out by this factory. The contextual information is a map of key-value pairs that can provide more details about validation failures. For example, if the message is "Password may not be empty" and the map contains the key-value pair{"username": "john.smith"}
, the exception message would be:Password may not be empty username: john.smith
Note that values are wrapped in an
Optional
because modern maps do not supportnull
values.- Returns:
- an unmodifiable map from each entry's name to its value
-
withContext
Sets the contextual information for validators created by this factory.This method adds contextual information to exception messages. The contextual information is stored as key-value pairs in a map. Values set by this method may be overridden by
ValidatorComponent.withContext(Object, String)
}.- Parameters:
value
- the value of the entryname
- the name of an entry- Returns:
- the underlying validator factory
- Throws:
NullPointerException
- ifname
is nullIllegalArgumentException
- ifname
:- contains whitespace
- is empty
- is already in use by the value being validated or the validator context
-
removeContext
Removes the contextual information of validators created by this factory.- Parameters:
name
- the parameter name- Returns:
- the underlying validator factory
- Throws:
NullPointerException
- ifname
is nullIllegalArgumentException
- ifname
:- contains whitespace
- is empty
-
globalConfiguration
Returns the global configuration shared by all validators.NOTE: Updating this configuration affects existing and new validators.
- Returns:
- the global configuration updater
-