Commit activity
Last 12 weeks · 206 commits
Community health
4 of 6 standards met
Recent PRs & issues
Summary Allow error maps (the param, per-parse , and ) to return a structured payload alongside , and keep it on the resulting issue. This would let i18n libraries that translate at render time (Lingui, i18next, FormatJS) carry a message id and its values through Zod without encoding them into the string. Motivation Many apps translate validation errors at render time rather than parse time: Schemas are defined once at module level and shared across packages in a monorepo, so they can't depend on the active locale. The UI should re-render existing errors when the user switches language. On the server (SSR, RSC), there is no single global i18n instance to call during . With Lingui, a message is defined as a descriptor and translated later: Today the only field an error map can set on an issue is . That leaves two workarounds, both with drawbacks. 1. Use the id as the message and read values from the issue at render time. This works with Zod's formatting helpers, but is no longer human-readable, values are limited to fields Zod puts on the issue, and anything that reduces an issue to its string (such as form-library resolvers) loses the values. 2. Encode the whole descriptor into . This survives being reduced to a string, but every consumer has to know to decode it, logs show encoded strings, and non-JSON values such as need special handling. Proposal Let an error map return an object with an extra field, for example , that Zod copies onto the issue unchanged: Consumers then translate with the existing mapper argument of the formatting helpers: Typing To make type-safe without a global registry, it could be typed via declaration merging, similar to how other libraries type user-defined metadata: An alternative is a generic on the error map and , but declaration merging avoids threading a type parameter through every schema. Precedence would follow the existing precedence chain (check, schema, per-parse, global, locale). The map that supplies the also supplies , so the two never come from different levels. Prior art Zod 3's custom issues accepted a object that error maps could read, but it applied only to refinements, not built-in checks. uses to carry translation keys, for the same reason. Alternatives considered Keep using as a carrier: works today, but as described above, every consumer must agree on an encoding and readable messages are lost. Translate at parse time inside the error map: requires a locale-aware i18n instance at parse time, which doesn't fit shared schemas or SSR and freezes the language at parse time. metadata:** attaches data to schemas, not issues, and a global registry is awkward in monorepos with multiple apps or i18n instances. Scope This is additive and backwards-compatible. Existing error maps that return a string or are unaffected, and issues without look exactly as they do today.
Recent fixes
Since 4.6.0, emits , and for , and . None of those are JSON Schema formats, so ajv warns about them. Chained after a real format, the later check also overwrote it: exported in place of , which lets accept input Zod rejects. These three checks now contribute only their , as in 4.5.x. A format set by , or similar is kept in either chain order, and / still emit their own format. The converter tells them apart by the trait rather than a list of names. Output matches across 24 schemas on , and , apart from the email regex text, which changed independently. Bundle: +12 B gzip classic, +9 B gzip mini ( fixtures). Runtime: no measurable change in (interleaved, load ~60, indicative); parse paths are untouched. Memory: no new properties or closures. Closes #6648 Thanks to @MFA-G for the original fix and test.
Since zod 4.6.0 (#6554, "derive JSON Schema constraints by folding checks in the converter"), copies every check's name into the JSON Schema keyword. For , , and that name is not a JSON Schema format, so validators such as ajv report . Reproduction on zod 4.6.5 (bun 1.4): Output: On 4.5.4 the same schemas emit only the . Expected: the alone, since it already carries the constraint, and only for names JSON Schema (or zod's documented format set) defines. Where it comes from: copies from every check, and // construct their checks with and the internal names above. Fix Branch, ready to open as a PR by a collaborator (this repository refuses PR creation from my account): https://github.com/colinhacks/zod/compare/main...Vivswan:zod:fix/json-schema-string-format-trait One change in : a check contributes a only if it carries the trait. Why that works: , and are built with , not . That is why they never wrote on 4.5.x. Test: fails on with received, passes on the branch. Full suite green. Bundle: unchanged unless is used, then +31 gzip bytes classic, +30 mini. Trait read, not :** pulls into mini bundles that never build a format check (+89 bytes).
In the Armenian () locale, an issue with a single expected value reads (, line 125), so the message names : Every other locale reads there. A one-line fix with a test () is ready on https://github.com/bokelley/zod/tree/fix/hy-locale-invalid-value (commit https://github.com/bokelley/zod/commit/d7bc23d). I tried to open it as a PR but the repository didn't allow it from my account. Happy to open it if PRs can be enabled for me, or feel free to take the commit.
Repository: colinhacks/zod. Description: TypeScript-first schema validation with static type inference Stars: 44324, Forks: 2342. Primary language: TypeScript. Languages: TypeScript (89.3%), MDX (9.7%), JavaScript (0.5%), HTML (0.5%), CSS (0.1%). License: MIT. Homepage: https://zod.dev Topics: runtime-validation, schema-validation, static-types, type-inference, typescript. Latest release: v4.6.7 (1mo ago). Open PRs: 17, open issues: 104. Last activity: 3h ago. Community health: 85%. Top contributors: colinhacks, JacobWeisenburger, scotttrinh, pullfrog[bot], jeremyBanks, samchungy, igalklebanov, tmcw, noritaka1166, alexxander and others.