2023-02-10 17:18:27 +01:00
|
|
|
import type { Context } from '../context.ts'
|
2023-02-13 22:21:30 +01:00
|
|
|
import type { Env, ValidationTargets, MiddlewareHandler } from '../types.ts'
|
2023-02-24 18:36:11 +01:00
|
|
|
import { parseBody } from '../utils/body.ts'
|
2022-12-28 03:25:48 +01:00
|
|
|
|
2023-02-13 22:21:30 +01:00
|
|
|
type ValidationTargetKeysWithBody = 'form' | 'json'
|
|
|
|
type ValidationTargetByMethod<M> = M extends 'get' | 'head' // GET and HEAD request must not have a body content.
|
|
|
|
? Exclude<keyof ValidationTargets, ValidationTargetKeysWithBody>
|
|
|
|
: keyof ValidationTargets
|
2023-01-03 01:09:04 +01:00
|
|
|
|
2023-03-15 14:20:50 +01:00
|
|
|
export type ValidationFunction<
|
|
|
|
InputType,
|
|
|
|
OutputType,
|
|
|
|
E extends Env = {},
|
|
|
|
P extends string = string
|
|
|
|
> = (value: InputType, c: Context<E, P>) => OutputType | Response | Promise<Response>
|
2023-03-11 14:17:08 +01:00
|
|
|
|
2022-12-29 06:59:57 +01:00
|
|
|
export const validator = <
|
2023-03-11 14:17:08 +01:00
|
|
|
InputType,
|
2023-01-22 13:21:37 +01:00
|
|
|
P extends string,
|
|
|
|
M extends string,
|
2023-02-13 22:21:30 +01:00
|
|
|
U extends ValidationTargetByMethod<M>,
|
2023-03-11 14:17:08 +01:00
|
|
|
OutputType = ValidationTargets[U],
|
2023-03-15 14:20:50 +01:00
|
|
|
P2 extends string = P,
|
2023-03-11 14:17:08 +01:00
|
|
|
V extends {
|
|
|
|
in: { [K in U]: unknown extends InputType ? OutputType : InputType }
|
|
|
|
out: { [K in U]: OutputType }
|
|
|
|
} = {
|
|
|
|
in: { [K in U]: unknown extends InputType ? OutputType : InputType }
|
|
|
|
out: { [K in U]: OutputType }
|
|
|
|
},
|
2023-01-16 14:57:47 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
E extends Env = any
|
2022-12-29 06:59:57 +01:00
|
|
|
>(
|
2023-02-13 22:21:30 +01:00
|
|
|
target: U,
|
2023-03-14 15:43:33 +01:00
|
|
|
validationFunc: ValidationFunction<
|
|
|
|
unknown extends InputType ? ValidationTargets[U] : InputType,
|
|
|
|
OutputType,
|
2023-03-15 14:20:50 +01:00
|
|
|
E,
|
|
|
|
P2
|
2023-03-14 15:43:33 +01:00
|
|
|
>
|
2023-01-22 13:21:37 +01:00
|
|
|
): MiddlewareHandler<E, P, V> => {
|
2022-12-29 06:59:57 +01:00
|
|
|
return async (c, next) => {
|
2022-12-28 03:25:48 +01:00
|
|
|
let value = {}
|
|
|
|
|
2023-02-13 22:21:30 +01:00
|
|
|
switch (target) {
|
2022-12-28 03:25:48 +01:00
|
|
|
case 'json':
|
2023-01-03 00:15:23 +01:00
|
|
|
try {
|
2023-02-24 18:36:11 +01:00
|
|
|
value = await c.req.raw.clone().json()
|
2023-01-03 00:15:23 +01:00
|
|
|
} catch {
|
|
|
|
console.error('Error: Malformed JSON in request body')
|
|
|
|
return c.json(
|
|
|
|
{
|
|
|
|
success: false,
|
|
|
|
message: 'Malformed JSON in request body',
|
|
|
|
},
|
|
|
|
400
|
|
|
|
)
|
|
|
|
}
|
2022-12-28 03:25:48 +01:00
|
|
|
break
|
|
|
|
case 'form':
|
2023-02-24 18:36:11 +01:00
|
|
|
value = await parseBody(c.req.raw.clone())
|
2022-12-28 03:25:48 +01:00
|
|
|
break
|
|
|
|
case 'query':
|
2023-03-16 13:49:28 +01:00
|
|
|
value = Object.fromEntries(
|
|
|
|
Object.entries(c.req.queries()).map(([k, v]) => {
|
|
|
|
return v.length === 1 ? [k, v[0]] : [k, v]
|
|
|
|
})
|
|
|
|
)
|
2022-12-28 03:25:48 +01:00
|
|
|
break
|
|
|
|
case 'queries':
|
|
|
|
value = c.req.queries()
|
|
|
|
break
|
2023-03-15 14:20:50 +01:00
|
|
|
case 'param':
|
|
|
|
value = c.req.param() as Record<string, string>
|
|
|
|
break
|
2022-12-28 03:25:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 14:20:50 +01:00
|
|
|
const res = validationFunc(value as never, c as never)
|
2022-12-28 03:25:48 +01:00
|
|
|
|
2022-12-29 06:59:57 +01:00
|
|
|
if (res instanceof Response || res instanceof Promise) {
|
2022-12-28 03:25:48 +01:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-02-13 22:21:30 +01:00
|
|
|
c.req.addValidatedData(target, res as never)
|
2022-12-28 03:25:48 +01:00
|
|
|
|
|
|
|
await next()
|
|
|
|
}
|
|
|
|
}
|