validate method

  1. @override
ValidationResult validate(
  1. T? value, {
  2. bool stopOnFirstError = false,
})

Implementation

@override
ValidationResult validate(T? value, {bool stopOnFirstError = false}) {
  if (value == null) {
    return ValidationResult.failure(['Value must not be null.']);
  }

  List<String>? collectedErrors;

  for (final validator in validators) {
    final (valid, error) = validator(value, context);

    // If any validator passes, NotValidator fails
    if (valid) {
      return ValidationResult.failure([
        error ?? 'A validator passed, which is not allowed.',
      ]);
    }

    // Collect error if validator fails (for informational purposes, if needed)
    if (error != null && !stopOnFirstError) {
      collectedErrors ??= [];
      collectedErrors.add(error);
    }

    // If stopOnFirstError is true, stop after the first passing validator
    if (stopOnFirstError && valid) {
      return ValidationResult.failure([
        error ?? 'A validator passed, which is not allowed.',
      ]);
    }
  }

  return ValidationResult.success();
}