Node Js Express Validator Check if field is numeric if not empty

I am using express-validator to validate message fields, one of the fields must be decimal or empty. I used this scheme:

request.checkBody({ 
        'product_price': {
            optional: true,
            isDecimal: {
                errorMessage: 'The product price must be a decimal'
            }
        }
})

      

The problem with this schema is not to check my post if the product_price is empty, even with "optional: true".

+3


source to share


2 answers


You need to set an option checkFalsy

for optional

to allow certain but empty values:



request.checkBody({ 
  'product_price': {
    optional: {
      options: { checkFalsy: true }
    },
    isDecimal: {
      errorMessage: 'The product price must be a decimal'
    }
  }
});

      

+4


source


Worked for me req.checkBody('optionalParam', 'optionalParam must be a number').optional().isNumber();



0


source







All Articles