Gathering detailed insights and metrics for hono-zod-validator-i18n
Gathering detailed insights and metrics for hono-zod-validator-i18n
Gathering detailed insights and metrics for hono-zod-validator-i18n
Gathering detailed insights and metrics for hono-zod-validator-i18n
This library is used to translate Zod's default error messages.
npm install hono-zod-validator-i18n
Typescript
Module System
Node Version
NPM Version
72.2
Supply Chain
88.5
Quality
79.8
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
1,987
Last Day
3
Last Week
87
Last Month
289
Last Year
1,987
MIT License
4 Stars
14 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Mar 09, 2025
Minified
Minified + Gzipped
Latest Version
1.0.5
Package Id
hono-zod-validator-i18n@1.0.5
Unpacked Size
162.22 kB
Size
29.84 kB
File Count
39
NPM Version
10.8.2
Node Version
18.20.2
Published on
Sep 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
3
Compared to previous day
Last Week
70.6%
87
Compared to previous week
Last Month
1.8%
289
Compared to previous month
Last Year
0%
1,987
Compared to previous year
3
2
This library is used to translate Zod's default error messages.
1npm install hono-zod-validator-i18n @intlify/hono
This library depends on @intlify/hono
.
1import { Hono } from 'hono'; 2import { 3 defineI18nMiddleware, 4 detectLocaleFromAcceptLanguageHeader, 5 useTranslation, 6} from '@intlify/hono'; 7import { zValidatorI18n } from 'hono-zod-validator-i18n'; 8import zodEn from 'hono-zod-validator-i18n/locales/en/zod.json'; 9import zodJa from 'hono-zod-validator-i18n/locales/ja/zod.json'; 10 11const i18nMiddleware = defineI18nMiddleware({ 12 locale: detectLocaleFromAcceptLanguageHeader, 13 messages: { 14 en: { 15 hello: 'Hello {name}!', 16 zod: zodEn, 17 }, 18 ja: { 19 hello: 'こんにちは、{name}!', 20 zod: zodJa, 21 }, 22 }, 23}); 24 25const app = new Hono(); 26 27app.use('*', i18nMiddleware); 28 29const schema = z.object({ 30 name: z.string(), 31 age: z.number(), 32}); 33 34app.post('/author', zValidatorI18n('json', schema), (c) => { 35 const data = c.req.valid('json'); 36 return c.json({ 37 success: true, 38 message: `${data.name} is ${data.age}`, 39 }); 40}); 41 42export default app;
zValidatorI18n
The zValidatorI18n
function works exactly the same as the original zValidator
function from @hono/zod-validator
so you can still add a custom hook function.
1zValidatorI18n('json', schema, (result, c) => { 2 // "result" and "c" are fully typed! 3 console.log('This will run after the error translation.'); 4});
You can define custom Zod error message translations like this:
1import zodEn from 'hono-zod-validator-i18n/locales/en/zod.json';
2import zodJa from 'hono-zod-validator-i18n/locales/ja/zod.json';
3
4const i18nMiddleware = defineI18nMiddleware({
5 locale: 'ja',
6 messages: {
7 en: {
8 hello: 'Hello {name}!',
9 // All custom error message translations should be inside: zod.custom
10 zod: { ...zodEn, custom: { some_message: 'Some message' } },
11 },
12 ja: {
13 hello: 'こんにちは、{name}!',
14 zod: { ...zodJa, custom: { some_message: '何かのメッセージ' } },
15 },
16 },
17});
And use it like this:
1zValidatorI18n(
2 'json',
3 z.object({
4 name: z.string({ message: 'zod.custom.some_message' }), // Will result in: '何かのメッセージ'
5 })
6),
Custom Zod error translations only work when prefixed with zod.custom.
. If you do not use this prefix, the translation of the standard Zod error message will be used
hono-zod-validator-i18n
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.
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.
No vulnerabilities found.
No security vulnerabilities found.