PredicateValidator<T extends Object> class
Builds a validator from a plain predicate — a one-off check without declaring a new class.
final finite = PredicateValidator<num>(
(v) => v.isFinite,
error: 'Must be a finite number',
);
finite(1.5); // null
finite(double.nan); // 'Must be a finite number'
finite(null); // null — empty input is skipped by default
Like every non-text validator it treats only null as empty, so a
PredicateValidator<String> does run its predicate on blank strings.
Reach for the text validators (or check blankness in the predicate) when
blank input should be skipped.
- Inheritance
- Available extensions
Constructors
- PredicateValidator(bool predicate(T value), {required String error, bool ignoreEmptyValues = true})
-
Constructs a validator that reports
errorwhenpredicatereturnsfalse.const
Properties
- error → String
-
The error message returned when validation fails.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- ignoreEmptyValues → bool
-
Whether empty input (see isEmpty) short-circuits to "valid".
finalinherited
- predicate → bool Function(T value)
-
Decides whether a non-empty value is valid.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
T? value) → String? -
Validates
value: returnsnullwhen it is acceptable, error when it is not.inherited -
isEmpty(
T value) → bool -
Whether a non-null
valuecounts as "empty" for this value type.inherited -
isValid(
T value) → bool -
Checks whether a non-empty
valuesatisfies the validation criteria.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator &(
Validator< T> other) → ValidatorGroup<T> -
Available on Validator<
Combines this validator withT> , provided by the ValidatorComposition extensionother; the result reports the first error. -
operator ==(
Object other) → bool -
The equality operator.
inherited