feat(temporal-zod): support custom validation errors (#54) - #69
Open
macalinao wants to merge 1 commit into
Open
Conversation
Add an .error({ error }) method to every z<Type>/z<Type>Instance validator
that returns a copy with a customized error, taking Zod's params object
({ error } — a string or error-map function). Non-breaking: the plain
validators are unchanged and used directly as before.
- temporalValidators(cls, { extraInputs?, error? }) threads error into the
z.union + z.instanceof (what Zod 4 actually surfaces; the default error is
unchanged at "Invalid input")
- withError() augments a schema with a chainable .error() via Object.assign
(verified safe: no collision, parses in z.object, toJSONSchema intact)
- json-schemas reuses each base validator's .error() to rebuild, re-applying
JSON Schema metadata (default keeps its $def; custom variants inline an
equivalent schema)
- New public types: TemporalError, TemporalErrorParams, WithError,
TemporalValidator, TemporalInstanceValidator
- Tests cover string/function errors, nesting, unchanged defaults, and
JSON-Schema metadata; README + minor changeset updated
macalinao
force-pushed
the
feat/custom-errors
branch
from
June 29, 2026 19:47
335ce96 to
8503d33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #54.
Summary
Adds custom validation errors to
temporal-zodvia an.error()method on every validator — non-breaking, so existing code using the validators directly keeps working:The custom error surfaces for any invalid input (malformed string or wrong type), works on both the coercing and instance validators, on the main entry and
temporal-zod/base.Implementation notes
temporalValidators(cls, { extraInputs?, error? })threadserrorinto thez.union+z.instanceof— which is what Zod 4 actually surfaces (the union masks the inner transform message), so the default error is unchanged ("Invalid input").withError()augments a schema with a chainable.error()method viaObject.assign. Verified safe:z.string().errorisundefined(no collision), the augmented schema parses correctly inz.object/z.union, andz.toJSONSchemais unaffected.json-schemas.tsreuses each base validator's own.error()to rebuild, then re-applies JSON Schema metadata. The default keeps its registered$def; a custom-error variant inlines an identical schema (a distinct instance can't share the registered id — dedup is just an optimization).TemporalError,TemporalErrorParams,WithError,TemporalValidator,TemporalInstanceValidator.Verification
bun run build✅ ·bun run lint✅ ·bun test— 114 pass / 0 fail ✅z.object, unchanged default messages, and preserved JSON-Schema metadata. Existing tests use the validators directly (unchanged), confirming non-breaking.Versioned as a minor bump with a changeset (new feature, non-breaking).
Note on PR #68
The in-flight oxlint/oxfmt migration (#68) also edits
temporal-validator.ts. This branch is off currentmaster(Biome/ESLint); whichever merges first, the other needs a trivial rebase. The new code is written to satisfy both lint setups.