Prompt
Treat null/undefined and “absence vs presence” as first-class concerns.
- TypeScript: Keep null safety effective by ensuring
strictNullChecksis enabled and not overridden by extended configs (explicitly set it totrueif needed). - Zod runtime: Decide what should happen when input is
undefinedvs when it is a wrong value ornull.- Use
.default(value)for absent inputs (typicallyundefined). - Use
.catch(fallback)for parsing failures (including wrong strings), not as a substitute for handlingundefined.
- Use
- Form inputs: Don’t assume “required field” means “non-empty”. Browsers/DOM often send
""for an “empty” required field;z.string()will accept""as a valid string. If you require non-empty content, validate it explicitly (e.g.,.min(1)).
Example:
import { z } from "zod";
const boolSchema = z.string().boolean();
// absent (undefined) -> fallback
const withDefault = boolSchema.default(false);
// wrong value (including "other" / null if you pass it in) -> fallback
const withCatch = boolSchema.catch(false);
// non-empty string required by UI
const requiredText = z.string().min(1, "Required");
Apply these consistently so validation behavior matches what your types and UI expectations actually mean.