Skip to content

Expo Value Rules

SteffenBlake edited this page Sep 28, 2026 · 1 revision

Value rules

Comparison rules check a property against a constant. Enum rules reject values that the enum does not define. Combine rules when an operation needs a bounded range.

Compare with constants

// CourseCapacity.cs
[Expo]
public sealed partial class CourseCapacity
{
    [IsGreaterThanOrEqualTo(1, "A course needs at least one place.")]
    [IsLessThanOrEqualTo(200)]
    public int Capacity { get; init; }

    [IsNotEqualTo("DRAFT", "Choose a published course code.")]
    public string? CourseCode { get; init; }
}

Capacity must be between 1 and 200 inclusive. CourseCode fails when it equals DRAFT; the equality rule allows null here because it differs from that constant.

Rule Required comparison
[IsGreaterThan(value)] Property is greater than the constant
[IsGreaterThanOrEqualTo(value)] Property is greater than or equal to the constant
[IsLessThan(value)] Property is less than the constant
[IsLessThanOrEqualTo(value)] Property is less than or equal to the constant
[IsEqualTo(value)] Property equals the constant
[IsNotEqualTo(value)] Property differs from the constant

Ordering uses Comparer<T>.Default; equality uses EqualityComparer<T>.Default. The constant must be compatible with the property's type and valid as a C# attribute argument. Nullable values follow those comparers' semantics. Add an explicit presence rule when the application requires a value.

For a range relative to another property, use Property comparisons.

Defined enum values

// CourseStatus.cs
public enum CourseStatus
{
    Draft,
    Published,
    Closed
}
// CoursePublication.cs
[Expo]
public sealed partial class CoursePublication
{
    [EnumIsDefined("Choose a known course status.")]
    public CourseStatus Status { get; init; }
}

Casting an arbitrary integer to an enum does not guarantee a declared value. [EnumIsDefined] checks it with Enum.IsDefined. For nullable enums, null passes; add [IsRequired] if needed. For flags enums, a combination passes only when that exact combined value is declared.

Comparison and enum rules also apply to individual collection items. See Collections.

Clone this wiki locally