Skip to content

Commit

Permalink
improve validation error response
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkvon committed Oct 14, 2023
1 parent 3169f81 commit e85e0d1
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/middlewares/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type { Middleware } from 'koa'
const ajv = new Ajv2020({ allErrors: true })
addFormats(ajv)

/**
* This middleware generator accepts json-schema and returns Middleware
* It checks that request body matches the given schema,
* and responds with 400, and validation errors if schema is not satisfied
* The response data detail contains raw validation errors that ajv provides
* maybe TODO: return nicer (more human-readable) validation errors
*/
export const validateBody =
(schema: Parameters<typeof ajv.compile>[0]): Middleware =>
async (ctx, next) => {
Expand All @@ -15,6 +22,9 @@ export const validateBody =
else {
ctx.response.status = 400
ctx.response.type = 'json'
ctx.response.body = validate.errors
ctx.response.body = {
message: 'Invalid data',
detail: validate.errors,
}
}
}

0 comments on commit e85e0d1

Please sign in to comment.