Validated<T extends Object?> class
sealed
Represents the result of validation: either Valid or Invalid.
Validated is similar to Result but specifically designed for validation use cases where you may need to collect multiple errors instead of failing fast on the first error. This makes it ideal for form validation where users want to see all problems at once, rather than fixing them one by one.
Use Validated when:
- You need to collect multiple validation errors together
- You want a fluent, chainable validation API
- You're validating forms or complex objects with many rules
- You want users to see all validation problems simultaneously
Use Result instead when:
- You only care about a single error, not accumulation
- You need fail-fast behavior
- You're handling operations with typed errors
Example:
// Collect all validation errors at once
final validation = check(name, (n) => n.isNotEmpty, error: 'Required')
.check((n) => n.length >= 3, error: 'Too short')
.check((n) => n.length <= 50, error: 'Too long');
if (validation.isValid) {
processName(validation.unwrapOrNull()!);
} else {
// Show all errors to the user
final errors = (validation as Invalid).errors.toList();
}
- Implementers
- Available extensions
- Annotations
-
- @experimental
- @immutable
Constructors
- Validated.check(T value, bool predicate(T value), {required String error})
-
Validates a value against a predicate and returns the result.
factory
-
Validated.invalid(Set<
String> errors) -
Creates an Invalid result with error messages.
constfactory
- Validated.valid(T value)
-
Creates a Valid result with the given value.
constfactory
Properties
Methods
-
and(
Validated< T> other) → Validated<T> - Combines two validation results, merging their errors.
-
asResult(
) → Result< T, Set< String> > - Converts this validation result to a Result type.
-
check(
bool rule(T), {required String error}) → Validated< T> -
Available on Validated<
Applies an additional validation rule to this result.T> , provided by the ValidatedOps extension -
getErrors(
) → Set< String> ? - Gets errors if Invalid, null if Valid.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unwrapOrNull(
) → T? - Extracts the value if Valid, or returns null if Invalid.
Operators
-
operator &(
Validated< T> other) → Validated<T> - Combines two validation results, merging their errors.
-
operator ==(
Object other) → bool -
The equality operator.
inherited