Skip to content

email-schema

Edit

Validates a trimmed email address and returns a branded value without changing its casing.

email-schema.ts
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>;
Terminal
wget -O src/schemas/email-schema.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/zod-schemas/email-schema.ts
email-schema.example.ts
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.COM
  • input (string) — The candidate email address. Leading and trailing whitespace is removed before validation.

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.