Skip to content

Commit add7434

Browse files
authored
fix: wrap response validation errors when using joi (#323)
previously `joi` threw the raw `joi` errors when response validation failed, unlike `zod` validation which wrapped them in a `KoaRuntimeError` this change aligns the behavior to match `zod` schema builder also fixes a few documentation typos
1 parent 86fa31f commit add7434

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

packages/typescript-koa-runtime/src/joi.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {Schema as JoiSchema} from "joi"
22
import {KoaRuntimeError, type RequestInputType} from "./errors"
33

4-
/** @deprecated: update & re-generate to import from @nahkies/typescript-koa-runtime/server directly */
4+
/** @deprecated: update and re-generate to import from @nahkies/typescript-koa-runtime/server directly */
55
export type {Params} from "./server"
66

77
// Note: joi types don't appear to have an equivalent of z.infer,
@@ -48,32 +48,36 @@ export function responseValidationFactory(
4848
possibleResponses.sort((x, y) => (x[0] < y[0] ? -1 : 1))
4949

5050
return (status: number, value: unknown) => {
51-
for (const [match, schema] of possibleResponses) {
52-
const isMatch =
53-
(/^\d+$/.test(match) && String(status) === match) ||
54-
(/^\d[xX]{2}$/.test(match) && String(status)[0] === match[0])
51+
try {
52+
for (const [match, schema] of possibleResponses) {
53+
const isMatch =
54+
(/^\d+$/.test(match) && String(status) === match) ||
55+
(/^\d[xX]{2}$/.test(match) && String(status)[0] === match[0])
5556

56-
if (isMatch) {
57-
const result = schema.validate(value)
57+
if (isMatch) {
58+
const result = schema.validate(value)
59+
60+
if (result.error) {
61+
throw result.error
62+
}
63+
64+
return result.value
65+
}
66+
}
67+
68+
if (defaultResponse) {
69+
const result = defaultResponse.validate(value)
5870

5971
if (result.error) {
6072
throw result.error
6173
}
6274

6375
return result.value
6476
}
65-
}
66-
67-
if (defaultResponse) {
68-
const result = defaultResponse.validate(value)
6977

70-
if (result.error) {
71-
throw result.error
72-
}
73-
74-
return result.value
78+
return value
79+
} catch (err) {
80+
throw KoaRuntimeError.ResponseError(err)
7581
}
76-
77-
return value
7882
}
7983
}

packages/typescript-koa-runtime/src/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ export type ServerConfig = {
6868
/**
6969
* set to "disabled" to disable body parsing middleware, omit or pass undefined for defaults.
7070
*
71-
* if disabling, ensure you pass a body parsing middlware that places the parsed
71+
* if disabling, ensure you pass a body parsing middleware that places the parsed
7272
* body on `ctx.body` for request body processing to work.
7373
**/
7474
body?: "disabled" | KoaBodyMiddlewareOptions | undefined
7575

7676
/**
7777
*
78-
* useful for mounting logging, alternative body parsers, etc
78+
* useful for mounting logging, alternative body parsers, etc.
7979
*/
8080
middleware?: Middleware[]
8181

@@ -105,7 +105,7 @@ export type Params<Params, Query, Body, Header> = {
105105
* Enables CORS and body parsing by default. It's recommended to customize the CORS options
106106
* for production usage.
107107
*
108-
* If you need more control over your Koa server you should avoid calling this function,
108+
* If you need more control over your Koa server, you should avoid calling this function
109109
* and instead mount the router from your generated codes `createRouter` call directly
110110
* onto a server you have constructed.
111111
*/

packages/typescript-koa-runtime/src/zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {z} from "zod"
22
import {KoaRuntimeError, type RequestInputType} from "./errors"
33

4-
/** @deprecated: update & re-generate to import from @nahkies/typescript-koa-runtime/server directly */
4+
/** @deprecated: update and re-generate to import from @nahkies/typescript-koa-runtime/server directly */
55
export type {Params} from "./server"
66

77
export function parseRequestInput<Schema extends z.ZodTypeAny>(

0 commit comments

Comments
 (0)