Skip to content

Commit 9974916

Browse files
authored
feat: initial support for valibot (#4395)
1 parent 2d8143f commit 9974916

File tree

9 files changed

+517
-22
lines changed

9 files changed

+517
-22
lines changed

.changeset/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@vee-validate/rules",
1111
"@vee-validate/zod",
1212
"@vee-validate/yup",
13+
"@vee-validate/valibot",
1314
"@vee-validate/nuxt"
1415
]
1516
],

.changeset/shiny-apricots-fix.md

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ dist
1313
.DS_STORE
1414

1515
lerna-debug.log
16+
packages/*/src/playground.ts

packages/valibot/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@vee-validate/valibot",
3+
"version": "4.10.9",
4+
"description": "vee-validate integration with valibot schema validation",
5+
"author": "Abdelrahman Awad <[email protected]>",
6+
"license": "MIT",
7+
"module": "dist/vee-validate-valibot.esm.js",
8+
"unpkg": "dist/vee-validate-valibot.js",
9+
"main": "dist/vee-validate-valibot.js",
10+
"types": "dist/vee-validate-valibot.d.ts",
11+
"homepage": "https://vee-validate.logaretm.com/v4/integrations/zod-schema-validation/",
12+
"repository": {
13+
"url": "https://github.com/logaretm/vee-validate.git",
14+
"type": "git",
15+
"directory": "packages/valibot"
16+
},
17+
"sideEffects": false,
18+
"keywords": [
19+
"VueJS",
20+
"Vue",
21+
"validation",
22+
"validator",
23+
"inputs",
24+
"form"
25+
],
26+
"files": [
27+
"dist/*.js",
28+
"dist/*.d.ts"
29+
],
30+
"dependencies": {
31+
"type-fest": "^4.0.0",
32+
"valibot": "^0.7.0",
33+
"vee-validate": "^4.10.8"
34+
}
35+
}

packages/valibot/src/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { PartialDeep } from 'type-fest';
2+
import type { TypedSchema, TypedSchemaError } from 'vee-validate';
3+
import { Output, Input, BaseSchema, safeParseAsync, parse, Issue } from 'valibot';
4+
import { normalizeFormPath } from '../../shared';
5+
6+
export function toTypedSchema<
7+
TSchema extends BaseSchema,
8+
TOutput = Output<TSchema>,
9+
TInput = PartialDeep<Input<TSchema>>,
10+
>(valibotSchema: TSchema): TypedSchema<TInput, TOutput> {
11+
const schema: TypedSchema = {
12+
__type: 'VVTypedSchema',
13+
async parse(value) {
14+
const result = await safeParseAsync(valibotSchema, value);
15+
if (result.success) {
16+
return {
17+
value: result.data,
18+
errors: [],
19+
};
20+
}
21+
22+
const errors: Record<string, TypedSchemaError> = {};
23+
processIssues(result.error.issues, errors);
24+
25+
return {
26+
errors: Object.values(errors),
27+
};
28+
},
29+
cast(values) {
30+
try {
31+
return parse(valibotSchema, values);
32+
} catch {
33+
return values;
34+
}
35+
},
36+
};
37+
38+
return schema;
39+
}
40+
41+
function processIssues(issues: Issue[], errors: Record<string, TypedSchemaError>): void {
42+
issues.forEach(issue => {
43+
const path = normalizeFormPath((issue.path || []).map(p => p.key).join('.'));
44+
if (issue.issues?.length) {
45+
processIssues(
46+
issue.issues.flatMap(ue => ue.issues || []),
47+
errors,
48+
);
49+
50+
if (!path) {
51+
return;
52+
}
53+
}
54+
55+
if (!errors[path]) {
56+
errors[path] = { errors: [], path };
57+
}
58+
59+
errors[path].errors.push(issue.message);
60+
});
61+
}

0 commit comments

Comments
 (0)