Skip to content

Commit

Permalink
feat: make sure all validation errors have a statusCode set
Browse files Browse the repository at this point in the history
As part of the error handling refactoring of #3261, we should
not be setting a custom status code for validation routes.
We should rely only on the error.statusCode property instead and
leave the user full customization capabilities. Unfortunately
a change was missed.

Ref: #3261
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jun 21, 2022
1 parent 1f0dc84 commit 0c2baff
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/handleRequest.js
Expand Up @@ -90,7 +90,7 @@ function preValidationCallback (err, request, reply) {
const result = validateSchema(reply.context, request)
if (result) {
if (reply.context.attachValidation === false) {
reply.code(400).send(result)
reply.send(result)
return
}

Expand Down
2 changes: 2 additions & 0 deletions lib/validation.js
Expand Up @@ -106,11 +106,13 @@ function validate (context, request) {

function wrapValidationError (result, dataVar, schemaErrorFormatter) {
if (result instanceof Error) {
result.statusCode = result.statusCode || 400
result.validationContext = result.validationContext || dataVar
return result
}

const error = schemaErrorFormatter(result, dataVar)
error.statusCode = error.statusCode || 400
error.validation = result
error.validationContext = dataVar
return error
Expand Down
2 changes: 1 addition & 1 deletion test/reply-error.test.js
Expand Up @@ -324,7 +324,7 @@ test('invalid schema - ajv', t => {

fastify.setErrorHandler((err, request, reply) => {
t.ok(Array.isArray(err.validation))
reply.send('error')
reply.code(400).send('error')
})

fastify.inject({
Expand Down
2 changes: 1 addition & 1 deletion test/schema-feature.test.js
Expand Up @@ -826,7 +826,7 @@ test('Validation context in validation result', t => {
t.equal(err instanceof Error, true)
t.ok(err.validation, 'detailed errors')
t.equal(err.validationContext, 'body')
reply.send()
reply.code(400).send()
})
fastify.post('/', {
handler: echoParams,
Expand Down
32 changes: 32 additions & 0 deletions test/validation-error-handling.test.js
Expand Up @@ -97,6 +97,38 @@ test('should be able to use setErrorHandler specify custom validation error', t
})
})

test('validation error has 400 statusCode set', t => {
t.plan(3)

const fastify = Fastify()

fastify.setErrorHandler((error, request, reply) => {
const errorResponse = {
message: error.message,
statusCode: error.statusCode || 500
}

reply.code(errorResponse.statusCode).send(errorResponse)
})

fastify.post('/', { schema }, echoBody)

fastify.inject({
method: 'POST',
payload: {
hello: 'michelangelo'
},
url: '/'
}, (err, res) => {
t.error(err)
t.same(res.json(), {
statusCode: 400,
message: "body must have required property 'name'"
})
t.equal(res.statusCode, 400)
})
})

test('error inside custom error handler should have validationContext', t => {
t.plan(1)

Expand Down

0 comments on commit 0c2baff

Please sign in to comment.