I'm using this library for validation: https://validatejs.org/#validate-async.
I've got a fairly complicated way of validating nested objects against their own schema. See the relevant code below.
Basically, it will:
- Create an async promise for each nested object.
- Then call Promise.all to resolve or reject the validations.
It's working well, except one problem. With the default promise library Promise.all
"fails fast" so in the catch handler, which is in later express middleware, it only receives results from the first failed promise. But for my validators to work I need to combine the results from all the failed promises.
Is there an alternative promise library (A+ compliant) that I could swap into the validator that would allow me to capture all errors?
ValidationAdapter.prototype.validateCreateCtrl = function(req, res, next){
var obj = {};
var self = this;
this.logger.debug("Validating " + self.config.modelName + " create request");
self.fieldList.forEach(function(fieldName){
obj[fieldName] = req.body[fieldName];
});
self._resolveObjectRefs(obj);
this.logger.debug("Composed request object " + JSON.stringify(obj, null, 2));
var Promises = [];
Promises.push(validate.async(obj, self.constraints));
Object.keys(self.embeddedConstraints).forEach(function (key){
var value = obj[key];
var constraints = self.embeddedConstraints[key];
if (value.constructor === Array){
var i = 0;
value.forEach(function(newVal){
Promises.push(validate.async(newVal, constraints, {path: key + "[" + i + "]."}));
i++;
});
}else{
Promises.push(validate.async(value, constraints, {path: key}))
}
});
// by default it should fall through
Promise.all(Promises).then(function(){
return next();
}).catch(next);
};