ValidatedOps<T> extension
Extension for chaining multiple validation rules on Validated results.
This extension provides fluent API for applying sequential validation rules to a value. As you chain calls to check, validation errors accumulate in a Set, allowing you to collect all failures at once rather than stopping at the first error.
This is particularly useful for form validation where you want to show users all problems with their input simultaneously, improving user experience by avoiding multi-pass form submission.
Example:
// Chain multiple validation rules
final email = check(
userInput,
(v) => v.isNotEmpty,
error: 'Email is required',
)
.check((v) => v.contains('@'), error: 'Invalid email format')
.check((v) => !v.endsWith('.'), error: 'Cannot end with period')
.check((v) => v.length <= 100, error: 'Email too long');
// Check if all rules passed
if (email.isValid) {
final validEmail = email.unwrapOrNull()!;
saveEmail(validEmail);
} else {
final invalid = email as Invalid<String>;
showErrors(invalid.errors); // Show all errors to user
}