Gathering detailed insights and metrics for zod-i18n-map
Gathering detailed insights and metrics for zod-i18n-map
Gathering detailed insights and metrics for zod-i18n-map
Gathering detailed insights and metrics for zod-i18n-map
@crabas0npm2/voluptate-aut-ullam
security holding package
@crabas0npm2/perspiciatis-voluptate-similique
security holding package
@taktikorg/unde-animi-omnis
<p align="center"> <a href="https://www.npmjs.com/package/@taktikorg/unde-animi-omnis"><img src="https://img.shields.io/npm/v/@taktikorg/unde-animi-omnis"></a> <a href=""><img src="https://img.shields.io/github/actions/workflow/status/RemiMyrset/@taktikor
@taktikorg/sapiente-quas-velit
[![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url]
npm install zod-i18n-map
Typescript
Module System
Node Version
NPM Version
99.4
Supply Chain
99.6
Quality
76.1
Maintenance
100
Vulnerability
100
License
zod-i18n-map-v2.27.0
Updated on Jan 30, 2024
zod-i18n-map-v2.26.0
Updated on Jan 26, 2024
zod-i18n-map-v2.25.0
Updated on Jan 08, 2024
zod-i18n-map-v2.24.0
Updated on Jan 08, 2024
zod-i18n-map-v2.23.0
Updated on Dec 13, 2023
zod-i18n-map-v2.22.0
Updated on Dec 07, 2023
TypeScript (99.93%)
Shell (0.07%)
Total Downloads
4,548,541
Last Day
15,859
Last Week
76,398
Last Month
377,744
Last Year
3,273,291
MIT License
766 Stars
422 Commits
77 Forks
2 Watchers
44 Branches
38 Contributors
Updated on May 05, 2025
Minified
Minified + Gzipped
Latest Version
2.27.0
Package Id
zod-i18n-map@2.27.0
Unpacked Size
190.78 kB
Size
31.83 kB
File Count
37
NPM Version
8.19.4
Node Version
18.19.0
Published on
Jan 30, 2024
Cumulative downloads
Total Downloads
Last Day
-15.9%
15,859
Compared to previous day
Last Week
-22.4%
76,398
Compared to previous week
Last Month
-1.7%
377,744
Compared to previous month
Last Year
175%
3,273,291
Compared to previous year
This library is used to translate Zod's default error messages.
1yarn add zod-i18n-map i18next
This library depends on i18next
.
1import i18next from "i18next"; 2import { z } from "zod"; 3import { zodI18nMap } from "zod-i18n-map"; 4// Import your language translation files 5import translation from "zod-i18n-map/locales/es/zod.json"; 6 7// lng and resources key depend on your locale. 8i18next.init({ 9 lng: "es", 10 resources: { 11 es: { zod: translation }, 12 }, 13}); 14z.setErrorMap(zodI18nMap); 15 16// export configured zod instance 17export { z }
Import wherever it's required
1import { z } from "./es-zod"; 2 3const schema = z.string().email(); 4// Translated into Spanish (es) 5schema.parse("foo"); // => correo inválido
makeZodI18nMap
Detailed customization is possible by using makeZodI18nMap
and option values.
1export type MakeZodI18nMap = (option?: ZodI18nMapOption) => ZodErrorMap; 2 3export type ZodI18nMapOption = { 4 t?: i18n["t"]; 5 ns?: string | readonly string[]; // See: `Namespace` 6 handlePath?: { 7 // See: `Handling object schema keys` 8 keyPrefix?: string; 9 }; 10};
ns
)You can switch between translation files by specifying a namespace. This is useful in cases where the application handles validation messages for different purposes, e.g., validation messages for forms are for end users, while input value checks for API schemas are for developers.
The default namespace is zod
.
1import i18next from "i18next"; 2import { z } from "zod"; 3import { makeZodI18nMap } from "zod-i18n-map"; 4 5i18next.init({ 6 lng: "en", 7 resources: { 8 en: { 9 zod: { 10 // default namespace 11 invalid_type: "Error: expected {{expected}}, received {{received}}", 12 }, 13 formValidation: { 14 // custom namespace 15 invalid_type: 16 "it is expected to provide {{expected}} but you provided {{received}}", 17 }, 18 }, 19 }, 20}); 21 22// use default namespace 23z.setErrorMap(makeZodI18nMap()); 24z.string().parse(1); // => Error: expected string, received number 25 26// select custom namespace 27z.setErrorMap(makeZodI18nMap({ ns: "formValidation" })); 28z.string().parse(1); // => it is expected to provide string but you provided number
📝 You can also specify multiple namespaces in an array.
Messages using {{maximum}}
, {{minimum}}
or {{keys}}
can be converted to the plural form.
Keys are i18next compliant. (https://www.i18next.com/translation-function/plurals)
1{ 2 "exact_one": "String must contain exactly {{minimum}} character", 3 "exact_other": "String must contain exactly {{minimum}} characters" 4}
1import i18next from "i18next"; 2import { z } from "zod"; 3import { zodI18nMap } from "zod-i18n-map"; 4 5i18next.init({ 6 lng: "en", 7 resources: { 8 en: { 9 zod: { 10 errors: { 11 too_big: { 12 string: { 13 exact_one: "String must contain exactly {{maximum}} character", 14 exact_other: "String must contain exactly {{maximum}} characters", 15 }, 16 }, 17 }, 18 }, 19 }, 20 }, 21}); 22 23z.setErrorMap(zodI18nMap); 24 25z.string().length(1).safeParse("abc"); // => String must contain exactly 1 character 26 27z.string().length(5).safeParse("abcdefgh"); // => String must contain exactly 5 characters
You can translate also custom errors, for example errors from refine
.
Create a key for the custom error in a namespace and add i18n
to the refine second arg(see example)
1import i18next from "i18next"; 2import { z } from "zod"; 3import { makeZodI18nMap } from "zod-i18n-map"; 4import translation from "zod-i18n-map/locales/en/zod.json"; 5 6i18next.init({ 7 lng: "en", 8 resources: { 9 en: { 10 zod: translation, 11 custom: { 12 my_error_key: "Something terrible", 13 my_error_key_with_value: "Something terrible {{msg}}", 14 }, 15 }, 16 }, 17}); 18 19z.setErrorMap(makeZodI18nMap({ ns: ["zod", "custom"] })); 20 21z.string() 22 .refine(() => false, { params: { i18n: "my_error_key" } }) 23 .safeParse(""); // => Something terrible 24 25// Or 26 27z.string() 28 .refine(() => false, { 29 params: { 30 i18n: { key: "my_error_key_with_value", values: { msg: "happened" } }, 31 }, 32 }) 33 .safeParse(""); // => Something terrible happened
handlePath
)When dealing with structured data, such as when using Zod as a validator for form input values, it is common to generate a schema with z.object
.
You can handle the object's key in the message by preparing messages with the key in the with_path
context.
1import i18next from "i18next"; 2import { z } from "zod"; 3import { zodI18nMap } from "zod-i18n-map"; 4 5i18next.init({ 6 lng: "en", 7 resources: { 8 en: { 9 zod: { 10 errors: { 11 invalid_type: "Expected {{expected}}, received {{received}}", 12 invalid_type_with_path: 13 "{{path}} is expected {{expected}}, received {{received}}", 14 }, 15 userName: "User's name", 16 }, 17 }, 18 }, 19}); 20 21z.setErrorMap(zodI18nMap); 22 23z.string().parse(1); // => Expected string, received number 24 25const schema = z.object({ 26 userName: z.string(), 27}); 28schema.parse({ userName: 1 }); // => User's name is expected string, received number
If _with_path
is suffixed to the key of the message, that message will be adopted in the case of an object type schema.
If there is no message key with _with_path
, fall back to the normal error message.
Object schema keys can be handled in the message with {{path}}
.
By preparing the translated data for the same key as the key in the object schema, the translated value will be output in {{path}}
, otherwise the key will be output as is.
It is possible to access nested translation data by specifying handlePath.keyPrefix
.
1i18next.init({ 2 lng: "en", 3 resources: { 4 en: { 5 zod: { 6 errors: { 7 invalid_type: "Expected {{expected}}, received {{received}}", 8 invalid_type_with_path: 9 "{{- path}} is expected {{expected}}, received {{received}}", 10 }, 11 }, 12 form: { 13 paths: { 14 userName: "User's name", 15 }, 16 }, 17 }, 18 }, 19}); 20 21z.setErrorMap( 22 zodI18nMap({ 23 ns: ["zod", "form"], 24 handlePath: { 25 keyPrefix: "paths", 26 }, 27 }) 28);
zod-i18n-map
contains translation files for several locales.
It is also possible to create and edit translation files. You can use this English translation file as a basis for rewriting it in your language.
If you have created a translation file for a language not yet in the repository, please send us a pull request.
next-i18next
Many users will want to use it with next-i18next
(i.e. on Next.js). This example summarizes how to use with it.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details
No vulnerabilities found.
No security vulnerabilities found.