mirror of
https://github.com/michivonah/themepark-assistant.git
synced 2025-12-23 14:36:29 +01:00
install zod for input validation
This commit is contained in:
parent
0b11cccf33
commit
9a4fd29fa5
4 changed files with 50 additions and 8 deletions
|
|
@ -24,8 +24,12 @@ export class MissingParameter extends HTTPException{
|
|||
}
|
||||
|
||||
export class InvalidParameter extends HTTPException{
|
||||
constructor(paramName: string){
|
||||
super(400, { message: `Provided parameter '${paramName}' is invalid.` })
|
||||
constructor(paramName?: string){
|
||||
super(400, { message:
|
||||
paramName
|
||||
? `Provided parameter '${paramName}' is invalid.`
|
||||
: 'Provided invalid parameter.'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
15
api/src/lib/http-z-validator.ts
Normal file
15
api/src/lib/http-z-validator.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import * as z from 'zod'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { InvalidParameter } from '../errors'
|
||||
|
||||
/**
|
||||
* Custom Zod Validator Middleware with support for HTTP Exceptions
|
||||
* @param type Part of HonoRequest object to get data from
|
||||
* @param schema Zod Validation scheme (docs: https://zod.dev/api)
|
||||
* @returns zValidator for running the validation
|
||||
*/
|
||||
export default function httpZValidator<T extends z.ZodTypeAny>(type: 'query' | 'json' | 'param' = 'query', schema: T){
|
||||
return zValidator(type, schema, (result, c) => {
|
||||
if(!result.success) throw new InvalidParameter();
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue