Skip to content

String Validation

Preview ZodSharp Reviewed 2026-09-17 purview-dev/zodsharp aspnetcore c-sharp csharp dotnet json-schema newtonsoft-json schema source-generator system-text-json validation zero-allocation zod

ZodString (namespace ZodSharp.Schemas) validates string values. ParseInternal rejects null with an invalid_type error ("Expected string, but got null"); on success the accumulated rules run.

using ZodSharp;
var schema = Z.String().Min(3).Max(50).Email();
var result = schema.Validate("[email protected]");
Method Signature Rule added
Min Min(int minLength) MinLengthRuletoo_small via validation_failed when too short
Max Max(int maxLength) MaxLengthRule
Length Length(int length) exact length (both bounds)
Email Email() EmailRule — static compiled regex
Regex Regex(Regex pattern, string? message) / Regex(string pattern, string? message) RegexRule; the string overload compiles with a 100 ms timeout
Url Url(string? message) UrlRule — regex or absolute http/https URI
Phone Phone(string? message) PhoneRule — digits plus () .+-, at least one digit
CreditCard CreditCard(string? message) CreditCardRule — Luhn algorithm
Base64String Base64String(string? message) Base64StringRuleConvert.FromBase64String
UUID UUID(string? message) UUIDRule — char-scan, RFC 9562 versions 1-8, variant nibble 8-9/a-b, plus nil and max
UUID UUID(UuidVersion version, string? message) UUIDRule — requires a specific version (e.g. V7), variant nibble 8-9/a-b, nil/max rejected
StartsWith StartsWith(string prefix, string? message) StartsWithRule — ordinal comparison
EndsWith EndsWith(string suffix, string? message) EndsWithRule — ordinal comparison
ToLower ToLower() wraps a transform (ToLowerInvariant), returns a ZodString
ToUpper ToUpper() wraps a transform (ToUpperInvariant)
Trim Trim() wraps a transform (Trim)
ValidateSpan ValidateSpan(ReadOnlySpan<char> value) zero-allocation span validation
var email = Z.String().Email().Validate("[email protected]");
var url = Z.String().Url().Validate("https://example.com");
var uuid = Z.String().UUID().Validate("550e8400-e29b-41d4-a716-446655440000");
var uuidV7 = Z.String().UUID(UuidVersion.V7).Validate("0192b4c1-7a9b-7f5e-9a3c-2d4e6f8a0b1c");
var prefix = Z.String().StartsWith("https://");
var suffix = Z.String().EndsWith(".com");
var exact = Z.String().Length(10);
var normalized = Z.String().Trim().ToUpper().Validate(" hello "); // "HELLO"
ReadOnlySpan<char> span = "[email protected]".AsSpan();
var spanResult = Z.String().Min(3).Max(50).Email().ValidateSpan(span);

Rules produce ValidationError entries with code validation_failed and an empty path. Many methods accept a custom message parameter. Rule structs live in ZodSharp.Rules and can be reused standalone with IValidationRule<T>.

ValidateSpan(ReadOnlySpan<char> value) avoids string allocations on the validation path. An empty span validates successfully as "".