Skip to content

Commit 3efcb14

Browse files
committed
Fixed client-side validation for Date schema fields.
1 parent b2904be commit 3efcb14

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12-
- The warning shown when using multiple forms with the same id, is now working on `superForm` instantiation, not just when new form data is received.
12+
- When multiple forms exists with the same id, the warning is now displayed on `superForm` instantiation, not just when new form data is received.
13+
- Fixed client-side validation for `Date` schema fields.
1314

1415
## [1.3.0] - 2023-07-14
1516

src/lib/traversal.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,23 @@ export function comparePaths(newObj: unknown, oldObj: unknown) {
213213
if (data.isLeaf) {
214214
if (!exists) {
215215
addDiff();
216-
} else if (
216+
} else if (data.value !== exists.value) {
217+
addDiff();
218+
}
219+
} else if (exists) {
220+
if (
217221
data.value instanceof Date &&
218222
exists.value instanceof Date &&
219223
data.value.getTime() != exists.value.getTime()
220224
) {
221225
addDiff();
222-
} else if (data.value !== exists.value) {
226+
} else if (
227+
data.value instanceof Set &&
228+
exists.value instanceof Set &&
229+
!eqSet(data.value, exists.value)
230+
) {
223231
addDiff();
224232
}
225-
} else if (
226-
exists &&
227-
data.value instanceof Set &&
228-
exists.value instanceof Set &&
229-
!eqSet(data.value, exists.value)
230-
) {
231-
addDiff();
232233
}
233234
}
234235

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Actions, PageServerLoad } from './$types';
2+
import { message, superValidate } from '$lib/server';
3+
import { schema } from './schema';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = (async () => {
7+
const form = await superValidate(schema);
8+
return { form };
9+
}) satisfies PageServerLoad;
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, schema);
17+
console.log('POST', form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
} satisfies Actions;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script lang="ts">
2+
import { dateProxy, superForm, superValidateSync } from '$lib/client';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import { schema } from './schema';
5+
6+
const { errors, enhance, form, tainted } = superForm(
7+
superValidateSync(schema),
8+
{
9+
SPA: true,
10+
validators: schema,
11+
dataType: 'json',
12+
onSubmit({ cancel }) {
13+
if (!$tainted) cancel();
14+
},
15+
onUpdate({ form }) {
16+
if (form.valid) alert('valid');
17+
else alert('invalid');
18+
}
19+
}
20+
);
21+
22+
const date = dateProxy(form, 'date', {
23+
format: 'datetime-local',
24+
empty: 'undefined'
25+
});
26+
</script>
27+
28+
<SuperDebug data={{ $form, $errors, $tainted }} />
29+
30+
<form method="POST" use:enhance>
31+
<label>
32+
Name: <input name="date" type="datetime-local" bind:value={$date} />
33+
{#if $errors.date}<span class="invalid">{$errors.date}</span>{/if}
34+
</label>
35+
<div>
36+
<button>Submit</button>
37+
</div>
38+
</form>
39+
40+
<style lang="scss">
41+
form {
42+
margin: 2rem 0;
43+
44+
input {
45+
background-color: #dedede;
46+
}
47+
48+
.invalid {
49+
color: crimson;
50+
}
51+
}
52+
</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+
date: z
5+
.date()
6+
.min(new Date('2021-01-01'))
7+
.max(new Date('2023-07-01'), 'Max date: 2023-07-01')
8+
.optional()
9+
});

0 commit comments

Comments
 (0)