Canonicalize identifiers
Normalize usernames or other case-insensitive identifiers before application logic consumes them.
Zod · Module
.toLowerCase() defines a Zod string schema that converts parsed strings to lowercase. It is a chainable string transformation.
.toLowerCase() is a chainable string-schema transform. During parsing, it converts the string value to lowercase, so the returned value is normalized rather than preserving the original casing.
z.string().toLowerCase()A Zod string schema that parses strings into lowercase values.
z.string() to define a lowercase-producing string schema.import { z } from "zod";
const username = z.string().toLowerCase();
console.log(username.parse("AdaLovelace")); // "adalovelace"The schema accepts a string and transforms its parsed value to lowercase.
Normalize usernames or other case-insensitive identifiers before application logic consumes them.
Store or compare user-provided text using a consistent lowercase representation.
.toLowerCase() do?It creates a string schema that lowercases the value during parsing. For example, parsing "AdaLovelace" produces "adalovelace".
Follow-up: How would you combine it with length constraints? Chain the constraints and transform on the string schema, such as z.string().min(1).toLowerCase().
It is used as a chainable method on z.string(). The documentation presents it alongside other string transformations such as trimming, uppercasing, and Unicode normalization.
Follow-up: When should normalization happen? Apply it in the schema when the normalized representation should be the value returned by parsing.
The documented declaration is z.string().toLowerCase(). It returns a schema whose parsed string value is lowercase.
Follow-up: What is a practical use? Normalize case-insensitive identifiers, such as usernames or email-like keys, before later application logic uses them.
z.string()trim()toUpperCase()normalize()