Skip to content

Expo Collections

SteffenBlake edited this page Sep 28, 2026 · 1 revision

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.

Require a collection and check its size

// 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.

Check item values

// 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.

Nested item models

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.

Clone this wiki locally