'Express Validator - If exists for nested array of object

{
    "orderId": "3",
    "storeId": "1",
    "products": [
         "productId": "123",
        {
         "prescriptions" : [{
            "eye": "1",
            "sph": "1",
            "cyl": "1",
            "add": "1",
            "axis": "1"
        }],
            "lensItems": {
                "lensId": "1",
                "lensType": "1",
                "lensCoating": "1",
                "index": "1",
                "price": "3000"
            },
            "frameSize": {
                "a": "1",
                "b": "1",
                "ed": "1",
                "fh": "1",
                "d": "2"
            },
            "counterSale": false
        }
    ]
}

I want to check the prescriptions only if the lensItems exists.

I have tried this,

 body('products.*.prescriptions')
        .if(body('products.*.lensItems').exists())
        .exists()
        .withMessage('prescriptions not exists')
        .isArray()
        .withMessage('prescriptions is not an array')
        .notEmpty()
        .withMessage('prescriptions is empty'),

Its working when I check with string like below,

 body('products.*.prescriptions')
            .if(body('products.*.productId').exists())
            .exists()
            .withMessage('prescriptions not exists')
            .isArray()
            .withMessage('prescriptions is not an array')
            .notEmpty()
            .withMessage('prescriptions is empty'),

I want to the compare array of object with object . If any more regarding this please comment, I will share you much informations which are possible. Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source