Skip to content

Commit 663a6e0

Browse files
committed
feat: update valibot support (#4415)
1 parent 10f301c commit 663a6e0

File tree

7 files changed

+260
-411
lines changed

7 files changed

+260
-411
lines changed

.changeset/spotty-queens-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vee-validate/valibot': patch
3+
---
4+
5+
feat: upgrade valibot support

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"shiki": "^0.14.3",
3131
"tailwindcss": "^3.3.3",
3232
"unist-util-visit": "^5.0.0",
33-
"valibot": "^0.12.0",
33+
"valibot": "^0.13.0",
3434
"vee-validate": "^4.11.1",
3535
"vue": "^3.3.0",
3636
"yup": "^1.2.0",

docs/src/pages/guide/composition-api/typed-schema.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,15 @@ You can also define default values on your schema directly and it will be picked
331331

332332
```ts
333333
import { useForm } from 'vee-validate';
334-
import { object, string, useDefault, minLength } from 'valibot';
334+
import { object, string, withDefault, minLength } from 'valibot';
335335
import { toTypedSchema } from '@vee-validate/valibot';
336336

337337
const { values, handleSubmit } = useForm({
338338
validationSchema: toTypedSchema(
339339
object({
340-
email: useDefault(string([minLength(1, 'required')]), '[email protected]'),
341-
password: useDefault(string([minLength(1, 'required')]), ''),
342-
name: useDefault(string(), ''),
340+
email: withDefault(string([minLength(1, 'required')]), '[email protected]'),
341+
password: withDefault(string([minLength(1, 'required')]), ''),
342+
name: withDefault(string(), ''),
343343
}),
344344
),
345345
});

packages/valibot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
],
3030
"dependencies": {
3131
"type-fest": "^4.2.0",
32-
"valibot": "^0.12.0",
32+
"valibot": "^0.13.0",
3333
"vee-validate": "4.11.2"
3434
}
3535
}

packages/valibot/src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { PartialDeep } from 'type-fest';
22
import type { TypedSchema, TypedSchemaError } from 'vee-validate';
3-
import { Output, Input, BaseSchema, safeParseAsync, parse, Issue } from 'valibot';
3+
import { Output, Input, BaseSchema, BaseSchemaAsync, safeParseAsync, safeParse, Issue } from 'valibot';
44
import { normalizeFormPath } from '../../shared';
55

66
export function toTypedSchema<
7-
TSchema extends BaseSchema,
7+
TSchema extends BaseSchema | BaseSchemaAsync,
88
TOutput = Output<TSchema>,
99
TInput = PartialDeep<Input<TSchema>>,
1010
>(valibotSchema: TSchema): TypedSchema<TInput, TOutput> {
@@ -20,18 +20,20 @@ export function toTypedSchema<
2020
}
2121

2222
const errors: Record<string, TypedSchemaError> = {};
23-
processIssues(result.error.issues, errors);
23+
processIssues(result.issues, errors);
2424

2525
return {
2626
errors: Object.values(errors),
2727
};
2828
},
2929
cast(values) {
30-
try {
31-
return parse(valibotSchema, values);
32-
} catch {
30+
if (valibotSchema.async) {
3331
return values;
3432
}
33+
34+
const result = safeParse(valibotSchema, values);
35+
36+
return result.success ? result.data : values;
3537
},
3638
};
3739

packages/valibot/tests/valibot.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Ref } from 'vue';
22
import { useField, useForm } from '@/vee-validate';
3-
import { string, minLength, email as emailV, object, coerce, any, number, useDefault, optional } from 'valibot';
3+
import { string, minLength, email as emailV, object, coerce, any, number, withDefault, optional } from 'valibot';
44
import { toTypedSchema } from '@/valibot';
55
import { mountWithHoc, flushPromises, setValue } from '../../vee-validate/tests/helpers';
66

@@ -227,7 +227,7 @@ describe('valibot', () => {
227227
setup() {
228228
const schema = toTypedSchema(
229229
object({
230-
age: useDefault(number(), 11),
230+
age: withDefault(number(), 11),
231231
}),
232232
);
233233

@@ -261,13 +261,13 @@ describe('valibot', () => {
261261
setup() {
262262
const schema = toTypedSchema(
263263
object({
264-
name: useDefault(string(), 'test'),
265-
age: useDefault(number(), 11),
264+
name: withDefault(string(), 'test'),
265+
age: withDefault(number(), 11),
266266
unknownKey: optional(string()),
267-
object: useDefault(
267+
object: withDefault(
268268
object({
269269
nestedKey: optional(string()),
270-
nestedDefault: useDefault(string(), 'nested'),
270+
nestedDefault: withDefault(string(), 'nested'),
271271
}),
272272
{} as any,
273273
),

0 commit comments

Comments
 (0)