Skip to content

Commit 52f0057

Browse files
committed
fix: Disable ajv "strict mode" by default, as it generates a lot of noise on most OpenAPI documents.
Add `strictValidation` option to turn it back on. fix #262
1 parent 3c54c8f commit 52f0057

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

docs/Options.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ document in "/src/index.js", you could specify `controllers` as:
4040
import * as path from 'path';
4141

4242
const options = {
43-
controllers: path.resolve(__dirname, 'controllers'),
44-
controllersPattern: "**/*.@(ts|js)"
45-
}
43+
controllers: path.resolve(__dirname, 'controllers'),
44+
controllersPattern: '**/*.@(ts|js)',
45+
};
4646
```
4747

4848
Then you can use [`x-exegesis-controller: Pets`](https://github.com/exegesis-js/exegesis/blob/master/docs/OAS3%20Specification%20Extensions.md)
@@ -67,11 +67,11 @@ for details.
6767

6868
## mimeTypeParsers
6969

70-
An objet where keys are either mime types or mime type wildcards (e.g. 'text/*'),
70+
An objet where keys are either mime types or mime type wildcards (e.g. 'text/\*'),
7171
and values are parsers.
7272

7373
This option is used to control how Exegesis parses message bodies and certain
74-
parameters. By default, parsers are provided for 'text/*' and
74+
parameters. By default, parsers are provided for 'text/\*' and
7575
'application/json'; however you can override either of these.
7676

7777
OpenAPI 3.x defines special handling for 'application/x-www-form-urlencoded',
@@ -223,6 +223,10 @@ a discussion about the difference between these.
223223

224224
This defaults to false, but in a future release it will default to true.
225225

226+
## strictValidation
227+
228+
If true, then this will put ajv into ["strict mode"](https://ajv.js.org/strict-mode.html).
229+
226230
## allErrors
227231

228232
If set, then when encountering a validation error Exegesis will return

src/oas3/Schema/validators.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Ajv, { ValidateFunction } from 'ajv';
22
import traveseSchema from 'json-schema-traverse';
3-
import { CustomFormats, IValidationError, ParameterLocation, ValidatorFunction } from '../../types';
3+
import { IValidationError, ParameterLocation, ValidatorFunction } from '../../types';
44
import { resolveRef } from '../../utils/json-schema-resolve-ref';
55
import * as jsonPaths from '../../utils/jsonPaths';
66
import * as jsonSchema from '../../utils/jsonSchema';
@@ -48,12 +48,6 @@ function getParameterDescription(parameterLocation: ParameterLocation) {
4848
return description;
4949
}
5050

51-
function addCustomFormats(ajv: Ajv, customFormats: CustomFormats) {
52-
for (const key of Object.keys(customFormats)) {
53-
ajv.addFormat(key, customFormats[key]);
54-
}
55-
}
56-
5751
function removeExamples(schema: any) {
5852
// ajv will print "schema id ignored" to stdout if an example contains a filed
5953
// named "id", so just axe all the examples.
@@ -211,9 +205,13 @@ function generateValidator(
211205
coerceTypes: allowTypeCoercion ? 'array' : false,
212206
removeAdditional: allowTypeCoercion ? 'failing' : false,
213207
allErrors: schemaContext.options.allErrors,
208+
strict: schemaContext.options.strictValidation,
214209
});
215210

216-
addCustomFormats(ajv, customFormats);
211+
for (const key of Object.keys(customFormats)) {
212+
ajv.addFormat(key, customFormats[key]);
213+
}
214+
217215
const validate = ajv.compile(schema);
218216

219217
return function (json: any) {

src/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface ExegesisCompiledOptions {
3636
validateDefaultResponses: boolean;
3737
allErrors: boolean;
3838
treatReturnedJsonAsPure: boolean;
39+
strictValidation: boolean;
3940
}
4041

4142
// See the OAS 3.0 specification for full details about supported formats:
@@ -132,5 +133,6 @@ export function compileOptions(options: ExegesisOptions = {}): ExegesisCompiledO
132133
validateDefaultResponses,
133134
allErrors: options.allErrors || false,
134135
treatReturnedJsonAsPure: options.treatReturnedJsonAsPure || false,
136+
strictValidation: options.strictValidation ?? false,
135137
};
136138
}

src/types/options.ts

+5
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,9 @@ export interface ExegesisOptions {
159159
* will call `context.res.json(val)`.
160160
*/
161161
treatReturnedJsonAsPure?: boolean;
162+
163+
/**
164+
* If true, then this will put ajv into "strict mode" (see https://ajv.js.org/strict-mode.html).
165+
*/
166+
strictValidation?: boolean;
162167
}

0 commit comments

Comments
 (0)