Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate an array of strings using express validator?

options = ['asdasda', 'asdasdas', 'asdasdasafsaafasfasfasfasfasfasasasasasdas', 'asd'];
req.check('options', 'Option must not exceed 30 characters').isLength({max: 30});

Am trying to validate each string in the array options. Is there any way to do it?

like image 563
Zoho Corporation Avatar asked Oct 11 '17 08:10

Zoho Corporation


People also ask

How do you validate an array in JavaScript?

In JavaScript, we can check if a variable is an array by using 3 methods, using the isArray method, using the instanceof operator and using checking the constructor type if it matches an Array object. The Array. isArray() method checks whether the passed variable is an Array object.

How does express validator work?

According to the official website, Express Validator is a set of Express. js middleware that wraps validator. js , a library that provides validator and sanitizer functions. Simply said, Express Validator is an Express middleware library that you can incorporate in your apps for server-side data validation.

What is checkFalsy in Express validator?

What is checkFalsy in Express-validator? You can customize this behavior by passing an object with the following options: nullable: if true, fields with null values will be considered optional. checkFalsy: if true, fields with falsy values (eg "", 0, false, null) will also be considered optional.10-Jul-2014.

What is validationResult Express validator?

validationResult(req)Extracts the validation errors from a request and makes them available in a Result object. Each error returned by . array() and . mapped() methods has the following format by default: { "msg": "The error message", "param": "param.


1 Answers

Yes, you must use wildcards for that.

req.check('options.*').isLength({ max: 30 })
like image 82
gustavohenke Avatar answered Sep 29 '22 04:09

gustavohenke