Skip to content

Commit bed3940

Browse files
author
Karel Marek
committed
feat: Add lazyCompileValidationSchemas option
1 parent f3c9884 commit bed3940

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

docs/Options.md

+6
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,9 @@ If set, then when encountering a validation error Exegesis will return
233233
all errors found in the document instead of just the first error. This
234234
causes Exegesis to spend more time on requests with errors in them, so
235235
for performance reasons this is disabled by default.
236+
237+
## lazyCompileValidationSchemas
238+
239+
Response and request schemas are compiled by ajv to make validation faster. However compilation is slow
240+
and can cause compilation of API to take long time. Enabling this will cause validation schemas
241+
compilation to be executed when the validator is needed.

src/oas3/Schema/validators.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function doValidate(
117117
schemaPtr: string,
118118
parameterLocation: ParameterLocation,
119119
parameterRequired: boolean,
120-
ajvValidate: ValidateFunction,
120+
getAjvValidate: () => ValidateFunction,
121121
json: any
122122
) {
123123
const value = { value: json };
@@ -142,6 +142,7 @@ function doValidate(
142142
}
143143

144144
if (!errors) {
145+
const ajvValidate = getAjvValidate();
145146
ajvValidate(value);
146147
if (ajvValidate.errors) {
147148
errors = ajvValidate.errors.map((err) => {
@@ -167,6 +168,23 @@ function doValidate(
167168
return { errors, value: value.value };
168169
}
169170

171+
function createValidateGetter(schema: any, ajv: Ajv, lazy: boolean): () => ValidateFunction {
172+
if (lazy) {
173+
let validate: ValidateFunction | null = null;
174+
return function () {
175+
if (!validate) {
176+
validate = ajv.compile(schema);
177+
}
178+
return validate;
179+
};
180+
} else {
181+
const validate = ajv.compile(schema);
182+
return function () {
183+
return validate;
184+
};
185+
}
186+
}
187+
170188
function generateValidator(
171189
schemaContext: Oas3CompileContext,
172190
parameterLocation: ParameterLocation,
@@ -212,10 +230,14 @@ function generateValidator(
212230
ajv.addFormat(key, customFormats[key]);
213231
}
214232

215-
const validate = ajv.compile(schema);
233+
const getValidate = createValidateGetter(
234+
schema,
235+
ajv,
236+
schemaContext.options.lazyCompileValidationSchemas
237+
);
216238

217239
return function (json: any) {
218-
return doValidate(schemaPtr, parameterLocation, parameterRequired, validate, json);
240+
return doValidate(schemaPtr, parameterLocation, parameterRequired, getValidate, json);
219241
};
220242
}
221243

src/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface ExegesisCompiledOptions {
3737
allErrors: boolean;
3838
treatReturnedJsonAsPure: boolean;
3939
strictValidation: boolean;
40+
lazyCompileValidationSchemas: boolean;
4041
}
4142

4243
// See the OAS 3.0 specification for full details about supported formats:
@@ -134,5 +135,6 @@ export function compileOptions(options: ExegesisOptions = {}): ExegesisCompiledO
134135
allErrors: options.allErrors || false,
135136
treatReturnedJsonAsPure: options.treatReturnedJsonAsPure || false,
136137
strictValidation: options.strictValidation ?? false,
138+
lazyCompileValidationSchemas: options.lazyCompileValidationSchemas ?? false,
137139
};
138140
}

src/types/options.ts

+7
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,11 @@ export interface ExegesisOptions {
164164
* If true, then this will put ajv into "strict mode" (see https://ajv.js.org/strict-mode.html).
165165
*/
166166
strictValidation?: boolean;
167+
168+
/**
169+
* Response and request schemas are compiled by ajv to make validation faster. However compilation is slow
170+
* and can cause compilation of API to take long time. Enabling this will cause validation schemas
171+
* compilation to be executed when the validator is needed.
172+
*/
173+
lazyCompileValidationSchemas?: boolean;
167174
}

0 commit comments

Comments
 (0)