Repository navigation
Expo 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.
// 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.
// 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.
- 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