Skip to content

Commit b400fef

Browse files
committed
Added test for nested data with $ref
1 parent 89656da commit b400fef

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { nerveForm } from './schema.js';
2+
import { z } from 'zod';
3+
import { superValidate } from 'sveltekit-superforms/server';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import type { Actions } from '@sveltejs/kit';
6+
import { fail } from '@sveltejs/kit';
7+
import { parse } from 'devalue';
8+
9+
type DeficitTypeKey = keyof z.infer<typeof nerveForm>;
10+
type NerveFormData = z.infer<typeof nerveForm>;
11+
12+
type Side = 'left' | 'right';
13+
14+
export async function load() {
15+
const findings = [
16+
{
17+
id: '1',
18+
key: 'nerve',
19+
nerve: 'c5',
20+
type: 'motor',
21+
grade: 25,
22+
side: 'left',
23+
comments: 'This is a comment'
24+
},
25+
{
26+
id: '2',
27+
key: 'nerve',
28+
nerve: 'c5',
29+
type: 'sensory',
30+
grade: 35,
31+
side: 'left',
32+
comments: 'This is a comment'
33+
}
34+
];
35+
36+
const emptyData = {
37+
motor: { left: {}, right: {} },
38+
sensory: { left: {}, right: {} },
39+
dysesthesia: { left: {}, right: {} }
40+
};
41+
42+
const formData = findings.reduce<NerveFormData>((acc, cur) => {
43+
if (cur.grade && cur.type && cur.side) {
44+
acc[cur.type as DeficitTypeKey][cur.side as Side].grade = cur.grade;
45+
acc[cur.type as DeficitTypeKey][cur.side as Side].comments = cur.comments || '';
46+
}
47+
return acc;
48+
}, emptyData);
49+
50+
const form = await superValidate(formData, zod(nerveForm));
51+
return {
52+
form
53+
};
54+
}
55+
56+
export const actions: Actions = {
57+
default: async ({ request }) => {
58+
const formData = await request.formData();
59+
console.dir(parse(formData.get('__superform_json')), { depth: 10 });
60+
const form = await superValidate(formData, zod(nerveForm));
61+
console.dir(form, { depth: 10 }); //debug
62+
63+
if (!form.valid) console.log('Not valid');
64+
if (!form.valid) return fail(400, { form });
65+
return { form };
66+
}
67+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script lang="ts">
2+
import SuperDebug from '$lib/index.js';
3+
import { superForm } from '$lib/index.js';
4+
5+
export let data;
6+
7+
const form = superForm(data.form, { dataType: 'json' });
8+
const { form: formData, enhance } = form;
9+
10+
const sections = ['motor', 'sensory', 'dysesthesia'] as const;
11+
const sides = ['left', 'right'] as const;
12+
</script>
13+
14+
<form method="POST" use:enhance>
15+
<main class="flex flex-col gap-y-8">
16+
{#each sections as section}
17+
<fieldset class="border p-4 rounded-md">
18+
<legend class="px-2">{section}</legend>
19+
<div class="flex gap-4">
20+
{#each sides as side}
21+
<div class="flex flex-col space-y-2 basis-1/2">
22+
<div>{side} grade</div>
23+
<input type="number" bind:value={$formData[section][side].grade} class="w-28" />
24+
<textarea bind:value={$formData[section][side].comments}></textarea>
25+
</div>
26+
{/each}
27+
</div>
28+
</fieldset>
29+
{/each}
30+
</main>
31+
<footer class="my-8">
32+
<button class="border rounded-md px-2 py-1 bg-green-300" type="submit">Save Data</button>
33+
</footer>
34+
</form>
35+
36+
<SuperDebug data={formData} />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { z } from 'zod';
2+
3+
const deficitForm = z.object({
4+
grade: z.number().min(0).max(100).nullable().default(null),
5+
comments: z.string().optional()
6+
});
7+
8+
const sideData = z.object({
9+
left: deficitForm,
10+
right: deficitForm
11+
});
12+
13+
export const nerveForm = z.object({
14+
motor: sideData,
15+
sensory: sideData,
16+
dysesthesia: sideData
17+
});

0 commit comments

Comments
 (0)