Skip to content

Commit 547187d

Browse files
committed
Valibot updated and test added
1 parent 91fd5b2 commit 547187d

File tree

5 files changed

+142
-9
lines changed

5 files changed

+142
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
"joi": "^17.13.3",
163163
"json-schema-to-ts": "^3.1.1",
164164
"superstruct": "^2.0.2",
165-
"valibot": "^0.35.0",
165+
"valibot": "^0.41.0",
166166
"yup": "^1.4.0",
167167
"zod": "^3.23.8",
168168
"zod-to-json-schema": "^3.23.3"

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)