Skip to content

Commit

Permalink
Warn on use of sanitization-only middlewares
Browse files Browse the repository at this point in the history
Closes #781
  • Loading branch information
gustavohenke committed Jan 25, 2020
1 parent a1bb255 commit e53ee78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api-filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ title: Sanitization middlewares

These methods are all available via `require('express-validator')`.

> These sanitization-only middlewares have been deprecated, as the [validation ones](api-check.md)
offer the same functionality, and much more.
> They will be removed eventually.
## `sanitize(fields)`
- `field`: a string or an array of strings of field names to validate against.
> *Returns:* a [Sanitization Chain](api-sanitization-chain.md)
Expand Down
11 changes: 11 additions & 0 deletions src/middlewares/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import { InternalRequest, Location } from '../base';
import { bindAll } from '../utils';
import { ContextBuilder } from '../context-builder';

let hasNotified = false;

export function sanitize(
fields: string | string[] = '',
locations: Location[] = [],
): SanitizationChain {
if (!hasNotified) {
hasNotified = true;
console.warn(
'express-validator: sanitize(), sanitizeBody() and other sanitization-only middlewares ' +
'have been deprecated.\nPlease use check(), body() and others instead, which must offer ' +
'the same API, and more.',
);
}

const builder = new ContextBuilder()
.setFields(Array.isArray(fields) ? fields : [fields])
.setLocations(locations);
Expand Down

0 comments on commit e53ee78

Please sign in to comment.