Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

421
Views
How to validate arrays/objects in express-validator using body(), checking only the specified fields?

I have my field names in an array like these:

const baseFields = [
'employeeNumber',
'firstName',
'lastName',
'trEmail',
'position'
];

Those are the input fields I only have to care about being validated.

In the request body, I receive an array of objects. Example:

   {employeeNumber: 12343,
    firstName: Will,
    lastName: Smith,
    trEmail: smith@will.com,
    position: Actor,
    salary: low
    },
    
    {employeeNumber: 12344,
    firstName: Chris,
    lastName: Rock,
    trEmail: rock@chris.com,
    position: stuntman,
    salary: ''
    }

I want to validate this array with only the concern fields in the baseFields array.

This is my current validator code. I've found out that I can use wildcards in order to validate arrays.

const existsOptions = {
    checkNull: true,
    checkFalsy: true
};

const postRequiredFields = () => {

    const validators = [];

    const validator = body('*.*')
    .exists(existsOptions)
    .bail()
    .isString();
    validators.push(validator);
    
    return validators;
};

Using this const validator = body('*.*') will check all of the fields in the array of objects in the body. Since I can get this message:

{ value: '',
  msg: 'Invalid value',
  param: '[1].salary',
  location: 'body' }

You see, salary field is being checked. It returned invalid value because the second index in the array has the salary set to '' or empty. But again, the salary field is not one of the fields that I need to validate.

So I tried something like this body('baseFields*.*') to check the whole array of objects but only the concern fields but it won't work. I couldn't find the right wildcard pattern for my scenario online. The documentation also says very little.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

to check an object in an array, use: *.key

and then you can just loop your keys and add them dynamically:

const postRequiredFields = () => {

    const validators = [];

    baseFields.map((key) => {
        const validator = body(`*.${key}`)
            .exists(existsOptions)
            .bail()
            .isString();
        validators.push(validator);
    });

    return validators;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!