CodeSampleX

Exemple

zod 4.4.3: Read what zod 4 safeParse returns when validation fails, and what the error surface changed from zod 3

Échantillon vérifié pour npm zod 4.4.3: Read what zod 4 safeParse returns when validation fails, and what the error surface changed from zod 3. Le contrat…

sha256:a382ae2a31a7ab492fdd32bd455c4460df064c7a5fc94698cef12db9624d30a2

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré node 22 linux x64 node 22 javascript npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-14
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18

Cas

HOW
Objectif
Read what zod 4 safeParse returns when validation fails, and what the error surface changed from zod 3
Paquets
Symboles
  • safeParse
  • ZodError.issues
  • ZodIssue.received
  • z.coerce.number
  • z.union
  • z.discriminatedUnion
  • z.email
Environnement
node 22
Créé
2026-08-14T12:07:51Z

Contrat

  1. assert safeParse returns success plus data or success plus error, and the failing result carries no data key at all
  2. assert result.error is a ZodError, is instanceof ZodError and instanceof Error, and is named ZodError
  3. assert every issue carries code, path and message, and that an object schema reports every bad field in one pass
  4. assert an invalid_type issue carries expected but no received, and that error.message is the issues array serialised as JSON
  5. assert a path through an object inside an array reports the index as a number, so a path is (string | number)[]
  6. assert dropping the non-string path segments collapses two bad array rows onto one key, turning two errors into one
  7. assert a failure with nothing to blame carries an empty path, so joining a path needs a fallback
  8. assert an out-of-range number fails with too_small carrying origin, minimum and inclusive, not invalid_type
  9. assert z.coerce.number() accepts the string 42 while z.number() rejects it as invalid_type
  10. assert z.coerce.number() also accepts empty string, whitespace, null, false and [] as 0, and rejects 1e999 because Number() overflows to Infinity
  11. assert received is carried only when the input is already the right type but an unrepresentable value (NaN, Infinity, Invalid Date), and is absent for a wrong type even under coercion
  12. assert optional() omits a missing key, keeps a key passed as undefined, and rejects null
  13. assert nullable() accepts null but still requires the key, and default() replaces undefined only, never null
  14. assert an ambiguous union fails with one invalid_union issue holding one issue group per member in declaration order, named errors and not unionErrors
  15. assert a union where exactly one member survives its type checks reports that member's issues directly, with no invalid_union wrapper
  16. assert two surviving members bring the wrapper back, so one schema returns both shapes for different bad inputs
  17. assert a member failing only a length check survives and is reported bare, while a member failing only a literal is aborted and the wrapper returns
  18. assert a tagged z.union narrows the same way z.discriminatedUnion does, and that both are the same union type underneath
  19. assert an unknown discriminator names the discriminator and lists options, while the plain union falls back to every member group
  20. assert a literal mismatch is invalid_value carrying values, not zod 3's invalid_literal carrying expected
  21. assert error.errors from zod 3 is absent rather than a deprecated alias for error.issues
  22. assert z.string().email() still exists alongside z.email() and produces an issue deep-equal to it, while z.string().ip() really was removed in favour of z.ipv4

Fichiers

  • csx.json
  • package-lock.json
  • package.json
  • src/schema.mjs
  • test/contract.mjs

Télécharger l’artefact source (tar.gz)

Seeder d'origine

anonymous