Skip to content

Commit

Permalink
Added test for zod-i18n-map
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoheat committed Aug 13, 2024
1 parent 5279de0 commit 1b7a158
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-dci-lint": "^0.3.2",
"eslint-plugin-svelte": "2.36.0-next.13",
"i18next": "^23.12.3",
"only-allow": "^1.2.1",
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.2.6",
Expand All @@ -195,7 +196,8 @@
"typescript": "^5.5.4",
"uuid": "^9.0.1",
"vite": "^5.4.0",
"vitest": "^1.6.0"
"vitest": "^1.6.0",
"zod-i18n-map": "^2.27.0"
},
"svelte": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
31 changes: 26 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/routes/(v2)/v2/issue-455/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Actions, PageServerLoad } from './$types.js';

import { superValidate, message } from '$lib/index.js';
import { zod } from '$lib/adapters/zod.js';
import { fail } from '@sveltejs/kit';
import { schema } from './schema.js';

export const load: PageServerLoad = async () => {
return {
form: await superValidate(zod(schema))
};
};

export const actions: Actions = {
default: async ({ request }) => {
const form = await superValidate(request, zod(schema));
console.log(form);

if (!form.valid) return fail(400, { form });

return message(form, 'Form posted successfully!');
}
};
87 changes: 87 additions & 0 deletions src/routes/(v2)/v2/issue-455/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<script lang="ts">
import { page } from '$app/stores';
import { superForm } from '$lib/index.js';
import SuperDebug from '$lib/index.js';
export let data;
const { form, errors, message, enhance } = superForm(data.form);
</script>

<SuperDebug data={$form} />

<h3>when using zod-i18n-map, field errors are [undefined]</h3>

<p>Submit to see localized errors.</p>

{#if $message}
<!-- eslint-disable-next-line svelte/valid-compile -->
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
{$message}
</div>
{/if}

<form method="POST" use:enhance>
<label>
Name<br />
<input name="name" aria-invalid={$errors.name ? 'true' : undefined} bind:value={$form.name} />
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
</label>

<label>
Email<br />
<input
name="email"
type="email"
aria-invalid={$errors.email ? 'true' : undefined}
bind:value={$form.email}
/>
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
</label>

<button>Submit</button>
</form>

<hr />
<p>
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥
</p>

<style>
.invalid {
color: red;
}
.status {
color: white;
padding: 4px;
padding-left: 8px;
border-radius: 2px;
font-weight: 500;
}
.status.success {
background-color: seagreen;
}
.status.error {
background-color: #ff2a02;
}
input {
background-color: #ddd;
}
a {
text-decoration: underline;
}
hr {
margin-top: 4rem;
}
form {
padding-top: 1rem;
padding-bottom: 1rem;
}
</style>
22 changes: 22 additions & 0 deletions src/routes/(v2)/v2/issue-455/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import i18next from 'i18next';
import { z } from 'zod';
import { zodI18nMap } from 'zod-i18n-map';
import translation from 'zod-i18n-map/locales/es/zod.json';

i18next.init({
lng: 'es',
resources: {
es: { zod: translation }
}
});
z.setErrorMap(zodI18nMap);

export const schema = z.object({
name: z.string().min(2),
email: z.string().email()
});

const data = schema.safeParse({ name: '', email: '' });
if (!data.success) {
console.dir(data.error.flatten(), { depth: 10 }); //debug
}

0 comments on commit 1b7a158

Please sign in to comment.