Basic Usage
Type Signature
string | $ZodStringParams
Optional error message (string) or configuration object
Validation Methods
Length Validations
.min()
Enforces minimum string length.
number
required
Minimum length of the string
string | $ZodCheckMinLengthParams
Custom error message or params object with
message property.max()
Enforces maximum string length.
number
required
Maximum length of the string
string | $ZodCheckMaxLengthParams
Custom error message or params object
.length()
Enforces exact string length.
number
required
Exact length required
string | $ZodCheckLengthEqualsParams
Custom error message or params object
.nonempty()
Requires at least one character. Equivalent to .min(1).
string | $ZodCheckMinLengthParams
Custom error message
Content Validations
.regex()
Validates string against a regular expression.
RegExp
required
Regular expression pattern to match
string | $ZodCheckRegexParams
Custom error message or params object
.includes()
Requires string to contain a substring.
string
required
Substring that must be present
string | $ZodCheckIncludesParams
Custom error message or params object with optional
position property.startsWith()
Requires string to start with a specific prefix.
string
required
Required prefix
string | $ZodCheckStartsWithParams
Custom error message
.endsWith()
Requires string to end with a specific suffix.
string
required
Required suffix
string | $ZodCheckEndsWithParams
Custom error message
Case Validations
.lowercase()
Validates that string contains only lowercase characters.
string | $ZodCheckLowerCaseParams
Custom error message
.uppercase()
Validates that string contains only uppercase characters.
string | $ZodCheckUpperCaseParams
Custom error message
Transform Methods
Transform methods modify the string value during parsing..trim()
Removes whitespace from both ends.
.toLowerCase()
Converts string to lowercase.
.toUpperCase()
Converts string to uppercase.
.normalize()
Normalizes the string using Unicode normalization.
'NFC' | 'NFD' | 'NFKC' | 'NFKD' | (string & {})
Unicode normalization form (default: “NFC”)
.slugify()
Converts string to URL-friendly slug format.
Properties
.format
Returns the format string if the schema has a specific format, or null.
.minLength
Returns the minimum length constraint, or null if not set.
.maxLength
Returns the maximum length constraint, or null if not set.
Deprecated Format Methods
The following methods are deprecated in favor of standalone format schemas:.email()- Usez.email()instead.url()- Usez.url()instead.uuid()- Usez.uuid()instead.guid()- Usez.guid()instead.cuid()- Usez.cuid()instead.cuid2()- Usez.cuid2()instead.ulid()- Usez.ulid()instead.nanoid()- Usez.nanoid()instead.jwt()- Usez.jwt()instead.base64()- Usez.base64()instead.base64url()- Usez.base64url()instead.emoji()- Usez.emoji()instead.ipv4()- Usez.ipv4()instead.ipv6()- Usez.ipv6()instead.datetime()- Usez.iso.datetime()instead.date()- Usez.iso.date()instead.time()- Usez.iso.time()instead
Chaining
All validation and transform methods returnthis, enabling method chaining:
See Also
- String Formats - Specialized string format schemas
- Transforms - General transformation methods
- Custom Validations - Using
.refine()for custom logic