Skip to content

Commit

Permalink
add option removeAdditional to validateRequest options (#501)
Browse files Browse the repository at this point in the history
* add option removeAdditional to validateRequestOpts

* fix type def

* fix removeAdditional type in ValidateResponseOpts

Co-authored-by: Dmitry Ivanov <di@studio-tg.ru>
  • Loading branch information
rockmagic and Dmitry Ivanov committed Feb 14, 2021
1 parent 2866ce6 commit acada10
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/framework/types.ts
Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion src/openapi.validator.ts
Expand Up @@ -377,13 +377,14 @@ class AjvOptions {
}

get request(): RequestValidatorOptions {
const { allowUnknownQueryParameters, coerceTypes } = <ValidateRequestOpts>(
const { allowUnknownQueryParameters, coerceTypes, removeAdditional } = <ValidateRequestOpts>(
this.options.validateRequests
);
return {
...this.baseOptions(),
allowUnknownQueryParameters,
coerceTypes,
removeAdditional,
};
}

Expand Down

0 comments on commit acada10

Please sign in to comment.