Validated<T extends Object?>.check constructor
Validates a value against a predicate and returns the result.
This is the primary factory method for starting validation chains.
Evaluates predicate on value and returns either a Validated.valid
or Validated.invalid result. For chaining additional validations,
use the ValidatedOps.check extension method on the returned result.
Parameters:
value: The value to validatepredicate: Function that returns true if valid, false if invaliderror: Error message to include if validation fails
Example:
// Single validation
final result = Validated.check(
userInput,
(input) => input.isNotEmpty,
error: 'Name cannot be empty',
);
// Chaining multiple validations
final validation = Validated.check(
email,
(e) => e.isNotEmpty,
error: 'Required',
)
.check((e) => e.contains('@'), error: 'Must contain @')
.check((e) => !e.endsWith('.'), error: 'Cannot end with period');
Implementation
factory Validated.check(
T value,
bool Function(T value) predicate, {
required String error,
}) => predicate(value) ? Valid._(value) : Invalid._({error});