Closed
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.0.0
Plugin version
5.2.0
Node.js version
19.4
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13
Description
Currently, fastify schema validation and the pg: {transact: true}
option are not compatible.
The validation schema is executed before the transaction initialization, which is expected behavior. However, when an error response with code FST_ERR_VALIDATION
is sent, it triggers the onSend
handler of the fastify-postgres hook, which assumes the initialization of a client.
This cause the Cannot read properties of null (reading 'release')
error.
Steps to Reproduce
Configure your server:
fastify.post('/strictValidateFail', {
schema: {
body: {
type: 'object',
properties: {
hello: { type: 'string' }
},
required: ['hello']
}
},
pg: { transact: true }
}, async (req, reply) => {
await req.pg.query('THIS QUERY SHOULD NEVER BE EXECUTED WITH WRONG BODY', [])
return 'never catched'
});
Send a wrong body post
const response = await fastify.inject({
url: '/strictValidateFail',
method: 'POST',
body: { notValid: 'json input' }
})
the result is:
{"statusCode":400,"error":"Bad Request","message":"Cannot read properties of null (reading 'release')"}
Expected Behavior
The response should be
{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body must have required property 'hello'"}
Metadata
Metadata
Assignees
Labels
No labels