CodeSampleX

Пример

zod 3.24.2: Configure global and contextual error maps in Zod and verify precedence, undefined message fallback, and safeParse exception handling

Проверенный пример — npm zod 3.24.2: Configure global and contextual error maps in Zod and verify precedence, undefined message fallback, and safeParse…

sha256:f1680e0ed36414762576bfcdd9a2ce0f7d8d735b4f3e2870fa6ab3d2efd3b734

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
2
Ключи подписи, собравшие его
2
Заявленная среда node linux x64 node node npm

Среды запусков проверки

Окружение Контракт Этапы Запуск
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-17
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18

Кейс

HOW
Цель
Configure global and contextual error maps in Zod and verify precedence, undefined message fallback, and safeParse exception handling
Пакеты
Символы
  • setErrorMap
  • getErrorMap
  • defaultErrorMap
Окружение
node
Создан
2026-08-17T10:19:25Z

Контракт

  1. Returning an empty object from a custom error map leaves the issue message undefined rather than falling back to ctx.defaultError, causing error.format() and error.flatten() to produce arrays containing undefined.
  2. Global setErrorMap updates getErrorMap globally, but passing a contextual errorMap to safeParse takes precedence over global and schema-level error maps.
  3. Schema-level errorMap on an object schema handles issues for the object itself but does not cascade to child field schemas.
  4. Parse-level errorMap cascades recursively to all nested properties and child schemas.
  5. Inline check messages defined directly on validators bypass global setErrorMap.
  6. Throwing an unhandled exception inside a custom error map causes safeParse to throw synchronously instead of returning a safe error result.
  7. Passing defaultErrorMap back to setErrorMap restores standard error formatting.

Файлы

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • src/config.mjs
  • test/contract.mjs

Скачать артефакт с исходным кодом (tar.gz)

Исходный сидер

csx-seed