- Type Parameters:
V- type of the value tovalidate.
- All Superinterfaces:
Composable<Validator<?>>,Iterable<Validator<?>>
- All Known Implementing Classes:
AbstractComposedValidator,AbstractValidator,AbstractValueValidator,ComposedValidator,ProjectionValidator
A
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 of
ATTENTION:
The
Validator allows to validate 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 of
exceptions 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 all failures so the end-user can see and
fix all problems at once.ATTENTION:
The
null values is typically only handled by mandatory validator. Other validators
will treat null 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 accept null as
valid. So in such case you most probably want to combine it with the mandatory validator.- Since:
- 1.0.0
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault <T> Validator<T> default <T> Validator<T> default booleancontainsId(String id) default <T> TgetId()default <T extends Comparable<?>>
TgetMax()default <T extends Comparable<?>>
TgetMin()default <T extends Comparable<?>>
Range<T> getRange()default booleanstatic booleanisValidating(Validator<?> validator) static <T> Validator<T> none()default ValidationResultThis method validates the givenvalue.This method validates the givenvalue.Methods inherited from interface io.github.mmm.base.lang.Composable
getChild, getChildCount, iteratorMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
ID_MANDATORY
- See Also:
-
-
Method Details
-
validate
This method validates the givenvalue.- Parameters:
value- is the value to validate.- Returns:
- the
ValidationResultornullif the givenvalueis valid according to thisValidator.
-
validate
This method validates the givenvalue.- Parameters:
value- is the value to validate.valueSource- is thesourcedescribing the origin of the givenvalue. The source needs to have a reasonablestring-representationas 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:
-
isMandatory
default boolean isMandatory()- Returns:
trueif this is a validator for mandatory fields (that will not acceptnullor empty values),falseotherwise.
-
containsId
-
getChild
-
getRange
- Type Parameters:
T- type of theRangevalue. Typically of type <V> but e.g. forStringorCollectionthe type would beIntegerto validate the size of the actual value.- Returns:
- the
Rangeconstraint with an optionalmimimumand/ormaximumvalue. Will beRange.unbounded()by if no bounds are validated.
-
getMin
- Type Parameters:
T- type of the minimum value. Typically of type <V> but e.g. forStringorCollectionthe type would beIntegerto validate the size of the actual value.- Returns:
- the minimum allowed value.
-
getMax
- Type Parameters:
T- type of the minimum value. Typically of type <V> but e.g. forStringorCollectionthe type would beIntegerto validate the size of the actual value.- Returns:
- the maximum allowed value.
-
append
-
append
-
none
- Type Parameters:
T- type of the value tovalidate.- Returns:
- an instance of
Validatorthat always returnValidationResultValid(accepts any value as valid input).
-
isValidating
-