Interface ValidatorComponent<S,T>
- Type Parameters:
S
- the type of validatorT
- the type of the value that is being validated
- All Known Subinterfaces:
BigDecimalValidator
,BigIntegerValidator
,BooleanValidator
,ByteArrayValidator
,ByteValidator
,CharacterValidator
,CollectionValidator<T,
,E> ComparableValidator<T>
,DoubleValidator
,FloatValidator
,GenericTypeValidator<T>
,InetAddressValidator
,IntegerValidator
,JsonNodeValidator<T>
,ListValidator<T,
,E> LongValidator
,MapValidator<T,
,K, V> MultimapValidator<T,
,K, V> ObjectArrayValidator<T,
,E> ObjectValidator<T>
,OptionalValidator<T>
,PathValidator
,PrimitiveBooleanArrayValidator
,PrimitiveBooleanValidator
,PrimitiveByteArrayValidator
,PrimitiveByteValidator
,PrimitiveCharacterArrayValidator
,PrimitiveCharacterValidator
,PrimitiveDoubleArrayValidator
,PrimitiveDoubleValidator
,PrimitiveFloatArrayValidator
,PrimitiveFloatValidator
,PrimitiveIntegerArrayValidator
,PrimitiveIntegerValidator
,PrimitiveLongArrayValidator
,PrimitiveLongValidator
,PrimitiveShortArrayValidator
,PrimitiveShortValidator
,PrimitiveUnsignedIntegerValidator
,ShortValidator
,StringValidator
,UnsignedIntegerValidator
,UriValidator
public interface ValidatorComponent<S,T>
Methods that all validators must contain.
-
Method Summary
Modifier and TypeMethodDescriptionFacilitates the validation of related properties.Returns the list of failed validations.boolean
Throws an exception if a validation failed; otherwise, returnstrue
.Returns the contextual information for upcoming validations carried out by this validator.Returns the contextual information associated with this validator.getName()
Returns the name of the value.getValueOrDefault
(T defaultValue) Returns the value that is being validated.boolean
Checks if any validation has failed.withContext
(Object value, String name) Sets the contextual information for upcoming validations.
-
Method Details
-
getName
String getName()Returns the name of the value.- Returns:
- the name of the value
-
getValueOrDefault
Returns the value that is being validated.- Parameters:
defaultValue
- the fallback value to use if the value is invalid- Returns:
- the value, or
defaultValue
if the value is invalid (e.g. due to dereferencing a property of anull
object)
-
getContext
Returns the contextual information for upcoming validations carried out by this validator. 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
- See Also:
-
withContext
Sets the contextual information for upcoming validations.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 override any values that are set using
Validators.withContext(Object, String)
.There is no way to remove contextual information from a validator. Thread-level contextual information is removed automatically.
- Parameters:
value
- the value of the entryname
- the name of an entry- Returns:
- this
- Throws:
NullPointerException
- ifname
isnull
IllegalArgumentException
- ifname
:- contains whitespace
- is empty
- is already in use by the value being validated or the validator context
-
and
Facilitates the validation of related properties. For example:requireThat(nameToFrequency, "nameToFrequency"). and(m -> m.size().isPositive()). and(m -> m.keySet().contains("John"));
Any changes made during the validation process will impact this validator.
- Parameters:
validation
- the nested validation- Returns:
- this
- Throws:
NullPointerException
- ifvalidation
isnull
-
validationFailed
boolean validationFailed()Checks if any validation has failed.- Returns:
true
if at least one validation has failed
-
elseGetFailures
ValidationFailures elseGetFailures()Returns the list of failed validations.- Returns:
- an unmodifiable list of failed validations
-
elseThrow
boolean elseThrow()Throws an exception if a validation failed; otherwise, returnstrue
.- Returns:
- true if the validation passed
- Throws:
RuntimeException
- if a method precondition was violatedError
- if a class invariant or method postcondition was violatedMultipleFailuresException
- if more than one validation failed. This exception contains a list of the failures.
-
getContextAsString
String getContextAsString()Returns the contextual information associated with this validator.- Returns:
- the contextual information associated with this validator
-