Skip to content

Commit 611de79

Browse files
committed
Added test for issue 366
1 parent 52410a5 commit 611de79

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
'issue-358',
4242
'issue-360',
4343
'unknown-in-schema',
44-
'issue-374'
44+
'issue-374',
45+
'issue-366'
4546
].sort();
4647
</script>
4748

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { valibot } from '$lib/adapters/valibot.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { userSchema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(valibot(userSchema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, valibot(userSchema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import { valibot } from '$lib/adapters/valibot.js';
5+
import { userSchema } from './schema.js';
6+
import { pick } from 'valibot';
7+
8+
export let data;
9+
10+
const userAuthSchema = pick(userSchema, ['email']);
11+
//type UserAuthSchema = typeof userAuthSchema;
12+
13+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
14+
taintedMessage: false,
15+
validators: valibot(userAuthSchema)
16+
});
17+
</script>
18+
19+
<SuperDebug data={{ $form, $errors, $tainted }} />
20+
21+
{#if $message}<h4>{$message}</h4>{/if}
22+
23+
<form method="POST" use:enhance>
24+
<label>
25+
Email: <input
26+
name="email"
27+
bind:value={$form.email}
28+
aria-invalid={$errors.email ? 'true' : undefined}
29+
/>
30+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
31+
</label>
32+
<div>
33+
<button>Submit</button>
34+
</div>
35+
</form>
36+
37+
<style lang="scss">
38+
form {
39+
margin: 2rem 0;
40+
41+
input {
42+
background-color: #dedede;
43+
}
44+
45+
.invalid {
46+
color: crimson;
47+
}
48+
}
49+
</style>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
string,
3+
email,
4+
object,
5+
boolean,
6+
minLength,
7+
toTrimmed,
8+
picklist,
9+
literal,
10+
date
11+
} from 'valibot';
12+
13+
const roleOptions = ['USER', 'PREMIUM', 'ADMIN'] as const;
14+
15+
export const userSchema = object({
16+
firstName: string([toTrimmed(), minLength(1, 'Please enter your first name.')]),
17+
lastName: string([toTrimmed(), minLength(1, 'Please enter your last name.')]),
18+
email: string([
19+
toTrimmed(),
20+
email('The email address is not valid.'),
21+
minLength(1, 'An email address is required.')
22+
]),
23+
role: picklist(roleOptions, 'You must have a role.'),
24+
verified: boolean(),
25+
terms: literal(true, 'You must accept the terms and privacy policy.'),
26+
receiveEmail: boolean(),
27+
createdAt: date(),
28+
updatedAt: date()
29+
});
30+
31+
export const emailSchema = object({
32+
email: string([
33+
toTrimmed(),
34+
email('The email address is not valid.'),
35+
minLength(1, 'An email address is required.')
36+
])
37+
});
38+
39+
export type UserSchema = typeof userSchema;
40+
export type emailSchema = typeof emailSchema;

0 commit comments

Comments
 (0)