email-schema
EditValidates a trimmed email address and returns a branded value without changing its casing.
import * as z from "zod";
const EMAIL_MAX_LENGTH = 254;
/** Validates and brands a trimmed email address without changing its casing. */export const emailSchema = z .string({ error: "Email must be a string" }) .check(z.trim()) .pipe( z .email({ error: "Enter a valid email address" }) .check( z.maxLength(EMAIL_MAX_LENGTH, { error: `Email must contain at most ${EMAIL_MAX_LENGTH} characters`, }), ), ) .brand<"Email">() .meta({ title: "Email", description: "A trimmed email address with its original letter casing preserved.", examples: ["person@example.com"], });
export type Email = z.infer<typeof emailSchema>;Download
Section titled “Download”wget -O src/schemas/email-schema.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/zod-schemas/email-schema.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/zod-schemas "$temp_dir" && mv "$temp_dir/email-schema.ts" src/schemas/email-schema.ts && rm -r "$temp_dir"Examples
Section titled “Examples”import { emailSchema, type Email } from "./email-schema";
const email = emailSchema.parse(" Person@Example.COM ");
const sendReceipt = (recipient: Email) => { console.log(`Sending receipt to ${recipient}`);};
sendReceipt(email);// Output: Sending receipt to Person@Example.COMinput(string) — The candidate email address. Leading and trailing whitespace is removed before validation.
Returns
Section titled “Returns”Email, a string branded with "Email". The trimmed value must pass Zod 4’s
email format and contain at most 254 characters.
Letter casing is preserved, including the local part. The schema validates syntax only; it does not confirm that the address or mailbox exists.