Make new routing system the default
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -8,6 +8,8 @@ import {Logging} from '../service/Logging'
|
||||
/** Type tag for a validated runtime type. */
|
||||
export type Valid<T> = TypeTag<'@extollo/lib:Valid'> & T
|
||||
|
||||
export type ValidatorFactory<T extends Validator<T>> = T | (() => T)
|
||||
|
||||
/**
|
||||
* Error thrown if the schema for a validator cannot be located.
|
||||
*/
|
||||
|
||||
22
src/validation/middleware.ts
Normal file
22
src/validation/middleware.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {Request} from '../http/lifecycle/Request'
|
||||
import {Validator} from './Validator'
|
||||
import {ZodError} from 'zod'
|
||||
import {HTTPStatus, left, right} from '../util'
|
||||
import {json} from '../http/response/JSONResponseFactory'
|
||||
|
||||
export function validateMiddleware<T>(validator: Validator<T>) {
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
return (request: Request) => {
|
||||
try {
|
||||
const data = validator.parse(request.parsedInput)
|
||||
return right(data)
|
||||
} catch (e) {
|
||||
if ( e instanceof ZodError ) {
|
||||
// FIXME render this better
|
||||
return left(json(e.formErrors).status(HTTPStatus.BAD_REQUEST))
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user