eskemaList function

Validator eskemaList(
  1. List<Validator> eskema
)

Returns a Validator that checks a value against the eskema provided, the eskema defines a validator for each item in the list

Example:

final isValidList = eskemaList([isType<String>(), isType<int>()]);
isValidList(["1", 2]).isValid;   // true
isValidList(["1", "2"]).isValid; // false
isValidList([1, "2"]).isValid;   // false

isValidList will only be valid:

  • if the array is of length 2
  • the first item is a string
  • and the second item is an int

This validator also checks that the value is a list

Implementation

Validator eskemaList(List<Validator> eskema) {
  return (value) {
    // Before checking the eskema, we validate that it's a list and matches the eskema length
    final result = all([isType<List>(), listIsOfLength(eskema.length)]).call(value);
    if (result.isNotValid) return result;

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

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

    return Result.valid;
  };
}