z
Schema builders exported as z from jsr:@webtools/expressapi. Runtime validation + type narrowing.
Import
import { z } from "jsr:@webtools/expressapi";Pass schemas as the fourth argument of a route method: { body?, query?, params? }. Narrative guide: Validation.
Primordials
z.string(message?)→ chainmin,max,length,startsWith,endsWith,regex,email,uuid,urlz.number(message?)→ chainmin,max,int,positive,negative(coerces from strings)z.boolean(message?)- acceptstrue/false/1/0/on/offz.file(message?)→ chainminSize,maxSize(bytes),type(types: string[])matched withstartsWithz.any()- always succeeds
Composites
z.object(shape, message?)- also parses a JSON string before validatingz.array(itemSchema, message?)→min,max,length; also accepts a JSON stringz.union(schemas, message?)z.enum(values, message?)z.optional(schema)- value may be absent; key becomes optional in objectsz.nullable(schema)-T | null
Examples
z.string().min(3).max(100).email();
z.string().uuid();
z.string().regex(/^[A-Z]+$/);
z.number().int().positive().min(1).max(100);
z.boolean();
z.file().maxSize(1_000_000).type(["image/", "application/pdf"]);
z.object({
name: z.string("Name is required"),
age: z.optional(z.number().int()),
tags: z.array(z.string()).min(1).max(10),
role: z.enum(["admin", "user"]),
note: z.nullable(z.string()),
payload: z.union([z.string(), z.number()]),
});