Skip to content

Commit 1080275

Browse files
authored
Migrate valibot to v0.31.0-rc-5. (#437)
All tests passing.
1 parent d0b6fe4 commit 1080275

File tree

13 files changed

+99
-76
lines changed

13 files changed

+99
-76
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"joi": "^17.13.1",
111111
"superstruct": "^1.0.4",
112112
"svelte": "3.x || 4.x || >=5.0.0-next.51",
113-
"valibot": ">=0.28.1 <=0.30.0",
113+
"valibot": ">=0.31.0 <=0.32.0",
114114
"yup": "^1.4.0",
115115
"zod": "^3.23.8"
116116
},
@@ -145,15 +145,15 @@
145145
},
146146
"optionalDependencies": {
147147
"@exodus/schemasafe": "^1.3.0",
148-
"@gcornut/valibot-json-schema": "^0.0.27",
148+
"@gcornut/valibot-json-schema": "^0.31.0",
149149
"@sinclair/typebox": "^0.32.30",
150150
"@sodaru/yup-to-json-schema": "^2.0.1",
151151
"@vinejs/vine": "^1.8.0",
152152
"arktype": "2.0.0-dev.15",
153153
"joi": "^17.13.1",
154154
"json-schema-to-ts": "^3.1.0",
155155
"superstruct": "^1.0.4",
156-
"valibot": "^0.30.0",
156+
"valibot": "^0.31.0",
157157
"yup": "^1.4.0",
158158
"zod": "^3.23.8",
159159
"zod-to-json-schema": "^3.23.0"

pnpm-lock.yaml

Lines changed: 30 additions & 29 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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { TSchema, Static as Static$1 } from '@sinclair/typebox';
22
import type { Type } from 'arktype';
33
import type { AnySchema } from 'joi';
4-
import type { BaseSchema, BaseSchemaAsync, Input, Output } from 'valibot';
4+
import type {
5+
GenericSchema,
6+
GenericSchemaAsync,
7+
InferInput as Input,
8+
InferOutput as Output
9+
} from 'valibot';
510
import type { Schema as Schema$2, InferType } from 'yup';
611
import type { ZodSchema, input, output } from 'zod';
712
import type { SchemaTypes, Infer as VineInfer } from '@vinejs/vine/types';
@@ -47,7 +52,7 @@ type InferSchema<TResolver extends Resolver> = TResolver['base'];
4752

4853
type ValidationIssue = {
4954
message: string;
50-
path?: Array<string | number | symbol>;
55+
path?: Array<string | number | symbol | unknown>;
5156
};
5257

5358
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -94,9 +99,11 @@ interface TypeBoxResolver extends Resolver {
9499
}
95100

96101
interface ValibotResolver extends Resolver {
97-
base: BaseSchema | BaseSchemaAsync;
98-
input: this['schema'] extends BaseSchema | BaseSchemaAsync ? Input<this['schema']> : never;
99-
output: this['schema'] extends BaseSchema | BaseSchemaAsync ? Output<this['schema']> : never;
102+
base: GenericSchema | GenericSchemaAsync;
103+
input: this['schema'] extends GenericSchema | GenericSchemaAsync ? Input<this['schema']> : never;
104+
output: this['schema'] extends GenericSchema | GenericSchemaAsync
105+
? Output<this['schema']>
106+
: never;
100107
}
101108

102109
interface YupResolver extends Resolver {

src/lib/adapters/valibot.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,29 @@ import {
77
type ValidationResult,
88
type ClientValidationAdapter
99
} from './adapters.js';
10-
import { safeParseAsync, type BaseSchema, type BaseSchemaAsync, type SchemaConfig } from 'valibot';
10+
import {
11+
safeParseAsync,
12+
type GenericSchema,
13+
type GenericSchemaAsync,
14+
type Config,
15+
type GenericIssue,
16+
type IssuePathItem,
17+
type SetPathItem
18+
} from 'valibot';
1119
import { memoize } from '$lib/memoize.js';
1220
import {
1321
type ToJSONSchemaOptions,
1422
toJSONSchema as valibotToJSON
1523
} from '@gcornut/valibot-json-schema';
1624
import type { JSONSchema } from '../jsonSchema/index.js';
1725

18-
type SupportedSchemas = BaseSchema | BaseSchemaAsync;
26+
type SupportedSchemas = GenericSchema | GenericSchemaAsync;
1927

2028
const defaultOptions = {
2129
strictObjectTypes: true,
2230
dateStrategy: 'integer' as const,
2331
ignoreUnknownValidation: true,
24-
customSchemaConversion: { special: () => ({}), instance: () => ({}) }
32+
customSchemaConversion: { custom: () => ({}), instance: () => ({}) }
2533
} satisfies ToJSONSchemaOptions;
2634

2735
/* @__NO_SIDE_EFFECTS__ */
@@ -32,7 +40,7 @@ export const valibotToJSONSchema = (options: ToJSONSchemaOptions) => {
3240
async function validate<T extends SupportedSchemas>(
3341
schema: T,
3442
data: unknown,
35-
config?: SchemaConfig
43+
config?: Config<GenericIssue<unknown>>
3644
): Promise<ValidationResult<Infer<T>>> {
3745
const result = await safeParseAsync(schema, data, config);
3846
if (result.success) {
@@ -44,7 +52,7 @@ async function validate<T extends SupportedSchemas>(
4452
return {
4553
issues: result.issues.map(({ message, path }) => ({
4654
message,
47-
path: path?.map(({ key }) => key) as string[]
55+
path: (path as Exclude<IssuePathItem, SetPathItem>[])?.map(({ key }) => key) as string[]
4856
})),
4957
success: false
5058
};
@@ -54,7 +62,7 @@ function _valibot<T extends SupportedSchemas>(
5462
schema: T,
5563
options: Omit<ToJSONSchemaOptions, 'schema'> &
5664
AdapterOptions<Infer<T>> & {
57-
config?: SchemaConfig;
65+
config?: Config<GenericIssue<unknown>>;
5866
} = {}
5967
): ValidationAdapter<Infer<T>, InferIn<T>> {
6068
return createAdapter({

src/routes/(v2)/v2/issue-366/schema.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ import {
44
object,
55
boolean,
66
minLength,
7-
toTrimmed,
7+
trim,
88
picklist,
9+
pipe,
910
literal,
1011
date
1112
} from 'valibot';
1213

1314
const roleOptions = ['USER', 'PREMIUM', 'ADMIN'] as const;
1415

1516
export const userSchema = object({
16-
firstName: string([toTrimmed(), minLength(1, 'Please enter your first name.')]),
17-
lastName: string([toTrimmed(), minLength(1, 'Please enter your last name.')]),
18-
email: string([
19-
toTrimmed(),
17+
firstName: pipe(string(), trim(), minLength(1, 'Please enter your first name.')),
18+
lastName: pipe(string(), trim(), minLength(1, 'Please enter your last name.')),
19+
email: pipe(
20+
string(),
21+
trim(),
2022
email('The email address is not valid.'),
2123
minLength(1, 'An email address is required.')
22-
]),
24+
),
2325
role: picklist(roleOptions, 'You must have a role.'),
2426
verified: boolean(),
2527
terms: literal(true, 'You must accept the terms and privacy policy.'),
@@ -29,11 +31,12 @@ export const userSchema = object({
2931
});
3032

3133
export const emailSchema = object({
32-
email: string([
33-
toTrimmed(),
34+
email: pipe(
35+
string(),
36+
trim(),
3437
email('The email address is not valid.'),
3538
minLength(1, 'An email address is required.')
36-
])
39+
)
3740
});
3841

3942
export type UserSchema = typeof userSchema;

src/routes/(v2)/v2/nested-validation-valibot/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { array, integer, minLength, minValue, number, object, string } from 'valibot';
1+
import { array, integer, minLength, minValue, number, object, pipe, string } from 'valibot';
22

33
/*
44
export const schema2 = z
@@ -15,11 +15,11 @@ export const schema2 = z
1515
*/
1616

1717
export const schema = object({
18-
name: string([minLength(1, 'Name is too short')]),
18+
name: pipe(string(), minLength(1, 'Name is too short')),
1919
tags: array(
2020
object({
21-
id: number([integer(), minValue(3)]),
22-
name: string([minLength(2)])
21+
id: pipe(number(), integer(), minValue(3)),
22+
name: pipe(string(), minLength(2))
2323
})
2424
)
2525
});

0 commit comments

Comments
 (0)