string
ts
z.string().max(5);
z.string().min(5);
z.string().length(5);
z.string().regex(/^[a-z]+$/);
z.string().startsWith("aaa");
z.string().endsWith("zzz");
z.string().includes("---");
z.string().uppercase();
z.string().lowercase();ts
z.string().trim(); // 去除空白
z.string().toLowerCase(); // 转小写
z.string().toUpperCase(); // 转大写
z.string().normalize(); // 规范化 Unicode 字符ts
z.email();
z.uuid();
z.url();
z.httpUrl(); // http or https URLs only
z.hostname();
z.emoji(); // 验证单个 emoji 字符
z.base64();
z.base64url();
z.hex();
z.jwt();
z.nanoid();
z.cuid();
z.cuid2();
z.ulid();
z.ipv4();
z.ipv6();
z.mac();
z.cidrv4(); // ipv4 CIDR block
z.cidrv6(); // ipv6 CIDR block
z.hash("sha256"); // or "sha1", "sha384", "sha512", "md5"
z.iso.date();
z.iso.time();
z.iso.datetime();
z.iso.duration();templateLiteral / 模板字面量
在 zod@4.0 中引入。
ts
const schema = z.templateLiteral(["hello, ", z.string(), "!"]);
// `hello, ${string}!`email
默认的 email 很严格
可以自己写正则
ts
z.email({ pattern: /your regex here/ });官方也提供了一些
ts
// Zod 默认邮箱正则
z.email();
z.email({ pattern: z.regexes.email }); // 等价
// 浏览器用于 input[type=email] 字段的正则
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email
z.email({ pattern: z.regexes.html5Email });
// 经典 emailregex.com 正则 (RFC 5322)
z.email({ pattern: z.regexes.rfc5322Email });
// 一个允许 Unicode 的宽松正则 (适用于国际邮箱)
z.email({ pattern: z.regexes.unicodeEmail });UUID
ts
// 支持 "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"
z.uuid({ version: "v4" });
// 方便形式
z.uuidv4();
z.uuidv6();
z.uuidv7();更多
还有很多没有记录,具体看官网