Skip to main content

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() - Use z.email() instead
  • .url() - Use z.url() instead
  • .uuid() - Use z.uuid() instead
  • .guid() - Use z.guid() instead
  • .cuid() - Use z.cuid() instead
  • .cuid2() - Use z.cuid2() instead
  • .ulid() - Use z.ulid() instead
  • .nanoid() - Use z.nanoid() instead
  • .jwt() - Use z.jwt() instead
  • .base64() - Use z.base64() instead
  • .base64url() - Use z.base64url() instead
  • .emoji() - Use z.emoji() instead
  • .ipv4() - Use z.ipv4() instead
  • .ipv6() - Use z.ipv6() instead
  • .datetime() - Use z.iso.datetime() instead
  • .date() - Use z.iso.date() instead
  • .time() - Use z.iso.time() instead

Chaining

All validation and transform methods return this, enabling method chaining:

See Also