'How can I compare integer elements of an array in express validator?

I have an express validator for a 2-length integer array that looks like this.

exports.createItem = 
    check("times").exists()
        .withMessage('MISSING').isArray({min: 2, max: 2})
        .withMessage('err'),
    check("times.*").not()
        .isString().isInt(),
    (req,res, next) =>
    {
        validationResult(req,res,next);
    }
];

I would like to check that the second integer of the array is bigger than the first one. How can I do that?



Solution 1:[1]

As dimitris tseggenes said you can use custom validator in order to have access to the array element, I can add my solution to go through the array, in this case of objects, and be able to verify one of its properties

body("array")
    .optional()
    .custom((value) => {
        value.forEach(el => {
            if (el.uid == 0) throw new Error('The el is required');
            return true;
        });
        return true;
    });

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P