Skip to content

Commit 771b74a

Browse files
committed
Test case added (not failing)
1 parent af0f1e8 commit 771b74a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { fail, message, superValidate } from '$lib/index.js';
2+
import type { PageServerLoad } from './$types.js';
3+
import { zod } from '$lib/adapters/zod.js';
4+
import { schema } from './schema.js';
5+
import type { Actions } from '@sveltejs/kit';
6+
7+
export const load: PageServerLoad = async () => {
8+
const form = await superValidate(zod(schema));
9+
return { form };
10+
};
11+
12+
export const actions: Actions = {
13+
async default({ request }) {
14+
const form = await superValidate(request, zod(schema), { allowFiles: true });
15+
console.log('🚀 ~ default ~ form:', form);
16+
17+
if (!form.valid) {
18+
return fail(400, { form });
19+
}
20+
return message(form, 'Posted OK!');
21+
}
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script lang="ts">
2+
import { superForm, fileProxy } from '$lib/index.js';
3+
import { zodClient } from '$lib/adapters/zod.js';
4+
import { schema } from './schema.js';
5+
import SuperDebug from '$lib/client/SuperDebug.svelte';
6+
7+
export let data;
8+
9+
const { form, enhance, errors, message } = superForm(data.form, {
10+
validators: zodClient(schema)
11+
});
12+
13+
const file = fileProxy(form, 'file');
14+
</script>
15+
16+
<SuperDebug data={{ $form, $message }} />
17+
18+
<form method="POST" enctype="multipart/form-data" use:enhance>
19+
<input type="file" name="file" accept="image/png, image/jpeg" bind:files={$file} />
20+
{#if $errors.file}<span>{$errors.file}</span>{/if}
21+
<button>Submit</button>
22+
</form>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
file: z.instanceof(File).refine((f) => f.size < 100_000, 'Max 100 kB upload size.')
5+
});

0 commit comments

Comments
 (0)