Skip to content

Commit 660e7c1

Browse files
committed
Added test case for schemasafe bug
1 parent e31d1a8 commit 660e7c1

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Actions, PageServerLoad } from './$types.js';
2+
3+
import { superValidate, message } from '$lib/index.js';
4+
import { schemasafe } from '$lib/adapters/schemasafe.js';
5+
import { fail } from '@sveltejs/kit';
6+
import { schema } from './schema.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
const adapter = schemasafe(schema);
10+
const defaultValue = {
11+
arr: [{
12+
arr: [{
13+
tryInvalidValue: ''
14+
}]
15+
}]
16+
}
17+
return {
18+
form: await superValidate(defaultValue, adapter)
19+
};
20+
};
21+
22+
export const actions: Actions = {
23+
default: async ({ request }) => {
24+
const adapter = schemasafe(schema);
25+
const form = await superValidate(request, adapter);
26+
console.log(form, form.data.arr[0].arr[0].tryInvalidValue);
27+
28+
if (!form.valid) return fail(400, { form });
29+
30+
return message(form, 'Form posted successfully!');
31+
}
32+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<script lang="ts">
2+
import { page } from '$app/state';
3+
import { superForm } from '$lib/index.js';
4+
import { schemasafe } from '$lib/adapters/schemasafe.js';
5+
import SuperDebug from '$lib/index.js';
6+
import { schema } from './schema.js';
7+
8+
let { data } = $props();
9+
10+
const validators = schemasafe(schema);
11+
12+
const { form, errors, constraints, message, enhance } = superForm(data.form, {
13+
dataType: 'json',
14+
resetForm: false,
15+
validators,
16+
validationMethod: 'oninput'
17+
});
18+
</script>
19+
20+
<SuperDebug data={{ $form, $errors, $constraints }} />
21+
22+
<h3>Superforms testing ground - JSON Schema</h3>
23+
24+
{#if $message}
25+
<!-- eslint-disable-next-line svelte/valid-compile -->
26+
<div class="status" class:error={page.status >= 400} class:success={page.status == 200}>
27+
{$message}
28+
</div>
29+
{/if}
30+
31+
<form method="POST" use:enhance>
32+
{#each $form.arr as firstArr, i}
33+
{#each firstArr.arr as secondArr, j}
34+
<input
35+
type="text"
36+
bind:value={secondArr.tryInvalidValue}
37+
placeholder="Try entering text shorter than 5 chars"
38+
/>
39+
{#if $errors?.arr?.[i]?.arr?.[j]?.tryInvalidValue}<span class="invalid"
40+
>Please enter a valid email</span
41+
>{/if}
42+
{/each}
43+
{/each}
44+
<button>Submit</button>
45+
</form>
46+
47+
<hr />
48+
<p>
49+
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥
50+
</p>
51+
52+
<style>
53+
.invalid {
54+
color: red;
55+
}
56+
57+
.status {
58+
color: white;
59+
padding: 4px;
60+
padding-left: 8px;
61+
border-radius: 2px;
62+
font-weight: 500;
63+
}
64+
65+
.status.success {
66+
background-color: seagreen;
67+
}
68+
69+
.status.error {
70+
background-color: #ff2a02;
71+
}
72+
73+
input {
74+
background-color: #ddd;
75+
}
76+
77+
a {
78+
text-decoration: underline;
79+
}
80+
81+
hr {
82+
margin-top: 4rem;
83+
}
84+
85+
form {
86+
padding-top: 1rem;
87+
padding-bottom: 1rem;
88+
}
89+
</style>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { JSONSchema } from '$lib/index.js';
2+
3+
export const schema = {
4+
type: 'object',
5+
additionalProperties: false,
6+
required: ['arr'],
7+
properties: {
8+
arr: {
9+
type: 'array',
10+
items: {
11+
type: 'object',
12+
additionalProperties: false,
13+
required: ['arr'],
14+
properties: {
15+
arr: {
16+
type: 'array',
17+
items: {
18+
type: 'object',
19+
additionalProperties: false,
20+
properties: {
21+
tryInvalidValue: {
22+
type: 'string',
23+
minLength: 5
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}
32+
} as const satisfies JSONSchema;

0 commit comments

Comments
 (0)