Skip to content

Commit cd28d4b

Browse files
committed
Added test case
1 parent 96195a8 commit cd28d4b

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { effect } from '$lib/adapters/effect.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { CreateGalaxySchema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(effect(CreateGalaxySchema));
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, effect(CreateGalaxySchema), {
17+
allowFiles: true
18+
});
19+
console.log(form);
20+
21+
if (!form.valid) return fail(400, { form });
22+
23+
return message(form, 'Posted OK!');
24+
}
25+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
5+
export let data;
6+
7+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
8+
taintedMessage: false
9+
});
10+
</script>
11+
12+
<SuperDebug data={{ $form, $errors, $tainted }} />
13+
14+
{#if $message}<h4>{$message}</h4>{/if}
15+
16+
<form method="POST" enctype="multipart/form-data" use:enhance>
17+
<label>
18+
File: <input
19+
name="file"
20+
type="file"
21+
bind:value={$form.file}
22+
aria-invalid={$errors.file ? 'true' : undefined}
23+
/>
24+
{#if $errors.file}<span class="invalid">{$errors.file}</span>{/if}
25+
</label>
26+
<div>
27+
<button>Submit</button>
28+
</div>
29+
</form>
30+
31+
<style lang="scss">
32+
form {
33+
margin: 2rem 0;
34+
35+
input {
36+
background-color: #dedede;
37+
}
38+
39+
.invalid {
40+
color: crimson;
41+
}
42+
}
43+
</style>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Schema } from 'effect';
2+
3+
export const GalaxySchema = Schema.Struct({
4+
name: Schema.String,
5+
description: Schema.String,
6+
image: Schema.URL
7+
});
8+
9+
export type Galaxy = typeof GalaxySchema.Type;
10+
11+
export const CreateGalaxySchema = Schema.Struct({
12+
...GalaxySchema.omit('image').fields,
13+
file: Schema.instanceOf(File).annotations({
14+
jsonSchema: {}
15+
})
16+
});

0 commit comments

Comments
 (0)