checkIterable function

void checkIterable(
  1. dynamic input,
  2. String parameterName, {
  3. int? maxItems,
  4. int? minItems,
  5. bool uniqueItems = false,
})

Checks the input against the given Iterable validators.

Throws an FormatException when the input is not:

  • longer than maxItems.
  • shorter than minItems.
  • containing only uniqueItems. If the input is a Set or BuiltSet this validation is skipped.

Implementation

void checkIterable(
  dynamic input,
  String parameterName, {
  int? maxItems,
  int? minItems,
  bool uniqueItems = false,
}) {
  switch (input) {
    case ListJsonObject():
    case ListBuilder():
    case SetBuilder():
    case Iterable():
      break;
    default:
      return;
  }

  late final length = switch (input) {
    SetBuilder() => input.length,
    ListBuilder() => input.length,
    ListJsonObject() => input.value.length,
    Iterable() => input.length,
    _ => throw StateError('Type does not have a length'),
  };

  if (minItems != null && length < minItems) {
    throw FormatException('Parameter "$parameterName" has to be at least $minItems items long but was $length long');
  }

  if (maxItems != null && length > maxItems) {
    throw FormatException('Parameter "$parameterName" has to be at most $maxItems items long but was $length long');
  }

  late final value = switch (input) {
    // A set only contains unique items. We can thus skip this check.
    BuiltSet() || SetBuilder() || Set() => null,
    ListBuilder() => input.build().toSet(),
    ListJsonObject() => input.value.toSet(),
    Iterable() => input.toSet(),
    _ => throw StateError('Type does not behave like an iterable'),
  };

  if (uniqueItems && value != null) {
    if (length != value.length) {
      throw FormatException('Parameter "$parameterName" has to only contain unique items');
    }
  }
}