express-validator icon indicating copy to clipboard operation
express-validator copied to clipboard

How to get failed error type

Open jensysantana opened this issue 4 years ago • 4 comments

Hi i'm trying to get the error type failed to validate:

This is what o have when validation fail but i want to know what kind of fail was. like: required or maxlength etc. { value: undefined, msg: 'Invalid value', param: 'lname', location: 'body' }

I need something like this:

{ value: undefined, msg: 'Invalid value', param: 'lname', location: 'body', king: 'required' }

jensysantana avatar May 03 '21 14:05 jensysantana

It is impossible for us to detect which option threw the error because validatorjs doesn't give us any kind of information about that e.g. isLength({ min: 10 }). However we can add the generic validator that was unsuccessful e.g. isLength.

If anyone would like to work on this, please raise your hand!

fedeci avatar May 04 '21 05:05 fedeci

ok. thank you. I fixed I think I use a try catch block.

jensysantana avatar Jun 22 '21 04:06 jensysantana

You added trycatch around check().isLength...?

fedeci avatar Jun 22 '21 05:06 fedeci

As a suggestion, you can use withMessage to send error codes back to your frontend application, e.g. check('name').isAlpha().withMessage('IS_ALPHA'). It may be a bit cumbersome, but allows for greater flexibility, in my opinion.

gustavohenke avatar Jun 22 '21 22:06 gustavohenke

Closing as it's been ~2 years since the last activity

Please open a new issue if needed.

gustavohenke avatar Apr 15 '23 07:04 gustavohenke