Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: visit schemas with custom prototype #4248

Merged
merged 1 commit into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})