Skip to content

Commit 5995e88

Browse files
authored
feat: make zod errors better (#97)
* feat: make zod errors better * address
1 parent f9cf226 commit 5995e88

File tree

7 files changed

+79
-27
lines changed

7 files changed

+79
-27
lines changed

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"react-virtualized-auto-sizer": "^1.0.20",
4848
"react-window": "^1.8.10",
4949
"stainless": "workspace:*",
50-
"zod": "^3.22.4",
50+
"zod": "^3.25.76",
5151
"zustand": "^4.4.7"
5252
},
5353
"devDependencies": {

packages/hono/src/honoPlugin.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,41 @@ describe("basic routing", () => {
125125
"expected": "object",
126126
"message": "Required",
127127
"path": [
128-
"<stainless request body>",
128+
"<body>",
129129
],
130130
"received": "undefined",
131131
},
132132
],
133+
"message": "Required at "<body>"",
134+
}
135+
`);
136+
});
137+
138+
test("update posts, missing param", async () => {
139+
const response = await app.request("/api/posts/5", {
140+
method: "POST",
141+
headers: {
142+
"content-type": "application/json",
143+
},
144+
body: JSON.stringify({}),
145+
});
146+
expect(response).toHaveProperty("status", 400);
147+
expect(await response.json()).toMatchInlineSnapshot(`
148+
{
149+
"error": "bad request",
150+
"issues": [
151+
{
152+
"code": "invalid_type",
153+
"expected": "string",
154+
"message": "Required",
155+
"path": [
156+
"<body>",
157+
"content",
158+
],
159+
"received": "undefined",
160+
},
161+
],
162+
"message": "Validation error: Required at "<body>.content"",
133163
}
134164
`);
135165
});

packages/prisma/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"lodash": "^4.17.21",
3434
"stainless": "workspace:*",
35-
"zod": "^3.22.4"
35+
"zod": "^3.25.76"
3636
},
3737
"peerDependencies": {
3838
"prisma": "^4.0.0"

packages/stainless/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"lodash": "^4.17.21",
5151
"qs": "^6.11.2",
5252
"ts-node": "^10.9.1",
53-
"zod": "^3.22.4",
54-
"zod-openapi": "github:stainless-api/zod-openapi#2.8.0"
53+
"zod": "^3.25.76",
54+
"zod-openapi": "github:stainless-api/zod-openapi#2.8.0",
55+
"zod-validation-error": "^4.0.1"
5556
}
5657
}

packages/stainless/src/stl.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as z from "./z";
22
import { openapiSpec } from "./openapiSpec";
33
import type { OpenAPIObject } from "zod-openapi/lib-types/openapi3-ts/dist/oas31";
4+
import { fromZodError } from "zod-validation-error/v3";
45
import coerceParams from "./coerceParams";
56
export { openapiSpec };
67
export { type SelectTree, parseSelect } from "./parseSelect";
@@ -778,20 +779,27 @@ export class Stl<Plugins extends AnyPlugins> {
778779
context.endpoint[coercedQuery] ??= coerceParams(context.endpoint.query);
779780
context.parsedParams.query = await context.endpoint[coercedQuery]
780781
.parseAsync(params.query, parseParams)
781-
.catch(prependZodPath("<stainless request query>"));
782+
.catch(prependZodPath("<query>"));
782783
}
783784
if (context.endpoint.path) {
784785
context.endpoint[coercedPath] ??= coerceParams(context.endpoint.path);
785786
context.parsedParams.path = await context.endpoint[coercedPath]
786787
.parseAsync(params.path, parseParams)
787-
.catch(prependZodPath("<stainless request path>"));
788+
.catch(prependZodPath("<path>"));
788789
}
789790
context.parsedParams.body = await context.endpoint.body
790791
?.parseAsync(params.body, parseParams)
791-
.catch(prependZodPath("<stainless request body>"));
792+
.catch(prependZodPath("<body>"));
792793
} catch (error) {
793794
if (error instanceof z.ZodError) {
794-
throw new BadRequestError({ issues: error.issues });
795+
let validationError = fromZodError(error).message;
796+
if (validationError.startsWith("Validation error: ")) {
797+
validationError = validationError.slice("Validation error: ".length);
798+
}
799+
throw new BadRequestError({
800+
message: validationError,
801+
issues: error.issues,
802+
});
795803
}
796804
throw error;
797805
}

packages/ts-to-zod/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
"lodash": "^4.17.21",
3535
"pkg-up": "~3.1.0",
3636
"ts-morph": "^19.0.0",
37-
"zod": "^3.22.4"
37+
"zod": "^3.25.76"
3838
}
3939
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)