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

Usage of exist, if in checkSchema

Open yerycs opened this issue 2 years ago • 0 comments

I am implementing conditional validation. Here is the payload.

{
  "address": {
    "address1": ""
  }
}

address field is optional. Sub field address1 should be required when address exists.

Here is my checkSchema validator.

checkSchema({
  "address": {
    isObject: {
      options: { strict: true },
      bail: true,
    },
    optional: true,
  },
  "address.address1": {
    exists: {
      if: body("address").notEmpty(),
    }
  }
})

This validator is working fine.

The problem is matchedData after pass validator when there isn't address in payload, address and address1 is generated.

So, I passed empty object {}. But the matchedData is following value.

{
  "address": {
    "address1": undefined
  }
}

The matchedData should be empty object also. How can I solve it with correct validation?

yerycs avatar Sep 12 '23 18:09 yerycs