Skip to content

Commit 5802d3e

Browse files
committed
(Working) test for proxy snapshot.
1 parent 64084e3 commit 5802d3e

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
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, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import NumberInput from './NumberInput.svelte';
5+
6+
export let data;
7+
8+
const superform = superForm(data.form, {
9+
taintedMessage: false
10+
//dataType: 'json',
11+
//validators: zod(schema)
12+
});
13+
const { form, errors, tainted, message, enhance, capture, restore } = superform;
14+
15+
export const snapshot = { capture, restore };
16+
</script>
17+
18+
<SuperDebug data={{ $form, $errors, $tainted }} />
19+
20+
{#if $message}<h4>{$message}</h4>{/if}
21+
22+
<form method="POST" use:enhance>
23+
<label>
24+
Name: <input
25+
name="name"
26+
bind:value={$form.name}
27+
aria-invalid={$errors.name ? 'true' : undefined}
28+
/>
29+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
30+
</label>
31+
<label>
32+
Score: <NumberInput form={superform} name="score" />
33+
{#if $errors.score}<span class="invalid">{$errors.score}</span>{/if}
34+
</label>
35+
<div>
36+
<button>Submit</button>
37+
</div>
38+
</form>
39+
40+
<style lang="scss">
41+
form {
42+
margin: 2rem 0;
43+
44+
input {
45+
background-color: #dedede;
46+
}
47+
48+
.invalid {
49+
color: crimson;
50+
}
51+
}
52+
</style>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script context="module" lang="ts">
2+
import type { FormPathLeaves } from '$lib/index.js';
3+
type T = Record<string, unknown>;
4+
</script>
5+
6+
<script generics="T extends Record<string, unknown>" lang="ts">
7+
import { numberProxy, type SuperForm } from '$lib/index.js';
8+
9+
export let form: SuperForm<T>;
10+
export let name: FormPathLeaves<T>;
11+
12+
const proxy = numberProxy(form, name, { empty: 'zero', initiallyEmptyIfZero: true });
13+
</script>
14+
15+
<input {name} {...$$restProps} type="number" bind:value={$proxy} />
16+
17+
<style>
18+
input {
19+
background-color: #dedede;
20+
}
21+
22+
.invalid {
23+
color: crimson;
24+
}
25+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(2),
5+
score: z.number()
6+
});

0 commit comments

Comments
 (0)