Validating req.params with express validator

16,201

Solution 1

Params are parsed as string by express middleware. Say I make a req to path defined below like /some/1000

app.get('/some/:path', (req, res, next) => {
  console.log(typeof req.param.path)
  // outputs string
})

So you need to parse the incoming parameter to integer (Number) since you've stored accountNumber as integer. So adding toInt to chain like below should solve it:

const validateReq: [
  param('accountNumber').exists().toInt().custom(acctNo => accountNumberExist(acctNo)),
]

Solution 2

accountNumber inside accounts array is a number whereas req.params.accountNumber is a string. You need to convert the data type. You can do it as

    accountNumberExist(inputAcct) {
        const isfound = accounts.find(account => account.accountNumber.toString() === inputAcct);
        if (isfound === undefined) throw new Error('Account Number not found');
  }
Share:
16,201
Pelumi
Author by

Pelumi

Updated on June 08, 2022

Comments

  • Pelumi
    Pelumi almost 2 years

    I want the user to be able to write a specific account number in the endpoint, been trying to validate the endpoint param if it exists in my database. I couldn't get it to work, please what am I doing wrong?

    My validation

    const validateReq: [
    param('accountNumber').exists().custom(acctNo => accountNumberExist(acctNo)),]
    

    My accountNumberExist function

        accountNumberExist(inputAcct) {
        const isfound = accounts.find(account => account.accountNumber === inputAcct);
        if (isfound === undefined) throw new Error('Account Number not found');
      }
    

    My accounts file

        const accounts = [
      {
        id: 1,
        accountNumber: 1234567890,
        createdOn: new Date(),
        owner: 1,
        type: 'current',
        balance: 23444.43,
        status: 'active',
      },
      {
        id: 2,
        accountNumber: 1234167890,
        createdOn: new Date(),
        owner: 1,
        type: 'savings',
        balance: 2233444.43,
        status: 'active',
      },
      {
        id: 3,
        accountNumber: 9987654321,
        createdOn: new Date(),
        owner: 2,
        type: 'saving',
        balance: 73444.43,
        status: 'active',
      },
    ];
    

    But this is always throwing the 'Account Number not found' error even though, the req.param exists in my accounts database.

  • gustavohenke
    gustavohenke about 5 years
    The OP is using plain arrays. No async stuff involved.
  • gustavohenke
    gustavohenke about 5 years
    As the express-validator maintainer: this would be the preferred way of solving this ;)
  • dimitris tseggenes
    dimitris tseggenes about 5 years
    @gustavohenke where is the problem in this code? It is also recommended from the official docs express-validator.github.io/docs/…
  • gustavohenke
    gustavohenke about 5 years
    As said above, the OP is using plain arrays, the .find() mentioned above is Array.prototype.find -- not some kind of DB query or API call.
  • Akhil Raj
    Akhil Raj over 3 years
    what if i have value in param and in body? eg: resetting password -> we pass reset password token on param and email in body. How can we deal such situations?
  • Shahid Karimi
    Shahid Karimi over 3 years
    then wehere to use this sh*** validateReq variable?