Skip to content

Commit 38d78d0

Browse files
committed
Dates were set to undefined when posting invalid data.
1 parent f973411 commit 38d78d0

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Dates were set to `undefined` when posting invalid data.
13+
1014
### Added
1115

1216
- `onSubmit.submitJsonData`, to override what's being submitted, when dataType is set to `'json'` and validation succeeds for `$form`.

src/lib/errors.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { defaultTypes, defaultValue, type SchemaFieldType } from './jsonSchema/s
66
import type { JSONSchema } from './jsonSchema/index.js';
77
import { clone } from './utils.js';
88
import { merge } from 'ts-deepmerge';
9-
import { schemaInfo } from './jsonSchema/schemaInfo.js';
9+
import { schemaInfo, type SchemaType } from './jsonSchema/schemaInfo.js';
1010
import type { ValidationIssue } from './adapters/adapters.js';
1111

1212
export class SuperFormError extends Error {
@@ -185,10 +185,15 @@ export function replaceInvalidDefaults<T extends Record<string, unknown>>(
185185
return dataValue;
186186
}
187187

188+
const dateTypes: SchemaType[] = ['unix-time', 'Date', 'date'];
189+
188190
for (const schemaType of types) {
189191
const defaultTypeValue = defaultValue(schemaType, undefined);
190192

191-
const sameType = typeof dataValue === typeof defaultTypeValue;
193+
const sameType =
194+
typeof dataValue === typeof defaultTypeValue ||
195+
(dateTypes.includes(schemaType) && dataValue instanceof Date);
196+
192197
const sameExistance = sameType && (dataValue === null) === (defaultTypeValue === null);
193198

194199
if (sameType && sameExistance) {

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'submit-json',
2828
'trim-fields',
2929
'unions',
30+
'valibot-dates',
3031
'validate',
3132
'validate-update',
3233
'validators-clear'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { schema } from './schema.js';
2+
import { fail } from '@sveltejs/kit';
3+
import { superValidate } from '$lib/index.js';
4+
import { valibot } from '$lib/adapters/valibot.js';
5+
6+
export const load = async () => {
7+
const form = await superValidate(valibot(schema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
test: async (event) => {
13+
const formData = await event.request.formData();
14+
console.log('🚀 ~ test: ~ formData:', formData);
15+
const form = await superValidate(formData, valibot(schema));
16+
console.log(form);
17+
if (!form.valid) return fail(400, { form });
18+
return { form };
19+
}
20+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { dateProxy, superForm } from '$lib/index.js';
4+
5+
export let data;
6+
7+
let logForm = superForm(data.form, {
8+
taintedMessage: false,
9+
resetForm: false,
10+
dataType: $page.url.searchParams.has('json') ? 'json' : 'form'
11+
});
12+
13+
const { form, constraints, errors, enhance, posted } = logForm;
14+
const proxyDate = dateProxy(form, 'date', { format: 'datetime-local' });
15+
</script>
16+
17+
<form method="post" action="?/test" use:enhance>
18+
<p>
19+
<input
20+
name="date"
21+
type="datetime-local"
22+
bind:value={$proxyDate}
23+
aria-invalid={$errors.date ? 'true' : undefined}
24+
{...$constraints.date}
25+
/>
26+
Select a date
27+
</p>
28+
<p>
29+
<input type="text" bind:value={$form.missing} />
30+
Leave blank to produce error
31+
</p>
32+
<p>
33+
<button type="submit">Test</button>
34+
</p>
35+
<p>
36+
If the schema produces an error in the form action, the date becomes undefined, even if a value
37+
was selected.
38+
</p>
39+
</form>
40+
41+
{#if $posted}
42+
<p>DATE:{$form.date?.toISOString().slice(0, 10)}</p>
43+
{/if}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { date, minLength, object, string } from 'valibot';
2+
3+
export const schema = object({
4+
date: date(),
5+
missing: string([minLength(1)])
6+
});

0 commit comments

Comments
 (0)