Skip to content

Commit a2401c9

Browse files
committed
Added test for invalid type with refine.
1 parent a644bef commit a2401c9

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { superValidate, message } from '$lib/server';
2+
import { fail } from '@sveltejs/kit';
3+
import { userSchema } from './schema';
4+
5+
import type { Actions, PageServerLoad } from './$types';
6+
7+
///// Load function /////
8+
9+
export const load: PageServerLoad = async () => {
10+
const form = await superValidate(userSchema);
11+
return { form };
12+
};
13+
14+
///// Form actions /////
15+
16+
export const actions: Actions = {
17+
default: async ({ request }) => {
18+
const form = await superValidate(request, userSchema);
19+
20+
console.log('POST', form);
21+
22+
if (!form.valid) return fail(400, { form });
23+
24+
return message(form, 'Form posted successfully!');
25+
}
26+
};
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm } from '$lib/client';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { userSchema } from './schema';
6+
7+
export let data;
8+
9+
const { form, errors, message, enhance, tainted } = superForm(data.form, {
10+
customValidity: false,
11+
validators: userSchema,
12+
taintedMessage: null
13+
});
14+
15+
const salutations = [
16+
{ id: 1, text: 'one' },
17+
{ id: 2, text: 'two' }
18+
];
19+
</script>
20+
21+
<SuperDebug data={{ $form, $errors, $tainted }} />
22+
23+
<h3>Superforms testing ground</h3>
24+
25+
{#if $message}
26+
<div
27+
class="status"
28+
class:error={$page.status >= 400}
29+
class:success={$page.status == 200}
30+
>
31+
{$message}
32+
</div>
33+
{/if}
34+
35+
<form method="POST" use:enhance>
36+
<label>
37+
Salutation<br />
38+
<select
39+
name="salutationId"
40+
id="salutationId"
41+
bind:value={$form.salutationId}
42+
>
43+
<option value={0} selected>Choose Option</option>
44+
{#each salutations as salutation}
45+
<option value={salutation.id}>
46+
{salutation.text}
47+
</option>
48+
{/each}
49+
</select>
50+
{#if $errors.salutationId}<span class="invalid"
51+
>{$errors.salutationId}</span
52+
>{/if}
53+
</label>
54+
<br />
55+
56+
<label>
57+
Email<br />
58+
<input
59+
name="email"
60+
type="email"
61+
aria-invalid={$errors.email ? 'true' : undefined}
62+
bind:value={$form.email}
63+
/>
64+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
65+
</label>
66+
<br />
67+
68+
<label>
69+
Confirm Email<br />
70+
<input
71+
name="confirmEmail"
72+
type="email"
73+
aria-invalid={$errors.confirmEmail ? 'true' : undefined}
74+
bind:value={$form.confirmEmail}
75+
/>
76+
{#if $errors.confirmEmail}<span class="invalid"
77+
>{$errors.confirmEmail}</span
78+
>{/if}
79+
</label>
80+
<br />
81+
82+
<button>Submit</button>
83+
</form>
84+
85+
<style>
86+
.invalid {
87+
color: red;
88+
}
89+
90+
.status {
91+
color: white;
92+
padding: 4px;
93+
padding-left: 8px;
94+
border-radius: 2px;
95+
font-weight: 500;
96+
}
97+
98+
.status.success {
99+
background-color: seagreen;
100+
}
101+
102+
.status.error {
103+
background-color: #ff2a02;
104+
}
105+
106+
input {
107+
background-color: #ddd;
108+
}
109+
110+
form {
111+
padding-top: 1rem;
112+
padding-bottom: 1rem;
113+
}
114+
</style>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { z } from 'zod';
2+
export const userSchema = z
3+
.object({
4+
salutationId: z.number().positive(),
5+
//.default('' as unknown as number),
6+
name: z.string().min(1),
7+
email: z.string().email(),
8+
confirmEmail: z.string().email()
9+
})
10+
.refine(
11+
(data) => {
12+
console.log('REFINE', data);
13+
return data.email == data.confirmEmail;
14+
},
15+
{
16+
message: 'Email doesnt match',
17+
path: ['confirmEmail']
18+
}
19+
);

0 commit comments

Comments
 (0)