使用validate.jsparsing所有asynchronous承诺

我正在使用这个库进行validation: https : //validatejs.org/#validate-async 。

我有一个相当复杂的方式来validation自己的模式嵌套的对象。 请参阅下面的相关代码。

基本上,它会:

  1. 为每个嵌套对象创build一个asynchronous承诺。
  2. 然后调用Promise.all来解决或拒绝validation。

除了一个问题,它运作良好。 在默认承诺库Promise.all “快速失败”,因此在后来的快速中间件的catch处理程序中,它只接收第一个失败的promise的结果。 但对于我的validation工作,我需要结合所有失败的承诺的结果。

是否有一个替代承诺库(A +兼容),我可以交换到validation器,这将允许我捕获所有错误?

 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); }; 

你可以使用蓝鸟的反映来落实settleAll

 Promise.all(Promises.map(function(promise) { return promise.reflect(); })).each(function(inspection) { if (inspection.isFulfilled()) { console.log("Success..."); } else { console.error("Reject..."); } }); 

http://bluebirdjs.com/docs/api/reflect.html#reflect