-
- Type Parameters:
V
- type of the value tovalidate
.
- All Superinterfaces:
Composable<Validator<?>>
,Iterable<Validator<?>>
- All Known Implementing Classes:
AbstractComposedValidator
,AbstractValidator
,AbstractValidatorRange
,AbstractValueValidator
,ComposedValidator
,ProjectionValidator
,ValidatorCharSequnceSize
,ValidatorCollectionElements
,ValidatorCollectionSize
,ValidatorCompare
,ValidatorEmail
,ValidatorEmailSpec
,ValidatorHost
,ValidatorHostName
,ValidatorInstantAfter
,ValidatorInstantBefore
,ValidatorInstantFuture
,ValidatorInstantPast
,ValidatorIpAddress
,ValidatorIpV4Address
,ValidatorIpV6Address
,ValidatorLocalDateAfter
,ValidatorLocalDateBefore
,ValidatorLocalDateFuture
,ValidatorLocalDatePast
,ValidatorLocalDateTimeAfter
,ValidatorLocalDateTimeBefore
,ValidatorLocalDateTimeFuture
,ValidatorLocalDateTimePast
,ValidatorLocalTimeAfter
,ValidatorLocalTimeBefore
,ValidatorLocalTimeFuture
,ValidatorLocalTimePast
,ValidatorMandatory
,ValidatorMapKeys
,ValidatorMapSize
,ValidatorMapValues
,ValidatorOffsetDateTimeAfter
,ValidatorOffsetDateTimeBefore
,ValidatorOffsetDateTimeFuture
,ValidatorOffsetDateTimePast
,ValidatorOffsetTimeAfter
,ValidatorOffsetTimeBefore
,ValidatorOffsetTimeFuture
,ValidatorOffsetTimePast
,ValidatorPasswordConfirmation
,ValidatorPattern
,ValidatorPhoneNumber
,ValidatorRange
,ValidatorTemporalAfter
,ValidatorTemporalBefore
,ValidatorTemporalFuture
,ValidatorTemporalPast
,ValidatorZonedDateTimeAfter
,ValidatorZonedDateTimeBefore
,ValidatorZonedDateTimeFuture
,ValidatorZonedDateTimePast
public interface Validator<V> extends Composable<Validator<?>>
AValidator
allows tovalidate
according values.
There can be arbitrary implementations of this interface. A regular implementation shall be stateless and therefore thread-safe. All parameterization shall therefore happen on initialization - ideally at construction.
NOTE:
This API intentionally does NOT make use ofexceptions
as they are more expensive to produce and shall only occur in exceptional situations, while a validation failure is a regular use-case. Further, a validation shall validate entire objects to the end collecting allfailures
so the end-user can see and fix all problems at once.
ATTENTION:
Thenull
values is typically only handled bymandatory validator
. Other validators will treatnull
as a valid value. This design gives the best flexibility as it allows to define specific constraints also for optional values. However, you need to be aware this fact to avoid mistakes. So e.g. adding a validator requiring that the minimum size/length of a value needs to be e.g. 2 will still acceptnull
as valid. So in such case you most probably want tocombine
it with themandatory validator
.- Since:
- 1.0.0
-
-
Field Summary
Fields Modifier and Type Field Description static String
ID_MANDATORY
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T> Validator<T>
append(Validator<?> validator)
default <T> Validator<T>
append(Validator<?>... validators)
default boolean
containsId(String id)
String
getId()
default Object
getMax()
default Object
getMin()
default boolean
isMandatory()
static boolean
isValidating(Validator<?> validator)
static <T> Validator<T>
none()
default ValidationResult
validate(V value)
This method validates the givenvalue
.ValidationResult
validate(V value, Object valueSource)
This method validates the givenvalue
.-
Methods inherited from interface io.github.mmm.base.lang.Composable
getChild, getChildCount, iterator
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
ID_MANDATORY
static final String ID_MANDATORY
- See Also:
isMandatory()
,getId()
, Constant Field Values
-
-
Method Detail
-
validate
default ValidationResult validate(V value)
This method validates the givenvalue
.- Parameters:
value
- is the value to validate.- Returns:
- the
ValidationResult
ornull
if the givenvalue
is valid according to thisValidator
.
-
validate
ValidationResult validate(V value, Object valueSource)
This method validates the givenvalue
.- Parameters:
value
- is the value to validate.valueSource
- is thesource
describing the origin of the givenvalue
. The source needs to have a reasonablestring-representation
as this may be displayed to the end-user to locate the source of the failure. In most cases it is suitable to directly pass aString
.- Returns:
- the
ValidationResult
.
-
getId
String getId()
- Returns:
- the identifier of this
Validator
. - See Also:
ValidationResult.getCode()
-
isMandatory
default boolean isMandatory()
- Returns:
true
if this is a validator for mandatory fields (that will not acceptnull
or empty values),false
otherwise.
-
containsId
default boolean containsId(String id)
-
getMin
default Object getMin()
- Returns:
- the minimum allowed value. Typically of type <V> but this can not be guaranteed.
-
getMax
default Object getMax()
- Returns:
- the maximum allowed value. Typically of type <V> but this can not be guaranteed.
-
none
static <T> Validator<T> none()
- Type Parameters:
T
- type of the value tovalidate
.- Returns:
- an instance of
Validator
that always returnValidationResultValid
(accepts any value as valid input).
-
-