validate method

  1. @override
ValidationResult<T, E> validate(
  1. T value
)
override

Validates value and returns a ValidationResult.

Returns Valid if the value passes validation, or Invalid/InvalidAll if it fails.

Implementation

@override
ValidationResult<T, E> validate(T value) {
  final errors = <E>[];

  for (final validator in validators) {
    // Short-circuit if we've collected enough errors
    if (errors.length >= maxErrors) break;

    final result = validator.validate(value);
    for (final error in result.errors) {
      errors.add(error);
      if (errors.length >= maxErrors) break;
    }
  }

  if (errors.isEmpty) {
    return Valid(value);
  }

  return errors.length == 1 ? Invalid(errors.first) : InvalidAll(errors);
}