Skip to content

Commit e07162f

Browse files
committed
schemasafe test
1 parent 00b02a3 commit e07162f

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/routes/(v2)/v2/schemasafe-types/+page.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export const load = async () => {
1212

1313
export const actions = {
1414
default: async ({ request }) => {
15+
const formData = await request.formData();
16+
1517
const adapter = schemasafe(schema);
1618
const adapter2 = schemasafe(constSchema);
17-
const form = await superValidate(request, adapter);
18-
const form2 = await superValidate(request, adapter2);
19+
const form = await superValidate(formData, adapter);
20+
const form2 = await superValidate(formData, adapter2);
1921

2022
if (!form.valid || !form2.valid) {
2123
return fail(400, { form });

src/routes/(v2)/v2/schemasafe-types/+page.svelte

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,33 @@
55
66
export let data;
77
8-
const { form } = superForm(data.form);
8+
const { form, errors, enhance } = superForm(data.form, { taintedMessage: null });
99
const { form: form2 } = superForm(data.constForm, {
10-
validators: schemasafeClient(schema)
10+
validators: schemasafeClient(schema),
11+
taintedMessage: null
1112
});
12-
const name: unknown = $form.name;
13-
const name2: string = $form2.name;
13+
$: name = $form.name;
14+
$: name2 = $form2.name;
1415
</script>
1516

16-
<form method="POST">
17-
<label for="name">Name ({name} {name2})</label>
18-
<input type="text" name="name" bind:value={$form.name} />
17+
<form method="POST" use:enhance>
18+
<label for="name">Name ({name}{name2})</label>
19+
<input
20+
type="text"
21+
name="name"
22+
bind:value={$form.name}
23+
aria-invalid={$errors.name ? 'true' : undefined}
24+
/>
25+
{#if $errors.name}{$errors.name}{/if}
1926

2027
<label for="email">E-mail</label>
21-
<input type="email" name="email" bind:value={$form.email} />
28+
<input
29+
type="email"
30+
name="email"
31+
bind:value={$form.email}
32+
aria-invalid={$errors.email ? 'true' : undefined}
33+
/>
34+
{#if $errors.email}{$errors.email}{/if}
2235

2336
<div><button>Submit</button></div>
2437
</form>

src/routes/(v2)/v2/schemasafe-types/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { JSONSchema } from 'sveltekit-superforms';
44
export const schema = {
55
type: 'object',
66
properties: {
7-
name: { type: 'string', default: 'Hello world!' },
7+
name: { type: 'string', default: '', minLength: 3 },
88
email: { type: 'string', format: 'email' }
99
},
1010
required: ['email'],
@@ -15,7 +15,7 @@ export const schema = {
1515
export const constSchema = {
1616
type: 'object',
1717
properties: {
18-
name: { type: 'string', default: 'Hello world!' },
18+
name: { type: 'string', default: '', minLength: 3 },
1919
email: { type: 'string', format: 'email' }
2020
},
2121
required: ['email'],

0 commit comments

Comments
 (0)