Skip to content

Commit 1b7a158

Browse files
committed
Added test for zod-i18n-map
1 parent 5279de0 commit 1b7a158

File tree

5 files changed

+161
-6
lines changed

5 files changed

+161
-6
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
"eslint-config-prettier": "^9.1.0",
181181
"eslint-plugin-dci-lint": "^0.3.2",
182182
"eslint-plugin-svelte": "2.36.0-next.13",
183+
"i18next": "^23.12.3",
183184
"only-allow": "^1.2.1",
184185
"prettier": "^3.3.3",
185186
"prettier-plugin-svelte": "^3.2.6",
@@ -195,7 +196,8 @@
195196
"typescript": "^5.5.4",
196197
"uuid": "^9.0.1",
197198
"vite": "^5.4.0",
198-
"vitest": "^1.6.0"
199+
"vitest": "^1.6.0",
200+
"zod-i18n-map": "^2.27.0"
199201
},
200202
"svelte": "./dist/index.js",
201203
"types": "./dist/index.d.ts",

pnpm-lock.yaml

Lines changed: 26 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Actions, PageServerLoad } from './$types.js';
2+
3+
import { superValidate, message } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import { fail } from '@sveltejs/kit';
6+
import { schema } from './schema.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
return {
10+
form: await superValidate(zod(schema))
11+
};
12+
};
13+
14+
export const actions: Actions = {
15+
default: async ({ request }) => {
16+
const form = await superValidate(request, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Form posted successfully!');
22+
}
23+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm } from '$lib/index.js';
4+
import SuperDebug from '$lib/index.js';
5+
6+
export let data;
7+
8+
const { form, errors, message, enhance } = superForm(data.form);
9+
</script>
10+
11+
<SuperDebug data={$form} />
12+
13+
<h3>when using zod-i18n-map, field errors are [undefined]</h3>
14+
15+
<p>Submit to see localized errors.</p>
16+
17+
{#if $message}
18+
<!-- eslint-disable-next-line svelte/valid-compile -->
19+
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
20+
{$message}
21+
</div>
22+
{/if}
23+
24+
<form method="POST" use:enhance>
25+
<label>
26+
Name<br />
27+
<input name="name" aria-invalid={$errors.name ? 'true' : undefined} bind:value={$form.name} />
28+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
29+
</label>
30+
31+
<label>
32+
Email<br />
33+
<input
34+
name="email"
35+
type="email"
36+
aria-invalid={$errors.email ? 'true' : undefined}
37+
bind:value={$form.email}
38+
/>
39+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
40+
</label>
41+
42+
<button>Submit</button>
43+
</form>
44+
45+
<hr />
46+
<p>
47+
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥
48+
</p>
49+
50+
<style>
51+
.invalid {
52+
color: red;
53+
}
54+
55+
.status {
56+
color: white;
57+
padding: 4px;
58+
padding-left: 8px;
59+
border-radius: 2px;
60+
font-weight: 500;
61+
}
62+
63+
.status.success {
64+
background-color: seagreen;
65+
}
66+
67+
.status.error {
68+
background-color: #ff2a02;
69+
}
70+
71+
input {
72+
background-color: #ddd;
73+
}
74+
75+
a {
76+
text-decoration: underline;
77+
}
78+
79+
hr {
80+
margin-top: 4rem;
81+
}
82+
83+
form {
84+
padding-top: 1rem;
85+
padding-bottom: 1rem;
86+
}
87+
</style>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import i18next from 'i18next';
2+
import { z } from 'zod';
3+
import { zodI18nMap } from 'zod-i18n-map';
4+
import translation from 'zod-i18n-map/locales/es/zod.json';
5+
6+
i18next.init({
7+
lng: 'es',
8+
resources: {
9+
es: { zod: translation }
10+
}
11+
});
12+
z.setErrorMap(zodI18nMap);
13+
14+
export const schema = z.object({
15+
name: z.string().min(2),
16+
email: z.string().email()
17+
});
18+
19+
const data = schema.safeParse({ name: '', email: '' });
20+
if (!data.success) {
21+
console.dir(data.error.flatten(), { depth: 10 }); //debug
22+
}

0 commit comments

Comments
 (0)