Skip to content

Commit 2db8b38

Browse files
committed
TypeBox adapter bumped to 1.0
1 parent 62a6be9 commit 2db8b38

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
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+
### Changed
11+
12+
- TypeBox adapter has been bumped to 1.0! Check the [migration guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md) to upgrade. Note that if you need to stay on 0.x for a while, you cannot upgrade to this version of Superforms.
13+
1014
### Fixed
1115

1216
- Possibly fixed the SuperDebug broken import on Svelte 5 in enforced runes mode [#599](https://github.com/ciscoheat/sveltekit-superforms/issues/599)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
],
116116
"peerDependencies": {
117117
"@exodus/schemasafe": "^1.3.0",
118-
"@sinclair/typebox": "^0.34.28",
118+
"typebox": "^1.0.36",
119119
"@sveltejs/kit": "1.x || 2.x",
120120
"@typeschema/class-validator": "^0.3.0",
121121
"@vinejs/vine": "^1.8.0 || ^2.0.0 || ^3.0.0",
@@ -133,7 +133,7 @@
133133
"@exodus/schemasafe": {
134134
"optional": true
135135
},
136-
"@sinclair/typebox": {
136+
"typebox": {
137137
"optional": true
138138
},
139139
"@typeschema/class-validator": {
@@ -171,7 +171,7 @@
171171
"@exodus/schemasafe": "^1.3.0",
172172
"@finom/zod-to-json-schema": "^3.24.11",
173173
"@gcornut/valibot-json-schema": "^0.42.0",
174-
"@sinclair/typebox": "^0.34.41",
174+
"typebox": "^1.0.36",
175175
"@typeschema/class-validator": "^0.3.0",
176176
"@vinejs/vine": "^3.0.1",
177177
"arktype": "^2.1.22",

pnpm-lock.yaml

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

src/lib/adapters/typeSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSchema, Static as Static$1 } from '@sinclair/typebox';
1+
import type { TSchema, Static as Static$1 } from 'typebox';
22
import type { type } from 'arktype';
33
import type { AnySchema } from 'joi';
44
import type {

src/lib/adapters/typebox.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,41 @@ import {
66
type ValidationResult,
77
type ClientValidationAdapter
88
} from './adapters.js';
9-
import type { TSchema } from '@sinclair/typebox';
10-
import type { TypeCheck } from '@sinclair/typebox/compiler';
9+
import type { TSchema } from 'typebox';
10+
import type { Validator } from 'typebox/compile';
1111
import { memoize } from '$lib/memoize.js';
12+
import Type from 'typebox';
13+
14+
/**
15+
* Custom TypeBox type for Date for testing purposes.
16+
*/
17+
export class TDate extends Type.Base<globalThis.Date> {
18+
public override Check(value: unknown): value is globalThis.Date {
19+
return value instanceof globalThis.Date;
20+
}
21+
public override Errors(value: unknown): object[] {
22+
return this.Check(value) ? [] : [{ message: 'must be Date' }];
23+
}
24+
public override Create(): globalThis.Date {
25+
return new globalThis.Date(0);
26+
}
27+
}
28+
29+
/**
30+
* Custom TypeBox type for Date for testing purposes.
31+
*/
32+
export function Date(): TDate {
33+
return new TDate();
34+
}
1235

1336
// From https://github.com/sinclairzx81/typebox/tree/ca4d771b87ee1f8e953036c95a21da7150786d3e/example/formats
1437
const Email =
1538
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
1639

1740
async function modules() {
18-
const { TypeCompiler } = await import(/* webpackIgnore: true */ '@sinclair/typebox/compiler');
19-
const { FormatRegistry } = await import(/* webpackIgnore: true */ '@sinclair/typebox');
20-
return { TypeCompiler, FormatRegistry };
41+
const { Compile } = await import(/* webpackIgnore: true */ 'typebox/compile');
42+
const Format = await import(/* webpackIgnore: true */ 'typebox/format');
43+
return { Compile, Format };
2144
}
2245

2346
const fetchModule = /* @__PURE__ */ memoize(modules);
@@ -26,14 +49,14 @@ async function validate<T extends TSchema>(
2649
schema: T,
2750
data: unknown
2851
): Promise<ValidationResult<Infer<T, 'typebox'>>> {
29-
const { TypeCompiler, FormatRegistry } = await fetchModule();
52+
const { Compile, Format } = await fetchModule();
3053

3154
if (!compiled.has(schema)) {
32-
compiled.set(schema, TypeCompiler.Compile<TSchema>(schema));
55+
compiled.set(schema, Compile(schema));
3356
}
3457

35-
if (!FormatRegistry.Has('email')) {
36-
FormatRegistry.Set('email', (value) => Email.test(value));
58+
if (!Format.Has('email')) {
59+
Format.Set('email', (value: string) => Email.test(value));
3760
}
3861

3962
const validator = compiled.get(schema);
@@ -46,7 +69,7 @@ async function validate<T extends TSchema>(
4669
return {
4770
success: false,
4871
issues: errors.map((issue) => ({
49-
path: issue.path.substring(1).split('/'),
72+
path: issue.instancePath ? issue.instancePath.substring(1).split('/') : [],
5073
message: issue.message
5174
}))
5275
};
@@ -74,4 +97,4 @@ function _typeboxClient<T extends TSchema>(
7497
export const typebox = /* @__PURE__ */ memoize(_typebox);
7598
export const typeboxClient = /* @__PURE__ */ memoize(_typeboxClient);
7699

77-
const compiled = new WeakMap<TSchema, TypeCheck<TSchema>>();
100+
const compiled = new WeakMap<TSchema, Validator<{}, TSchema>>();

src/lib/formData.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function _parseFormData<T extends Record<string, unknown>>(
228228
return entry;
229229
}
230230

231-
console.log(`Parsing entry for key "${key}":`, entry); //debug
231+
//console.log(`Parsing entry for key "${key}":`, entry); //debug
232232

233233
if (entry && typeof entry !== 'string') {
234234
const allowFiles = legacyMode ? options?.allowFiles === true : options?.allowFiles !== false;
@@ -317,10 +317,10 @@ function parseFormDataEntry(
317317
info: SchemaInfo,
318318
fromURL = false
319319
): unknown {
320-
console.log(`Parsing FormData ${key} (${type}): ${value ? `"${value}"` : 'undefined'}`, info); //debug
320+
//console.log(`Parsing FormData ${key} (${type}): ${value ? `"${value}"` : 'undefined'}`, info); //debug
321321

322322
if (!value) {
323-
console.log(`No FormData for "${key}" (${type}).`, info); //debug
323+
//console.log(`No FormData for "${key}" (${type}).`, info); //debug
324324

325325
// Special case for booleans with default value true, *when posted* (not from URL)
326326
if (!fromURL && type == 'boolean' && info.isOptional && info.schema.default === true) {

src/routes/(v2)/v2/typebox/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type } from '@sinclair/typebox';
1+
import { Type } from 'typebox';
22

33
export const schema = Type.Object({
44
name: Type.String({ minLength: 2 }),

src/tests/superValidate.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ import {
4646
import { arktype } from '$lib/adapters/arktype.js';
4747
import { type } from 'arktype';
4848

49-
import { typebox } from '$lib/adapters/typebox.js';
50-
import { Type } from '@sinclair/typebox';
49+
import { typebox, Date as TypeBoxDate } from '$lib/adapters/typebox.js';
50+
import { Type } from 'typebox';
5151

5252
import { joi } from '$lib/adapters/joi.js';
5353
import Joi from 'joi';
@@ -241,7 +241,7 @@ describe('TypeBox', () => {
241241
email: Type.String({ format: 'email' }),
242242
tags: Type.Array(Type.String({ minLength: 2 }), { minItems: 3 }),
243243
score: Type.Integer({ minimum: 0 }),
244-
date: Type.Optional(Type.Date()),
244+
date: Type.Optional(TypeBoxDate()),
245245
nospace: Type.Optional(Type.String({ pattern: '^\\S*$' })),
246246
extra: Type.Union([Type.String(), Type.Null()])
247247
});

0 commit comments

Comments
 (0)