Skip to content

Commit 96195a8

Browse files
committed
Fixed bigint handling for Zod 4 and Valibot.
1 parent b705346 commit 96195a8

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

CHANGELOG.md

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

1212
- SuperDebug rune version is back, can now be imported as `import SuperDebug from 'sveltekit-superforms/SuperDebug.svelte';`
13+
- Fixed `bigint` handling for Zod 4 and Valibot.
1314

1415
## [2.26.1] - 2025-06-05
1516

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
},
170170
"optionalDependencies": {
171171
"@exodus/schemasafe": "^1.3.0",
172-
"@gcornut/valibot-json-schema": "^0.31.0",
172+
"@gcornut/valibot-json-schema": "^0.42.0",
173173
"@sinclair/typebox": "^0.34.35",
174174
"@typeschema/class-validator": "^0.3.0",
175175
"@vinejs/vine": "^3.0.1",

pnpm-lock.yaml

Lines changed: 19 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/adapters/valibot.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type SupportedSchemas = GenericSchema | GenericSchemaAsync;
2828
const defaultOptions = {
2929
strictObjectTypes: true,
3030
dateStrategy: 'integer' as const,
31+
bigintStrategy: 'integer' as const,
3132
ignoreUnknownValidation: true,
3233
customSchemaConversion: {
3334
custom: () => ({}),

src/lib/adapters/zod4.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const defaultJSONSchemaOptions = {
2828
if (def.type === 'date') {
2929
ctx.jsonSchema.type = 'string';
3030
ctx.jsonSchema.format = 'date-time';
31+
} else if (def.type === 'bigint') {
32+
ctx.jsonSchema.type = 'string';
33+
ctx.jsonSchema.format = 'bigint';
3134
}
3235
}
3336
} satisfies Options;

src/lib/jsonSchema/schemaInfo.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,19 @@ function schemaTypes(
117117
} else if (schema.format && conversionFormatTypes.includes(schema.format)) {
118118
types.unshift(schema.format as SchemaType);
119119

120-
// Remove the integer type, as the schema format will be used
120+
// For dates and int64 (bigint), remove the integer type, as the schema format will be used
121121
// instead in the following cases
122122
if (schema.format == 'unix-time' || schema.format == 'int64') {
123123
const i = types.findIndex((t) => t == 'integer');
124124
types.splice(i, 1);
125125
}
126+
127+
// For bigint, remove the string type, as the schema format will be used
128+
// instead in the following cases
129+
if (schema.format == 'bigint') {
130+
const i = types.findIndex((t) => t == 'string');
131+
types.splice(i, 1);
132+
}
126133
}
127134

128135
if (schema.const && schema.const !== null && typeof schema.const !== 'function') {

src/tests/superValidate.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,18 @@ describe('Valibot', () => {
470470
).not.toThrow();
471471
});
472472

473+
it('should handle bigint', async () => {
474+
const schema = v.object({
475+
id: v.bigint()
476+
});
477+
const data = new FormData();
478+
data.set('id', '123456789123456789');
479+
480+
const form = await superValidate(data, valibot(schema));
481+
expect(form.valid).toBe(true);
482+
expect(form.data).toEqual({ id: BigInt('123456789123456789') });
483+
});
484+
473485
/*
474486
it('should work with FormPathLeaves and brand', async () => {
475487
const schema = v.object({ id: v.brand(v.string(), 'Id') });
@@ -976,7 +988,7 @@ describe('Zod 4', () => {
976988
'enumDef'
977989
]);
978990
expect(adapter.defaults).toEqual({
979-
nativeEnumInt: 'Apple',
991+
nativeEnumInt: 7,
980992
nativeEnumString: 'GREEN',
981993
enum: 'a',
982994
enumDef: ''
@@ -1097,6 +1109,18 @@ describe('Zod 4', () => {
10971109
});
10981110
});
10991111

1112+
it('should work with bigint', async () => {
1113+
const schema = z4.object({
1114+
id: z4.bigint()
1115+
});
1116+
const data = new FormData();
1117+
data.set('id', '123456789123456789');
1118+
1119+
const form = await superValidate(data, zod4(schema));
1120+
expect(form.valid).toBe(true);
1121+
expect(form.data).toEqual({ id: BigInt('123456789123456789') });
1122+
});
1123+
11001124
schemaTest(zod4(schema), undefined, undefined, undefined, 'zod4');
11011125
});
11021126

0 commit comments

Comments
 (0)