Skip to content

Commit 97d3386

Browse files
committed
numberProxy and intProxy now works with the empty option.
Fixes #232.
1 parent 851ca9f commit 97d3386

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

CHANGELOG.md

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

1212
- 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.
13+
- Fixed client-side validation for `Date` schema fields. ([#232](https://github.com/ciscoheat/sveltekit-superforms/issues/232))
14+
- `numberProxy` and `intProxy` now works with the `empty` option. ([#232](https://github.com/ciscoheat/sveltekit-superforms/issues/232))
1415

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

src/lib/client/proxies.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,17 @@ function _stringProxy<
133133
options: DefaultOptions
134134
): Writable<string> {
135135
function toValue(val: unknown) {
136-
if (typeof val !== 'string')
137-
throw new SuperFormError('stringProxy received a non-string value.');
138-
139136
if (!val && options.empty !== undefined)
140137
return options.empty === 'null' ? null : undefined;
141138

139+
if (typeof val === 'number') {
140+
val = val.toString();
141+
}
142+
143+
if (typeof val !== 'string') {
144+
throw new SuperFormError('stringProxy received a non-string value.');
145+
}
146+
142147
if (type == 'string') return val;
143148
else if (type == 'boolean') return !!val;
144149
else if (type == 'date') return new Date(val);

src/routes/tests/datetime-local/+page.svelte

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<script lang="ts">
2-
import { dateProxy, superForm, superValidateSync } from '$lib/client';
2+
import {
3+
dateProxy,
4+
intProxy,
5+
superForm,
6+
superValidateSync
7+
} from '$lib/client';
38
import SuperDebug from '$lib/client/SuperDebug.svelte';
49
import { schema } from './schema';
510
@@ -24,11 +29,19 @@
2429
format: 'datetime-local',
2530
empty: 'undefined'
2631
});
32+
33+
const num = intProxy(form, 'number', {
34+
empty: 'undefined'
35+
});
2736
</script>
2837

29-
<SuperDebug data={{ $form, $errors, $tainted }} />
38+
<SuperDebug data={{ $form, $tainted }} />
3039

3140
<form method="POST" use:enhance>
41+
<label>
42+
Number: <input name="number" type="number" bind:value={$num} />
43+
{#if $errors.number}<span class="invalid">{$errors.number}</span>{/if}
44+
</label>
3245
<label>
3346
Name: <input name="date" type="datetime-local" bind:value={$date} />
3447
{#if $errors.date}<span class="invalid">{$errors.date}</span>{/if}

src/routes/tests/datetime-local/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod';
22

33
export const schema = z.object({
4+
number: z.number().optional(),
45
date: z
56
.date()
67
.min(new Date('2021-01-01'))

0 commit comments

Comments
 (0)