Skip to content

Commit 107e7cc

Browse files
committed
fix: Consider schema encapsulation.
1 parent b4faa69 commit 107e7cc

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

lib/validation.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function addResponseValidation(route) {
176176
This makes possible to handle shared schemas.
177177
*/
178178
this[interfaces_1.kHttpErrorsEnhancedResponseValidations].push([
179+
this,
179180
validators,
180181
Object.entries(route.schema.response)
181182
]);
@@ -206,18 +207,18 @@ function addResponseValidation(route) {
206207
}
207208
exports.addResponseValidation = addResponseValidation;
208209
function compileResponseValidationSchema() {
209-
const validator = new ajv_1.default({
210-
// The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
211-
removeAdditional: false,
212-
useDefaults: true,
213-
coerceTypes: false,
214-
allErrors: true,
215-
nullable: true
216-
});
217-
validator.addSchema(Object.values(this.getSchemas()));
218-
for (const [validators, schemas] of this[interfaces_1.kHttpErrorsEnhancedResponseValidations]) {
210+
for (const [instance, validators, schemas] of this[interfaces_1.kHttpErrorsEnhancedResponseValidations]) {
211+
const compiler = new ajv_1.default({
212+
// The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
213+
removeAdditional: false,
214+
useDefaults: true,
215+
coerceTypes: false,
216+
allErrors: true,
217+
nullable: true
218+
});
219+
compiler.addSchema(Object.values(instance.getSchemas()));
219220
for (const [code, schema] of schemas) {
220-
validators[code] = validator.compile(schema);
221+
validators[code] = compiler.compile(schema);
221222
}
222223
}
223224
}

src/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ declare module 'fastify' {
77
// eslint-disable-next-line @typescript-eslint/no-unused-vars
88
interface FastifyInstance {
99
responseValidatorSchemaCompiler: Ajv
10-
[kHttpErrorsEnhancedResponseValidations]: Array<[ResponseSchemas, Array<[string, object]>]>
10+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
11+
[kHttpErrorsEnhancedResponseValidations]: Array<[FastifyInstance, ResponseSchemas, Array<[string, object]>]>
1112
}
1213

1314
// eslint-disable-next-line @typescript-eslint/no-unused-vars

src/validation.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
208208
This makes possible to handle shared schemas.
209209
*/
210210
this[kHttpErrorsEnhancedResponseValidations].push([
211+
this,
211212
validators,
212213
Object.entries(route.schema.response as { [key: string]: object })
213214
])
@@ -251,20 +252,20 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
251252
}
252253

253254
export function compileResponseValidationSchema(this: FastifyInstance): void {
254-
const validator = new Ajv({
255-
// The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
256-
removeAdditional: false,
257-
useDefaults: true,
258-
coerceTypes: false,
259-
allErrors: true,
260-
nullable: true
261-
})
262-
263-
validator.addSchema(Object.values(this.getSchemas()))
264-
265-
for (const [validators, schemas] of this[kHttpErrorsEnhancedResponseValidations]) {
255+
for (const [instance, validators, schemas] of this[kHttpErrorsEnhancedResponseValidations]) {
256+
const compiler = new Ajv({
257+
// The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
258+
removeAdditional: false,
259+
useDefaults: true,
260+
coerceTypes: false,
261+
allErrors: true,
262+
nullable: true
263+
})
264+
265+
compiler.addSchema(Object.values(instance.getSchemas()))
266+
266267
for (const [code, schema] of schemas) {
267-
validators[code] = validator.compile(schema)
268+
validators[code] = compiler.compile(schema)
268269
}
269270
}
270271
}

types/interfaces.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export declare const kHttpErrorsEnhancedResponseValidations: unique symbol;
55
declare module 'fastify' {
66
interface FastifyInstance {
77
responseValidatorSchemaCompiler: Ajv;
8-
[kHttpErrorsEnhancedResponseValidations]: Array<[ResponseSchemas, Array<[string, object]>]>;
8+
[kHttpErrorsEnhancedResponseValidations]: Array<[FastifyInstance, ResponseSchemas, Array<[string, object]>]>;
99
}
1010
interface FastifyRequest {
1111
[kHttpErrorsEnhancedProperties]?: {

0 commit comments

Comments
 (0)