String Rules
String rules validate textual content and text containment. They do not replace type validation; use Types and Files when the value must specifically be a string, then add these rules for content.
Alpha
Requires the value to contain only letters.
[Alpha]RuleFor(x => x.FirstName)
.Alpha();AlphaNumeric
Requires the value to contain only letters and numbers.
[AlphaNumeric]RuleFor(x => x.ReferenceCode)
.AlphaNumeric();AlphaDash
Requires the value to contain only letters, numbers, dashes, and underscores. Use it for slugs, handles, and similar identifier-style text.
[AlphaDash]RuleFor(x => x.Slug)
.AlphaDash();Ascii
Requires the value to contain only single-byte ASCII characters.
[Ascii]RuleFor(x => x.LegacyCode)
.Ascii();Lowercase
Requires the text value to already be lowercase, and to contain at least one character that has a case. Digits and punctuation neither pass nor fail it on their own, so abc-1 passes while 123 does not.
[Lowercase]RuleFor(x => x.Username)
.Lowercase();Uppercase
Requires the text value to already be uppercase, and to contain at least one character that has a case. Digits and punctuation neither pass nor fail it on their own, so ABC-1 passes while 123 does not.
[Uppercase]RuleFor(x => x.CountryCode)
.Uppercase();StartsWith
Requires the text value to start with one of the provided prefixes.
[StartsWith(params string[] values)]
[StartsWith("ORD-", "INV-")]RuleFor(x => x.OrderNumber)
.StartsWith("ORD-", "INV-");EndsWith
Requires the text value to end with one of the provided suffixes.
[EndsWith(params string[] values)]
[EndsWith(".jpg", ".png")]RuleFor(x => x.FileName)
.EndsWith(".jpg", ".png");Contains
Requires the text value or collection to contain one of the provided values.
[Contains(params string[] values)]
[Contains("admin", "editor")]RuleFor(x => x.Roles)
.Contains("admin", "editor");DoesNotStartWith
Rejects text that starts with one of the provided prefixes.
[DoesNotStartWith(params string[] values)]
[DoesNotStartWith("admin", "system")]RuleFor(x => x.Username)
.DoesNotStartWith("admin", "system");DoesNotEndWith
Rejects text that ends with one of the provided suffixes.
[DoesNotEndWith(params string[] values)]
[DoesNotEndWith(".tmp", ".bak")]RuleFor(x => x.FileName)
.DoesNotEndWith(".tmp", ".bak");DoesNotContain
Rejects text or collections containing one of the provided values.
[DoesNotContain(params string[] values)]
[DoesNotContain("password", "secret")]RuleFor(x => x.DisplayName)
.DoesNotContain("password", "secret");