validate method

  1. @override
List<T> validate(
  1. String field,
  2. dynamic value
)
override

Implementation

@override
List<T> validate(String field, dynamic value) {
  if (value == null) {
    if (required) {
      throw ValidationError(field, 'Field "$field" is required');
    }
    return <T>[];
  }

  if (value is! List) {
    throw ValidationError(field, 'Expected list, got ${value.runtimeType}', value);
  }

  final list = value as List;

  if (minLength != null && list.length < minLength!) {
    throw ValidationError(field, 'Must have at least $minLength items', list);
  }

  if (maxLength != null && list.length > maxLength!) {
    throw ValidationError(field, 'Must have at most $maxLength items', list);
  }

  final validatedList = <T>[];
  for (int i = 0; i < list.length; i++) {
    try {
      validatedList.add(itemValidator.validate('$field[$i]', list[i]));
    } on ValidationError catch (e) {
      throw e;
    }
  }

  return validatedList;
}