Skip to content

Commit

Permalink
fix extend isCustomValidatorCompiler from parent controller (#4903)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Jul 15, 2023
1 parent 56ca418 commit c65a6be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/schema-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class SchemaController {
this.schemaBucket = this.opts.bucket(parent.getSchemas())
this.validatorCompiler = parent.getValidatorCompiler()
this.serializerCompiler = parent.getSerializerCompiler()
this.isCustomValidatorCompiler = parent.isCustomValidatorCompiler
this.isCustomSerializerCompiler = parent.isCustomSerializerCompiler
this.parent = parent
} else {
this.schemaBucket = this.opts.bucket()
Expand Down
41 changes: 40 additions & 1 deletion test/internals/request-validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ test('Nested Context', subtest => {
subtest.test('Level_1', tst => {
tst.plan(3)
tst.test('#compileValidationSchema', ntst => {
ntst.plan(4)
ntst.plan(5)

ntst.test('Should return a function - Route without schema', async t => {
const fastify = Fastify()
Expand Down Expand Up @@ -896,6 +896,45 @@ test('Nested Context', subtest => {
await fastify.inject('/')
}
)

ntst.test('Should compile the custom validation - nested with schema.headers', async t => {
const fastify = Fastify()
let called = false

const schemaWithHeaders = {
headers: {
'x-foo': {
type: 'string'
}
}
}

const custom = ({ schema, httpPart, url, method }) => {
if (called) return () => true
// only custom validators keep the same headers object
t.equal(schema, schemaWithHeaders.headers)
t.equal(url, '/')
t.equal(httpPart, 'headers')
called = true
return () => true
}

t.plan(4)

fastify.setValidatorCompiler(custom)

fastify.register((instance, opts, next) => {
instance.get('/', { schema: schemaWithHeaders }, (req, reply) => {
t.equal(called, true)

reply.send({ hello: 'world' })
})

next()
})

await fastify.inject('/')
})
})

tst.test('#getValidationFunction', ntst => {
Expand Down

0 comments on commit c65a6be

Please sign in to comment.