fastify-response-validation
fastify-response-validation copied to clipboard
fix: assign schemas to host ajv instance
Fixes the issue #113 by adding the schemas to the internal avj instance used by the plugin
Checklist
- [x] run
npm run testandnpm run benchmark - [ ] commit message and code follows the Developer's Certification of Origin and the Code of conduct
@diogosilva95 The CI is failing, can you add a unit test as well please?
@Fdawgs added the test and fixed lint :)
Note sure if it is a good idea, when encapsulation is exist, you can have same
$idwith different content.
would you provide an example please? when you add a schema to the ajv instance with an existing id it throws an error
when you add a schema to the ajv instance with an existing id it throws an error
That is the exact problem I mention, fastify allows the same $id of schema exists across different encapsulated context.
You can see the below foo schema shared the same name but different properties.
import fastify from "fastify";
const app = fastify();
app.register(async (instance) => {
instance.addSchema({
$id: 'foo',
type: 'object',
properties: {
foo: { type: 'string' }
}
})
})
app.register(async (instance) => {
instance.addSchema({
$id: 'foo',
type: 'object',
properties: {
foo: { type: 'string' },
bar: { type: 'string' }
}
})
})
await app.ready()
Isn't addSchema encapsulation aware?
The ajv instance is encapsulated too.
The problem is similar to @fastify/swagger. The instance of ajv is global in the root context of registered level.
So, the problem still persist, unless it creates a new instance of ajv on each new context creation.