diff --git a/README.md b/README.md index 37d56b4c..28175c68 100644 --- a/README.md +++ b/README.md @@ -583,6 +583,23 @@ Determines whether the validator should validate requests. coerceTypes: true; } ``` + + **removeAdditional:** + + Determines whether to keep or remove additional properties in request body or to fail validation if schema has `additionalProperties` set to `false`. For futher details, refer to [AJV documentation](https://ajv.js.org/docs/validation.html#removing-additional-properties) + + - `false` (**default**) - not to remove additional properties + - `"all"` - all additional properties are removed, regardless of additionalProperties keyword in schema (and no validation is made for them). + - `true` - only additional properties with additionalProperties keyword equal to false are removed. + - `"failing"` - additional properties that fail request schema validation will be removed (where additionalProperties keyword is false or schema). + + For example: + + ```javascript + validateRequests: { + removeAdditional: true; + } + ``` ### ▪️ validateResponses (optional) diff --git a/src/framework/types.ts b/src/framework/types.ts index 9f8a2498..6d6f0b08 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -46,10 +46,11 @@ export interface RequestValidatorOptions extends Options, ValidateRequestOpts {} export type ValidateRequestOpts = { allowUnknownQueryParameters?: boolean; coerceTypes?: boolean | 'array'; + removeAdditional?: boolean | 'all' | 'failing'; }; export type ValidateResponseOpts = { - removeAdditional?: 'failing' | boolean; + removeAdditional?: boolean | 'all' | 'failing'; coerceTypes?: boolean | 'array'; onError?: (err: InternalServerError, json: any) => void; }; diff --git a/src/openapi.validator.ts b/src/openapi.validator.ts index 2f7080af..da136cf5 100644 --- a/src/openapi.validator.ts +++ b/src/openapi.validator.ts @@ -377,13 +377,14 @@ class AjvOptions { } get request(): RequestValidatorOptions { - const { allowUnknownQueryParameters, coerceTypes } = ( + const { allowUnknownQueryParameters, coerceTypes, removeAdditional } = ( this.options.validateRequests ); return { ...this.baseOptions(), allowUnknownQueryParameters, coerceTypes, + removeAdditional, }; }