-
- 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<?>>
AValidatorallows tovalidateaccording 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 ofexceptionsas 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 allfailuresso the end-user can see and fix all problems at once.
ATTENTION:
Thenullvalues is typically only handled bymandatory validator. Other validators will treatnullas 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 acceptnullas valid. So in such case you most probably want tocombineit with themandatory validator.- Since:
- 1.0.0
-
-
Field Summary
Fields Modifier and Type Field Description static StringID_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 booleancontainsId(String id)StringgetId()default ObjectgetMax()default ObjectgetMin()default booleanisMandatory()static booleanisValidating(Validator<?> validator)static <T> Validator<T>none()default ValidationResultvalidate(V value)This method validates the givenvalue.ValidationResultvalidate(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
ValidationResultornullif the givenvalueis 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 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:
ValidationResult.getCode()
-
isMandatory
default boolean isMandatory()
- Returns:
trueif this is a validator for mandatory fields (that will not acceptnullor empty values),falseotherwise.
-
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
Validatorthat always returnValidationResultValid(accepts any value as valid input).
-
-