Skip to content

Commit

Permalink
fix: visit schemas with custom prototype (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Sep 3, 2022
1 parent 8971520 commit fb68209
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/schemas.js
Expand Up @@ -62,6 +62,7 @@ function normalizeSchema (routeSchemas, serverOptions) {
// let's check if our schemas have a custom prototype
for (const key of ['headers', 'querystring', 'params', 'body']) {
if (typeof routeSchemas[key] === 'object' && Object.getPrototypeOf(routeSchemas[key]) !== Object.prototype) {
routeSchemas[kSchemaVisited] = true
return routeSchemas
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/schema-special-usage.test.js
Expand Up @@ -2,6 +2,7 @@

const { test } = require('tap')
const Joi = require('joi')
const yup = require('yup')
const AJV = require('ajv')
const S = require('fluent-json-schema')
const Fastify = require('..')
Expand Down Expand Up @@ -673,3 +674,31 @@ test('JOI validation overwrite request headers', t => {
})
})
})

test('Custom schema object should not trigger FST_ERR_SCH_DUPLICATE', async t => {
const fastify = Fastify()
const handler = () => { }

fastify.get('/the/url', {
schema: {
query: yup.object({
foo: yup.string()
})
},
validatorCompiler: ({ schema, method, url, httpPart }) => {
return function (data) {
// with option strict = false, yup `validateSync` function returns the coerced value if validation was successful, or throws if validation failed
try {
const result = schema.validateSync(data, {})
return { value: result }
} catch (e) {
return { error: e }
}
}
},
handler
})

await fastify.ready()
t.pass('fastify is ready')
})

0 comments on commit fb68209

Please sign in to comment.