Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.default() does not work with Number 0 as value when used with checkSchema #1086

Closed
ghost opened this issue Sep 9, 2021 · 0 comments · Fixed by #1092
Closed

.default() does not work with Number 0 as value when used with checkSchema #1086

ghost opened this issue Sep 9, 2021 · 0 comments · Fixed by #1092

Comments

@ghost
Copy link

ghost commented Sep 9, 2021

Describe the bug

The default sanitizer should set default value to missing or null values.

Although, it doesn't work when we use it inside a schema and we want to set value to the Number 0.

To Reproduce

Simple script to reproduce:

const express = require('express');
const { body, checkSchema } = require('express-validator');

const app = express();

app.use(express.json());

app.post(
  "/schema/number",
  checkSchema({
    test: {
      in: ['body'],
      default: {options: 0}
    }
  }),
  (req, res, next) => {
    console.log(`/schema/number output: ${JSON.stringify(req.body, null, 4)}`);
    next();
  }
);

app.post(
  "/schema/string",
  checkSchema({
    test: {
      in: ['body'],
      default: {options: "0"}
    }
  }),
  (req, res, next) => {
    console.log(`/schema/string output: ${JSON.stringify(req.body, null, 4)}`);
    next();
  }
);

app.post(
  "/chain/number",
  body('test').default(0),
  (req, res, next) => {
    console.log(`/chain/number output: ${JSON.stringify(req.body, null, 4)}`);
    next();
  }
);

app.post(
  "/chain/string",
  body('test').default('0'),
  (req, res, next) => {
    console.log(`/chain/string output: ${JSON.stringify(req.body, null, 4)}`);
    next();
  }
);

app.listen(3000);

With express-validator 6.12.1 the output is:

/chain/number output: {
    "test": 0
}
/chain/string output: {
    "test": "0"
}
/schema/number output: {}
/schema/string output: {
    "test": "0"
}

Expected behavior

With same program, the expected behavior is to get 0 with the /schema/number route, as the /chain/number does:

/chain/number output: {
    "test": 0
}
/chain/string output: {
    "test": "0"
}
/schema/number output: {
    "test": 0
}
/schema/string output: {
    "test": "0"
}

Current behavior

See above.

Environment:

  • Express-validator version: 6.12.1
  • Express version: 4.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant