@autotranslate/zod
v1.0.0-beta.10
Published
Zod v4 error map: translates standard Zod issues through your autotranslate catalog.
Maintainers
Readme
@autotranslate/zod
Zod v4 error map that translates standard issues through your autotranslate
catalog.
pnpm add @autotranslate/zodUse it
import { z } from 'zod';
import { zodErrorMap } from '@autotranslate/zod';
z.config({ customError: zodErrorMap });Validation errors now flow through the active translator. Bind one with
withTranslator(translator, fn) for request-scoped servers, or
bindTranslator(translator) once at SPA boot.
Pipe keys through your catalog
Add the package's source module to your content glob so the standard issue
keys land in .translations/{locale}.json and get translated:
// autotranslate.config.ts
defineConfig({
content: ['src/**/*.{ts,tsx}', '@autotranslate/zod/source'],
// …
});Until you run translate, the package's bundled English catalog fills the gap.
Customize
One schema's message
import { t } from '@autotranslate/core/t';
z.string().min(8, { error: () => t('Use at least 8 characters') });One of our keys
Edit it in your catalog. Yours wins; ours is a fallback.
{
"zod.too_small.array": "{minimum, plural, =1 {Pick at least one} other {Pick at least #}}",
}Custom refinements
z.string().refine(isStrong, { error: () => t('That username is taken') });Codes we don't translate
invalid_union, invalid_key, invalid_element, custom — we return
undefined, Zod chains to z.locales.*():
z.config({
customError: zodErrorMap,
localeError: z.locales.fr().localeError,
});API
import {
createZodErrorMap,
zodErrorMap,
issueToLookup,
} from '@autotranslate/zod';
import { withRequestTranslator } from '@autotranslate/zod/next';
// or '@autotranslate/zod/remix'| Export | Notes |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| zodErrorMap | Reads the ambient translator. Pair with withTranslator / bindTranslator. |
| createZodErrorMap(translator \| options) | Bind explicitly. Right for tests and programmatic flows. |
| issueToLookup(issue) | Pure function: $ZodRawIssue → { key, params } \| undefined. Useful for reusing the mapping in your own error pipelines. |
| withRequestTranslator | Adapter wrapping a Next or Remix request handler in a translator scope. |
License
MIT © Tamim Bin Hakim and contributors.
