listEach function

Validator listEach(
  1. Validator itemValidator
)

Returns a Validator that runs itemValidator for each item in the list

This validator also checks that the value is a list

Implementation

Validator listEach(Validator itemValidator) {
  return (value) {
    if (value is! List) return Result.invalid('List', value);

    for (int index = 0; index < value.length; index++) {
      final item = value[index];
      final result = itemValidator.call(item);

      if (result.isNotValid) {
        return Result.invalid('[$index] -> ${result.expected}', item);
      }
    }

    return Result.valid;
  };
}