Normalize case
Canonicalizing user-entered identifiers or labels before storage or comparison.
Zod · Module
.toUpperCase() transforms parsed strings to uppercase. It takes no arguments and can be chained with other string transformations such as .trim().
.toUpperCase() is a string schema transformation that converts a successfully parsed string to uppercase. It can be chained with other string transformations such as .trim(); the resulting schema still represents a string.
z.string().toUpperCase()A Zod string schema that uppercases parsed string values.
.toUpperCase() on a string schema to create an uppercase-normalizing parser..trim() when whitespace should be removed before uppercasing..prefault(" tuna "), parsing undefined yields the normalized value "TUNA"..toUpperCase() appears among string transformations, alongside Unicode normalization and case conversion helpers..overwrite(), so they preserve the schema's inferred string type rather than creating a different output type.import { z } from "zod";
const schema = z.string().trim().toUpperCase();
console.log(schema.parse(" tuna ")); // "TUNA"Trims surrounding whitespace, then converts the parsed string to uppercase.
Canonicalizing user-entered identifiers or labels before storage or comparison.
Combining whitespace cleanup and case conversion in an input pipeline.
Applying a string transformation while retaining a string schema type.
.toUpperCase() do?It is called on a z.string() schema and uppercases the string during parsing. For example, z.string().toUpperCase().parse("hello") produces "HELLO".
Follow-up: How would you normalize both whitespace and case? Chain the transformations: z.string().trim().toUpperCase().
It is a transformation method listed alongside .trim(), .toLowerCase(), and .normalize(). The input must first satisfy the string schema, after which the uppercase conversion is applied.
Follow-up: What happens if the input is not a string? The base z.string() schema rejects it before the string transformation can run.
The method takes no argument: z.string().toUpperCase(). Its parsed result remains a string, with lowercase letters converted to uppercase.
Follow-up: How can the operation be combined with a default-like preprocessing flow? Chain it with other documented schema methods, such as .trim() and .prefault().
z.stringtrimtoLowerCasenormalizeprefaultoverwrite