Skip to content

Commit

Permalink
Merge pull request #22 from aiji42/feature
Browse files Browse the repository at this point in the history
support translations for zod v3.20
  • Loading branch information
aiji42 authored Dec 13, 2022
2 parents 6d5ccd2 + 07d04fd commit 0b1ca69
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 178 deletions.
16 changes: 8 additions & 8 deletions examples/with-next-i18next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
"lint": "next lint"
},
"dependencies": {
"@chakra-ui/react": "^2.4.2",
"@chakra-ui/react": "^2.4.3",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@hookform/resolvers": "^2.9.10",
"framer-motion": "^7.6.18",
"i18next": "^22.0.6",
"framer-motion": "^7.7.3",
"i18next": "^22.4.5",
"next": "13.0.6",
"next-i18next": "^13.0.0",
"next-i18next": "^13.0.1",
"nookies": "^2.5.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "7.40.0",
"react-i18next": "^12.0.0",
"zod": "^3.19.1",
"react-i18next": "^12.1.1",
"zod": "3.20.2",
"zod-i18n-map": "*"
},
"devDependencies": {
"@types/node": "18.11.10",
"@types/node": "18.11.14",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.9",
"eslint": "8.29.0",
"eslint-config-next": "13.0.6",
"typescript": "4.9.3"
"typescript": "4.9.4"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"husky": "^8.0.2",
"lint-staged": "^13.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.0",
"prettier": "^2.8.1",
"semantic-release": "^19.0.5",
"semantic-release-monorepo": "^7.0.5"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/core/locales/ar/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"custom": "مدخل غير صالح",
"invalid_intersection_types": "تعذر دمج نتائج التقاطع",
"not_multiple_of": "يجب أن يكون الرقم من مضاعفات {{multipleOf}}",
"not_finite": "يجب ان يكون العدد محدود",
"invalid_string": {
"email": "غير صالح {{validation}}",
"url": "غير صالح {{validation}}",
"uuid": "غير صالح {{validation}}",
"cuid": "غير صالح {{validation}}",
"regex": "غير صالح",
"datetime": "غير صالح {{validation}}",
"startsWith": "إدخال غير صالح: يجب أن يبدأ بـ {{startsWith}}",
"endsWith": "إدخال غير صالح: يجب أن ينتهي بـ {{endsWith}}"
},
Expand Down Expand Up @@ -72,7 +74,8 @@
"url": "عنوان url",
"uuid": "uuid",
"cuid": "cuid",
"regex": "التعبير النمطي"
"regex": "التعبير النمطي",
"datetime": "التاريخ والوقت"
},
"types": {
"function": "دالة",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/locales/en/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"custom": "Invalid input",
"invalid_intersection_types": "Intersection results could not be merged",
"not_multiple_of": "Number must be a multiple of {{multipleOf}}",
"not_finite": "Number must be finite",
"invalid_string": {
"email": "Invalid {{validation}}",
"url": "Invalid {{validation}}",
"uuid": "Invalid {{validation}}",
"cuid": "Invalid {{validation}}",
"regex": "Invalid",
"datetime": "Invalid {{validation}}",
"startsWith": "Invalid input: must start with {{startsWith}}",
"endsWith": "Invalid input: must end with {{endsWith}}"
},
Expand Down Expand Up @@ -72,7 +74,8 @@
"url": "url",
"uuid": "uuid",
"cuid": "cuid",
"regex": "regex"
"regex": "regex",
"datetime": "datetime"
},
"types": {
"function": "function",
Expand Down
6 changes: 4 additions & 2 deletions packages/core/locales/fr/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"custom": "Champ invalide",
"invalid_intersection_types": "Les résultats d'intersection n'ont pas pu être fusionnés",
"not_multiple_of": "Nombre doit être multiple de {{multipleOf}}",
"not_finite": "Nombre doit être fini",
"invalid_string": {
"email": "{{validation}} invalide",
"url": "{{validation}} invalide",
"uuid": "{{validation}} invalide",
"cuid": "{{validation}} invalide",
"regex": "{{validation}} invalide",
"datetime": "{{validation}} invalide",
"startsWith": "Champ invalide: doit commencer par {{startsWith}}",
"endsWith": "Champ invalide: doit se terminer par {{endsWith}}"
},
Expand Down Expand Up @@ -72,7 +74,8 @@
"url": "lien",
"uuid": "UUID",
"cuid": "CUID",
"regex": "expression régulière"
"regex": "expression régulière",
"datetime": "horodate"
},
"types": {
"function": "fonction",
Expand All @@ -97,4 +100,3 @@
"set": "ensemble"
}
}

