Skip to content

Commit 3c18fd3

Browse files
committed
feat(validation): add Yup adapter
1 parent bdf42f1 commit 3c18fd3

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

packages/next-safe-action/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@
8484
"typescript": "^5.5.3",
8585
"typescript-eslint": "^7.8.0",
8686
"valibot": "^0.36.0",
87+
"yup": "^1.4.0",
8788
"zod": "^3.23.6"
8889
},
8990
"peerDependencies": {
9091
"next": ">= 14.0.0",
9192
"react": ">= 18.2.0",
9293
"react-dom": ">= 18.2.0",
9394
"valibot": ">= 0.36.0",
94-
"zod": ">= 3.0.0"
95+
"zod": ">= 3.0.0",
96+
"yup": ">= 1.0.0"
9597
},
9698
"peerDependenciesMeta": {
9799
"zod": {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// https://github.com/decs/typeschema/blob/main/packages/yup/src/validation.ts
2+
3+
import type { Schema as YupSchema } from "yup";
4+
import { ValidationError } from "yup";
5+
import type { IfInstalled, Infer, ValidationAdapter, ValidationIssue } from "../adapters.types";
6+
7+
class YupAdapter implements ValidationAdapter {
8+
async validate<S extends IfInstalled<YupSchema>>(schema: S, data: unknown) {
9+
try {
10+
const result = await schema.validate(data, { strict: true });
11+
12+
return {
13+
success: true,
14+
data: result as Infer<S>,
15+
} as const;
16+
} catch (e) {
17+
if (e instanceof ValidationError) {
18+
const { message, path } = e;
19+
20+
return {
21+
success: false,
22+
issues: [
23+
{
24+
message,
25+
path: path && path.length > 0 ? path : undefined,
26+
},
27+
] as ValidationIssue[],
28+
} as const;
29+
}
30+
31+
throw e;
32+
}
33+
}
34+
}
35+
36+
export function yupAdapter() {
37+
return new YupAdapter();
38+
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)