Zod 4

Last reviewed Sep 6, 2026

Export as PDF

Prompt Details

Prompt Composition

3prompts
  1. JavaScript ES2026102 lines · 3.1K tokens
  2. TypeScript 746 lines · 1.5K tokens
  3. Zod 457 lines · 1.4K tokens
Total205 lines · 6K tokens

CWE Coverage

  • 20
  • 73
  • 79
  • 89
  • 95
  • 117
  • 200
  • 201
  • 208
  • 209
  • 323
  • 338
  • 346
  • 347
  • 352
  • 362
  • 459
  • 471
  • 494
  • 502
  • 602
  • 770
  • 807
  • 829
  • 838
  • 862
  • 915
  • 916
  • 918
  • 922
  • 943
  • 1004
  • 1321
  • 1333

Prompt Preview

Apply the TypeScript 7 and JavaScript ES2026 prompts first. These rules cover Zod 4 object strictness, coercion, defaults, unions, refinements, transforms, serialization boundaries, errors, recursive schemas, JSON Schema, and resource limits.

Object and collection schemas

  • Use z.strictObject({...}) at security boundaries: z.object() silently strips unknown keys by default, z.looseObject() passes them through, and .catchall() accepts those matching its catchall schema.
  • Keep ownership, role, tenant, status, price, audit, and server-generated fields out of client-input schemas.
  • Set minimum and maximum lengths for strings, arrays, sets, maps, records, and binary-like data.
  • Constrain record keys and values and do not treat a generic record as a safe domain object.
  • Use separate schemas for create, update, persistence, internal state, and public output.

Parsing, coercion, and defaults

  • Use parse or safeParse at the actual runtime boundary and never rely on inferred TypeScript types alone.
  • Avoid coercion where boolean, numeric, date, identifier, or null conversions could change security meaning.
  • Account for JavaScript coercion exactly: z.coerce.string() calls String, z.coerce.number() calls Number, and z.coerce.boolean() calls Boolean, so strings such as "false" are truthy.
  • For environment-style booleans, use z.stringbool({truthy, falsy, case}) with an explicit vocabulary; its default is case-insensitive and accepts true/1/yes/on/y/enabled and corresponding false terms.
  • Do not use defaults or catch values to silently