Skip to content

Commit 6529549

Browse files
committed
Fixed BigInt parsing.
Closes #526
1 parent 1e324d4 commit 6529549

File tree

8 files changed

+89
-3
lines changed

8 files changed

+89
-3
lines changed

CHANGELOG.md

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

1212
- Arbitrary types can now be sent in the form using the [transport](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) feature in SvelteKit hooks. There is a `transport` option both for `superValidate` and `superForm` that the `transport` export in `hooks.ts` can be used in directly. **Note:** Requires SvelteKit `^2.11.0`.
1313

14+
### Fixed
15+
16+
- `z.bigint()` was interpreted as a number instead of a [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
17+
1418
## [2.21.1] - 2024-12-04
1519

1620
### Fixed

src/lib/formData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ function parseFormDataEntry(
317317
const date = new Date(value ?? '');
318318
return !isNaN(date as unknown as number) ? date : undefined;
319319
}
320+
case 'int64':
320321
case 'bigint':
321322
return BigInt(value ?? '.');
322323
case 'symbol':

src/lib/jsonSchema/schemaDefaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export function defaultValue(type: SchemaType, enumType: unknown[] | undefined):
203203
case 'unix-time':
204204
// Cannot add default for Date due to https://github.com/Rich-Harris/devalue/issues/51
205205
return undefined;
206+
case 'int64':
206207
case 'bigint':
207208
return BigInt(0);
208209
case 'set':

src/lib/jsonSchema/schemaInfo.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type SchemaType =
88
| 'date'
99
| 'unix-time'
1010
| 'bigint'
11+
| 'int64'
1112
| 'any'
1213
| 'symbol'
1314
| 'set'
@@ -26,7 +27,7 @@ export type SchemaInfo = {
2627
required?: string[];
2728
};
2829

29-
const conversionFormatTypes = ['unix-time', 'bigint', 'any', 'symbol', 'set'];
30+
const conversionFormatTypes = ['unix-time', 'bigint', 'any', 'symbol', 'set', 'int64'];
3031

3132
/**
3233
* Normalizes the different kind of schema variations (anyOf, union, const null, etc)
@@ -113,7 +114,9 @@ function schemaTypes(
113114
} else if (schema.format && conversionFormatTypes.includes(schema.format)) {
114115
types.unshift(schema.format as SchemaType);
115116

116-
if (schema.format == 'unix-time') {
117+
// Remove the integer type, as the schema format will be used
118+
// instead in the following cases
119+
if (schema.format == 'unix-time' || schema.format == 'int64') {
117120
const i = types.findIndex((t) => t == 'integer');
118121
types.splice(i, 1);
119122
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
'effect',
7474
'issue-500',
7575
'typebox',
76-
'transport'
76+
'transport',
77+
'bigint'
7778
].sort();
7879
</script>
7980

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
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, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'A big number: ' + form.data.number);
22+
}
23+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
//import { zod } from '$lib/adapters/zod.js'
5+
//import { schema } from './schema.js';
6+
7+
export let data;
8+
9+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
10+
taintedMessage: false
11+
//dataType: 'json',
12+
//validators: zod(schema)
13+
});
14+
</script>
15+
16+
<SuperDebug data={{ $form, $errors, $tainted }} />
17+
18+
{#if $message}<h4>{$message}</h4>{/if}
19+
20+
<form method="POST" use:enhance>
21+
<label>
22+
A big number please: <input
23+
type="number"
24+
name="number"
25+
value={$form.number}
26+
oninput={(e) => ($form.number = BigInt(e.currentTarget.value))}
27+
aria-invalid={$errors.number ? 'true' : undefined}
28+
/>
29+
{#if $errors.number}<span class="invalid">{$errors.number}</span>{/if}
30+
</label>
31+
<div>
32+
<button>Submit</button>
33+
</div>
34+
</form>
35+
36+
<style lang="scss">
37+
form {
38+
margin: 2rem 0;
39+
40+
input {
41+
background-color: #dedede;
42+
}
43+
44+
.invalid {
45+
color: crimson;
46+
}
47+
}
48+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
number: z.bigint().min(BigInt(1000000), 'A BIG number please!')
5+
});

0 commit comments

Comments
 (0)