l.list()
Passes when the value is a List and every element passes element. Each element’s issues are reported at the element’s index, so a bad second element appears at path 1, or tags.1 inside a schema. The output is a new List<E> of the validated elements, where E is the element validator’s output: l.list(l.int()) validates to List<int?> and l.list(l.int().required()) to List<int>. Use l.list(l.any()) to accept any elements and l.list(l.union([...])) for elements that may take several shapes.
Signature
Section titled “Signature”ListValidator<E, List<E>?> list<E>(ValidatorReference<E> element, {String? message, MessageBuilder? messageBuilder})Parameters
Section titled “Parameters”| Parameter | Type | Meaning |
|---|---|---|
element |
ValidatorReference<E> |
Runs on every element. A forward reference is accepted. |
message |
String? |
Replaces the default error message. |
messageBuilder |
MessageBuilder? |
Builds the message from the issue. message wins when both are given. See Message builders. |
Code invalidType. Default message: value must be a list when the value is not a list. Element failures carry the element validator’s own code, at the element’s index. value is replaced by the field name when there is one. Elements inherit the list’s field name.
Code generation
Section titled “Code generation”A List<T>, Set<T> or Iterable<T> parameter maps to l.list(e) with e derived from T. A non-nullable T gets .required() inside the list; a nullable T? does not. Annotations on the field apply to the list itself, so @HasLength on a list fails generation. Element validators cannot be annotated yet.
Example
Section titled “Example”final tags = l.list(l.string().required());required List<String> tags, // l.list(l.string().required()).required()List<String?>? notes, // l.list(l.string())required List<Address> places, // l.list(forwardRef(() => $AddressSchema.required())).required()tags.validate(input)+2-2- valid:
['a', 'b'] - valid:
[] - invalid:
['a', 1, null] - invalid:
'a'
An optional element validator lets null elements through:
ValidationSuccess(data: [1, null])ValidationFailure(errors: {1: [value is required]})