5 changes: 4 additions & 1 deletion packages/core/locales/ja/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"custom": "入力形式が間違っています。",
"invalid_intersection_types": "交差型のマージができませんでした。",
"not_multiple_of": "{{multipleOf}}の倍数である必要があります。",
"not_finite": "有限数である必要があります。",
"invalid_string": {
"email": "{{validation}}の形式で入力してください。",
"url": "{{validation}}の形式で入力してください。",
"uuid": "{{validation}}の形式で入力してください。",
"cuid": "{{validation}}の形式で入力してください。",
"regex": "入力形式が間違っています。",
"datetime": "{{validation}}の形式で入力してください。",
"startsWith": "{{startsWith}}で始まる文字列である必要があります。",
"endsWith": "{{endsWith}}で終わる文字列である必要があります。"
},
Expand Down Expand Up @@ -77,7 +79,8 @@
"url": "URL",
"uuid": "UUID",
"cuid": "CUID",
"regex": "正規表現"
"regex": "正規表現",
"datetime": "日時"
},
"types": {
"function": "関数",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/locales/pt/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"custom": "Entrada inválida",
"invalid_intersection_types": "Valores de interseção não poderam ser mesclados",
"not_multiple_of": "O número deverá ser múltiplo de {{multipleOf}}",
"not_finite": "Número não pode ser infinito",
"invalid_string": {
"email": "E-mail inválido",
"url": "URL inválida",
"uuid": "UUID inválido",
"cuid": "CUID inválido",
"regex": "Combinação inválida",
"datetime": "datetime inválido",
"startsWith": "Entrada inválida: precisa iniciar com {{startsWith}}",
"endsWith": "Entrada inválida: precisa terminar com {{endsWith}}"
},
Expand Down Expand Up @@ -72,7 +74,8 @@
"url": "url",
"uuid": "uuid",
"cuid": "cuid",
"regex": "regex"
"regex": "regex",
"datetime": "datetime"
},
"types": {
"function": "function",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/locales/zh-CN/zod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"custom": "格式错误",
"invalid_intersection_types": "交集类型无法合并",
"not_multiple_of": "必须是 {{multipleOf}} 的倍数",
"not_finite": "不能为无限值",
"invalid_string": {
"email": "{{validation}}格式错误",
"url": "{{validation}} 格式错误",
"uuid": "{{validation}} 格式错误",
"cuid": "{{validation}} 格式错误",
"regex": "格式错误",
"datetime": "{{validation}} 格式错误",
"startsWith": "必须以 {{startsWith}} 起始",
"endsWith": "必须以 {{endsWith}} 结尾"
},
Expand Down Expand Up @@ -72,7 +74,8 @@
"url": "url",
"uuid": "uuid",
"cuid": "cuid",
"regex": "正则表达式"
"regex": "正则表达式",
"datetime": "datetime"
},
"types": {
"function": "函数",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
},
"devDependencies": {
"@tsconfig/node16": "^1.0.3",
"@types/node": "^18.11.10",
"@vitest/coverage-c8": "^0.25.3",
"@types/node": "^18.11.14",
"@vitest/coverage-c8": "^0.25.8",
"c8": "^7.12.0",
"i18next": "^22.0.6",
"i18next": "^22.4.5",
"rimraf": "^3.0.2",
"tsup": "^6.5.0",
"typescript": "^4.9.3",
"vite": "^3.2.4",
"vitest": "^0.25.3",
"zod": "^3.19.1"
"typescript": "^4.9.4",
"vite": "^4.0.1",
"vitest": "^0.25.8",
"zod": "^3.20.2"
}
}
5 changes: 5 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export const makeZodI18nMap =
defaultValue: message,
});
break;
case ZodIssueCode.not_finite:
message = t("zod:errors.not_finite", {
defaultValue: message,
});
break;
default:
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/integrations/ar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"يجب أن تحتوي السلسلة على 5 حرف (أحرف) كحد أقصى"
);
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("غير صالح التاريخ والوقت");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -79,6 +82,9 @@ test("number parser error messages", () => {
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual(
"يجب أن يكون الرقم أكبر من 0"
);
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"يجب ان يكون العدد محدود"
);
});

test("date parser error messages", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/integrations/en.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"String must contain at most 5 character(s)"
);
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("Invalid datetime");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -75,6 +78,9 @@ test("number parser error messages", () => {
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual(
"Number must be greater than 0"
);
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"Number must be finite"
);
});

test("date parser error messages", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/integrations/fr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"Champ de text doit contenir au plus 5 caractère(s)"
);
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("horodate invalide");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -77,6 +80,9 @@ test("number parser error messages", () => {
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual(
"Nombre doit être supérieur à 0"
);
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"Nombre doit être fini"
);
});

test("date parser error messages", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/integrations/ja.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"5文字以下の文字列である必要があります。"
);
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("日時の形式で入力してください。");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -79,6 +82,9 @@ test("number parser error messages", () => {
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual(
"0より大きな数値である必要があります。"
);
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"有限数である必要があります。"
);
});

test("date parser error messages", () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/tests/integrations/pt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"A string pode conter no máximo 5 caracter(es)"
);
// TODO: translation `datetime` (zod:validations.datetime and zod:errors.invalid_string.datetime)
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("datetime inválido");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -77,6 +81,9 @@ test("number parser error messages", () => {
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual(
"O número precisa ser maior que 0"
);
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"Número não pode ser infinito"
);
});

test("date parser error messages", () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/tests/integrations/zh-CN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ test("string parser error messages", () => {
expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual(
"最多只能包含 5 个字符"
);
// TODO: translation `datetime` (zod:validations.datetime)
expect(
getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00"))
).toEqual("datetime 格式错误");
});

test("number parser error messages", () => {
Expand Down Expand Up @@ -69,6 +73,9 @@ test("number parser error messages", () => {
"必须大于或等于 5"
);
expect(getErrorMessage(schema.positive().safeParse(0))).toEqual("必须大于 0");
expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual(
"不能为无限值"
);
});

test("date parser error messages", () => {
Expand Down
Loading

1 comment on commit 0b1ca69

@vercel
Copy link

@vercel vercel bot commented on 0b1ca69 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zod-i18n – ./

zod-i18n.vercel.app
zod-i18n-aiji42.vercel.app
zod-i18n-git-main-aiji42.vercel.app

Please sign in to comment.