Skip to content

Commit 2fe0cc4

Browse files
committed
Fixes #332
Array data being undefined.
1 parent 04f17d5 commit 2fe0cc4

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Array data was set to `undefined` when `dataType: 'json'` was set.
13+
814
## [2.0.0] - 2024-02-11
915

1016
### Removed

src/lib/client/superForm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,7 @@ export function superForm<
15971597
return data.set(undefined);
15981598
} else if (
15991599
Array.isArray(data.value) &&
1600+
data.value.length &&
16001601
data.value.every((v) => v instanceof File)
16011602
) {
16021603
const key = '__superform_files_' + mergePath(data.path);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { superValidate } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { fail } from '@sveltejs/kit';
4+
import { schema } from './schema.js';
5+
6+
import type { Actions, PageServerLoad } from './$types.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
const form = await superValidate(zod(schema));
10+
console.log(form.data);
11+
12+
// Always return { form } in load functions
13+
return { form };
14+
};
15+
16+
export const actions: Actions = {
17+
default: async ({ request }) => {
18+
// Use superValidate in form actions too, but with the request
19+
const formData = await request.formData();
20+
console.log('🚀 ~ default: ~ formData:', formData);
21+
const form = await superValidate(formData, zod(schema));
22+
23+
console.dir(form, { depth: 10 });
24+
25+
// Convenient validation check:
26+
if (!form.valid) {
27+
// Always return { form } and things will just work.
28+
return fail(400, { form });
29+
}
30+
31+
// TODO: Do something with the validated form.data
32+
33+
// Yep, return { form } here too
34+
return { form };
35+
}
36+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/index.js';
3+
import SuperDebug from '$lib/index.js';
4+
import { schema } from './schema.js';
5+
import { zodClient } from '$lib/adapters/zod.js';
6+
7+
export let data;
8+
9+
const { form, enhance } = superForm(data.form, {
10+
dataType: 'json',
11+
validators: zodClient(schema)
12+
});
13+
</script>
14+
15+
<SuperDebug data={$form} />
16+
17+
<form method="POST" use:enhance>
18+
<div><button>Submit</button></div>
19+
</form>
20+
21+
<div style="margin-top:2rem;">
22+
<a target="_blank" href="https://superforms.rocks/get-started">Tutorial for this example here</a>
23+
</div>
24+
25+
<style>
26+
.invalid {
27+
color: red;
28+
}
29+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().default('Hello world!'),
5+
testArray: z.object({ foo: z.string() }).array(),
6+
nested: z.object({
7+
arr: z.object({ foo: z.string() }).array()
8+
})
9+
});

src/tests/superValidate.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,49 @@ describe('Edge cases', () => {
688688
});
689689
});
690690
});
691+
692+
describe('Array validation', () => {
693+
describe('should return an empty array as default when dataType is json', () => {
694+
const schema = z.object({
695+
testArray: z.object({ foo: z.string() }).array(),
696+
nested: z.object({
697+
arr: z.object({ foo: z.string() }).array()
698+
})
699+
});
700+
701+
it('with the default values', async () => {
702+
const form = await superValidate(zod(schema));
703+
expect(form.errors.testArray?._errors).toBeUndefined();
704+
expect(form.data.testArray).toEqual([]);
705+
expect(form.errors.nested?.arr?._errors).toBeUndefined();
706+
expect(form.data.nested.arr).toEqual([]);
707+
});
708+
709+
it('when passing data directly', async () => {
710+
const form = await superValidate({ testArray: undefined }, zod(schema), { errors: true });
711+
712+
expect(form.errors.testArray?._errors).toEqual(['Required']);
713+
expect(form.data.testArray).toEqual([]);
714+
expect(form.errors.nested?.arr?._errors).toBeUndefined();
715+
expect(form.data.nested.arr).toEqual([]);
716+
});
717+
718+
it('when passing an empty object', async () => {
719+
const form = await superValidate({}, zod(schema), { errors: true });
720+
721+
expect(form.errors.testArray).toBeUndefined();
722+
expect(form.data.testArray).toEqual([]);
723+
expect(form.errors.nested?.arr?._errors).toBeUndefined();
724+
expect(form.data.nested.arr).toEqual([]);
725+
});
726+
727+
it('when passing undefined', async () => {
728+
const form = await superValidate(undefined, zod(schema), { errors: true });
729+
730+
expect(form.errors.testArray).toBeUndefined();
731+
expect(form.data.testArray).toEqual([]);
732+
expect(form.errors.nested?.arr?._errors).toBeUndefined();
733+
expect(form.data.nested.arr).toEqual([]);
734+
});
735+
});
736+
});

0 commit comments

Comments
 (0)