Express Validator Check Nested Fields If Object Exists

4.4k Views Asked by At

I'm currently trying to create Rest API Project with Express, NodeJs and using Express-Validator to validate request objects. In one service, I have a request body like :

{
  "name": "some value",
  "surname": "some value",
  "company": {
     "name": "some value",
     "address": "some value"
     ...
  }
}

and trying to validate company and its' subfields if company exists.

const checkCompany = () => {
return check('company')
    .optional()
    .custom((company) => {
        if (!isEmptyObject(company)) {
            [
                check('company.name')
                    .notEmpty().withMessage(CompanyMessages.Name.empty)
                    .isLength({ min: CompanyConstants.Name.MinLength, max: CompanyConstants.Name.MaxLength }).withMessage(CompanyMessages.Name.length),

                check('company.description')
                    .notEmpty().withMessage(CompanyMessages.Description.empty)
                    .isLength({ min: CompanyConstants.Description.MinLength, max: CompanyConstants.Description.MaxLength }).withMessage(CompanyMessages.Description.length),

                check('company.country')
                    .notEmpty().withMessage(CompanyMessages.Country.empty),

                check('company.city')
                    .notEmpty().withMessage(CompanyMessages.City.empty),

                check('company.address')
                    .notEmpty().withMessage(CompanyMessages.Address.empty)
                    .isLength({ min: CompanyConstants.Address.MinLength, max: CompanyConstants.Address.MaxLength }).withMessage(CompanyMessages.Address.length),
            ]
        }
    })}

What I want is:

  • If company field does not exist, It's okey
  • If company field exists, validate all subfields

I can use validation methods for all other fields and routes, however couldn't validate fields in this case. I'm stuck with this case, appreciated for any help, what is it wrong about my code?

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

I had the same problem and I found this solution :

https://stackoverflow.com/a/64323413/10661841

Use :

 check('company').optional(),
 check('company.name')
        .if(body('company').exists()) // check subfield only if 'company' exist
        .notEmpty().withMessage(CompanyMessages.Name.empty)
        .isLength({ min: CompanyConstants.Name.MinLength, max: CompanyConstants.Name.MaxLength })
        .withMessage(CompanyMessages.Name.length),
 check('company.description')
        .if(body('company').exists()) // check subfield only if 'company' exist
        .notEmpty().withMessage(CompanyMessages.Description.empty)
        .isLength({ min: CompanyConstants.Description.MinLength, max: CompanyConstants.Description.MaxLength })
        .withMessage(CompanyMessages.Description.length),
check('company.country')
        .if(body('company').exists()) // check subfield only if 'company' exist
        .notEmpty().withMessage(CompanyMessages.Country.empty),
check('company.city')
        .if(body('company').exists()) // check subfield only if 'company' exist
        .notEmpty().withMessage(CompanyMessages.City.empty),
check('company.address')
        .if(body('company').exists()) // check subfield only if 'company' exist
        .notEmpty().withMessage(CompanyMessages.Address.empty)
        .isLength({ min: CompanyConstants.Address.MinLength, max: CompanyConstants.Address.MaxLength })
        .withMessage(CompanyMessages.Address.length),

It works for me.