Skip to content

oneOf() creates empty string when trim() is used #875

@pawelfus

Description

@pawelfus

Example:

router.post('/endpoint', [
  oneOf([
    body('reference').isString().trim().escape(),
    body('otherId').trim().escape().toInt(),
  ])
], (req, res) => {
  console.log(req.body);
});

Tested with body:

{
  "otherId": 2
}

Expected behaviour:

req.body should keep it's format:

{
  otherId: 2
}

Current behaviour:

req.body is modified:

{
  otherId: 2,
  reference: ''
}

Version

express-validator v6.4.1

Related/similar issues:

#729

Let me know if I have missed something. For now I use custom sanitizer:

const emptyString2Undefined = (value: string): string|undefined => value === '' ? undefined : value;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions