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: assign schemas to host ajv instance #126

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: avoid adding schemas to avj when already existing
diogosilva95 committed Nov 3, 2024
commit 6ac52ba0dfd12c6724d61bd6c4e3e1bc0282bd23
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -41,9 +41,16 @@ function fastifyResponseValidation (fastify, opts, next) {
}

function onRoute (routeOpts) {
const schemas = Object.values(fastify.getSchemas())

for (const schema of schemas) {
if (!ajv.getSchema(schema.$id)) {
ajv.addSchema(schema)
}
}

if (routeOpts.responseValidation === false) return
if (routeOpts.schema && routeOpts.schema.response) {
ajv.addSchema(Object.values(fastify.getSchemas()));
const responseStatusCodeValidation = routeOpts.responseStatusCodeValidation || opts.responseStatusCodeValidation || false
routeOpts.preSerialization = routeOpts.preSerialization || []
routeOpts.preSerialization.push(buildHook(routeOpts.schema.response, responseStatusCodeValidation))