Lists and tuples
Validate repeated values with lists or fixed positions with tuples.
Lists
Use the list() method to create a list schema. This method allows you to validate and transform lists.
list(string()).min(5);
list(string()).max(10);
list(string()).length(5);
list(string()).everyOf(['Acanthis', 'Dart']);
list(string()).anyOf(['Acanthis', 'Dart']);
list(string()).unique();You can also create a list from the type.
string().list();
number().list();
boolean().list();Instead, if you want to get the element type from the list type you can use the unwrap() method.
final names = string().list();
final elementType = names.unwrap(); // string()Tuples
Unlike lists, tuples are fixed-length lists that specify different schemas for each index.
final tupleSchema = tuple([
string(),
number(),
boolean(),
]);You can also create a tuple from the type.
final tupleSchema = string().tuple([
string(),
number(),
boolean(),
]);INFO
The previous example creates a tuple of 4 elements not 3. The first element is the schema type that is used to create the tuple and the rest are what you pass to the tuple() method.
final tupleSchema = string().tuple([
string(),
number(),
boolean(),
]); // [string(), string(), number(), boolean()]variadic()
To create a variadic tuple, you can use the variadic() method. This method allows you to create a tuple with a fixed number of elements and a variable number of elements.
final tupleSchema = string().tuple([
string(),
number(),
]).variadic();The last element will behave like a list. It will accept any number of elements of the same type.
final tupleSchema = string().tuple([
string(),
number(),
]).variadic();
tupleSchema.parse([
'Acanthis',
'Dart',
5,
10,
15,
20,
]); // ✅