Repository navigation
Zod: Detect Configured Locale for Custom Messages - #890
Merged
Merged
Conversation
Replace the internal setLang() with detection of the locale Zod is configured with. Zod only stores the configured locale's error map, so getLocale() probes it with a known issue and compares the message to the ones our supported locales give, caching the result per error map. Detection runs lazily when an issue is finalized, so our custom messages appear for Zod Classic's default en locale or after z.config(en()) in Zod Mini, regardless of whether z.config() runs before or after import. With no locale or an unsupported one, Zod's own messages are used. Supported locales are typed as a subset of the locale names Zod exports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Bb6sv3VomENtuqfvmd7rcF Signed-off-by: Claude <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 3 files with indirect coverage changes 🚀 New features to boost your workflow:
|
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.
Description
Replaces the internal
setLang()in the Zod package with automatic detection of the locale Zod is configured with, so users get our custom error messages without calling a second function.How it works
Zod doesn't record which locale was passed to
z.config(). It only keeps that locale'slocaleErrormap, and eachen()call returns a new closure, so we can't compare by identity. Instead,getLocale()inlang/_internal.ts:z.config().localeErrorwhen an issue is being finalized (so it's lazy, not run at import).invalid_type, expectedstring, input0) and compares the message with what each supported Zod locale returns for that issue.WeakMap, so each configured locale is only probed once.Results:
enset by default when the first schema is built) → our English messagesz.config(en())→ our English messages"Invalid input") is usedja) or a customlocaleError→ Zod's locale message is usedBecause detection happens when parsing,
z.config()can be called before or after this package is imported, and changing locales later is picked up.Locale typing
Zod doesn't export a union type of its locale tags, but it does export each locale by name from
zod/locales. OurLocaletype is nowExtract<keyof typeof locales, "en">, a subset of Zod's own locale names. If Zod ever renamed one, it would show up as a type error.Other changes
setLangis removed. It was only exported from the internallang/index.tsand never from the package entrypoints, so the public API doesn't change.setLang("en").reviews.test.tsnow callsz.config(en())like the other test files that check messages.tests/v20170710/lang.test.tscovers detection (none /en/ Zod Classic default / unsupported locale / custom maps, including{ message }objects andnull/ switching locales at runtime) and checks all three custom messages through the real schemas.src/v20170710/lang/**is narrowed to the Valibot package, so the Zodlangfolder now counts toward coverage (it's fully covered).Behavior change to note: Zod Mini users who haven't configured any locale used to get our English messages (because
langdefaulted to English). They now get Zod's generic"Invalid input", which matches how Zod Mini behaves elsewhere.Nature of Pull Request
This Pull Request:
Its changes constitutes a:
Contribution Terms
I understand that by submitting this Pull Request:
🤖 Generated with Claude Code
https://claude.ai/code/session_01Bb6sv3VomENtuqfvmd7rcF
Generated by Claude Code