Skip to content

Expo String Rules

SteffenBlake edited this page Sep 28, 2026 · 1 revision

String rules

String rules check the length, content, or format of a value. They accept null, so combine them with [IsRequired] when the property must be present.

Length and whitespace

// CourseDescription.cs
[Expo]
public sealed partial class CourseDescription
{
    [IsRequired]
    [StringIsNotWhiteSpace("A course title must contain text.")]
    [StringHasMaximumLength(120)]
    public string? Title { get; init; }

    [StringHasExactLength(6)]
    public string? ShortCode { get; init; }
}

Title must contain non-whitespace text and fit within 120 characters. ShortCode is optional, but a supplied value must have exactly six characters.

Rule Condition for a non-null value
[StringHasMinimumLength(n)] Length is at least n
[StringHasMaximumLength(n)] Length is at most n
[StringHasExactLength(n)] Length equals n
[StringIsNotEmpty] Length is greater than zero
[StringIsNotWhiteSpace] Contains at least one non-whitespace character

Length uses string.Length. [StringIsNotEmpty] accepts spaces; [StringIsNotWhiteSpace] rejects empty and whitespace-only strings. Neither rule trims or changes the value.

Formats

// StudentContact.cs
[Expo]
public sealed partial class StudentContact
{
    [IsRequired]
    [StringMatchesEmail]
    public string? Email { get; init; }

    [StringMatchesPhoneNumber]
    public string? Phone { get; init; }

    [StringMatchesUrl]
    public string? ProfileUrl { get; init; }
}

The phone rule accepts international numbers beginning with +, followed by a nonzero digit and up to 14 more digits, such as +14165551234. The URL rule accepts absolute HTTP and HTTPS URLs. The email rule checks a simple address shape; it does not check whether a mailbox exists.

Rule Expected value
[StringMatchesEmail] Email address shape
[StringMatchesPhoneNumber] International phone number
[StringMatchesUuid] UUID text
[StringMatchesUrl] Absolute HTTP or HTTPS URL
[StringMatchesIpAddress] IPv4 or IPv6 address
[StringMatchesIpv4Address] IPv4 address
[StringMatchesIpv6Address] IPv6 address
[StringMatchesBase64] Decodable Base64 text
[StringMatchesHexColor] Three, four, six, or eight hex digits, with optional #
[StringMatchesSlug] Lowercase letters and digits separated by single hyphens
[StringMatchesAlpha] One or more Unicode letters
[StringMatchesAlphaNumeric] One or more Unicode letters or decimal digits
[StringMatchesDigits] One or more digits

Each rule accepts an optional message argument. Use Generated regex rules when your application needs a specific text pattern.

On a collection of strings, these rules check each item. See Collections for collection size and indexed errors.

Clone this wiki locally