express-validator
express-validator copied to clipboard
Usage of exist, if in checkSchema
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?