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?) → chain min, max, length, startsWith, endsWith, regex, email, uuid, url
  • z.number(message?) → chain min, max, int, positive, negative (coerces from strings)
  • z.boolean(message?) - accepts true/false/1/0/on/off
  • z.file(message?) → chain minSize, maxSize (bytes), type(types: string[]) matched with startsWith
  • z.any() - always succeeds

Composites

  • z.object(shape, message?) - also parses a JSON string before validating
  • z.array(itemSchema, message?) → min, max, length; also accepts a JSON string
  • z.union(schemas, message?)
  • z.enum(values, message?)
  • z.optional(schema) - value may be absent; key becomes optional in objects
  • z.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()]),
});