Repository navigation
Expo Collections
Expo checks collection size separately from item values. Items* rules apply to the collection itself. String, comparison, enum, and reusable custom rules on an enumerable property apply to each item.
// CourseSelection.cs
[Expo]
public sealed partial class CourseSelection
{
[IsRequired("Select courses before submitting.")]
[ItemsHasMinimumLength(1)]
[ItemsHasMaximumLength(5)]
[IsGreaterThan(0, "Course IDs must be positive.")]
public int[]? CourseIds { get; init; }
}CourseIds must be present, contain between one and five entries, and have positive IDs. A negative ID at index 2 produces an error at /CourseIds/2. A size failure uses /CourseIds.
| Rule | Collection condition |
|---|---|
[ItemsHasMinimumLength(n)] |
At least n items |
[ItemsHasMaximumLength(n)] |
At most n items |
[ItemsHasExactLength(n)] |
Exactly n items |
[ItemsIsNotEmpty] |
At least one item |
These size rules accept null. [IsRequired] rejects a null collection but accepts an empty one. Combine presence and size rules when both matter.
Arrays and generic enumerable collections are supported. For an IEnumerable<T> without a count, validation enumerates it to determine size. Item rules can also enumerate it separately. Supply a repeatable collection, such as an array or list, when applying several rules.
// RegistrationContacts.cs
[Expo]
public sealed partial class RegistrationContacts
{
[ItemsIsNotEmpty]
[StringMatchesEmail]
public List<string?>? Emails { get; init; }
}The email rule runs for each item in a supplied list. Its null behavior remains the same as on a scalar property, so a null item passes this format rule. A null outer list also passes; an empty non-null list fails [ItemsIsNotEmpty].
[IsRequired] on a collection checks the outer collection. To reject null items, use a reusable custom item rule that rejects null, or a model-specific rule that inspects the list. See Custom rules.
When a collection's declared item type implements IExpoValidatable, Expo validates each non-null child and prefixes its errors with the collection name and index. Null child items are skipped by nested validation. See Nested models for an example.
- Home
- Getting started
-
Package guides
- Partie
- Expo
-
Mise
- Database setup
- Table mappings
- Aliases
- Relationships
- Result rows
- SQL interpolation
- Queries
- Filtering
- Joins
- Aggregation
- Paging and ordering
- Subqueries
- Set operations
- Common table expressions
- Readers
- Streaming
- Writers
- Inserts
- Updates
- Deletes
- Returned rows
- Custom SQL and procedures
- Transactions
- Resource lifetimes
- Database dialects
- Diagnostics
- Integrations
- Recipes
- Reference
- Troubleshooting
- Development