From 098d4440779b241df2b19c30e014a883283ffe5d Mon Sep 17 00:00:00 2001 From: "Yuya.Nishida" Date: Thu, 8 Dec 2016 19:06:44 +0900 Subject: [PATCH] Add array support * New validator: forEach and forEachSingle * Add validator specification about return value. --- validate.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/validate.js b/validate.js index 9d470c4..ae4a5c4 100644 --- a/validate.js +++ b/validate.js @@ -611,6 +611,11 @@ expandMultipleErrors: function(errors) { var ret = []; errors.forEach(function(error) { + if (v.isFunction(error.error.expand)) { + error.error.expand(ret, error); + return; + } + // Removes errors without a message if (v.isArray(error.error)) { error.error.forEach(function(msg) { @@ -1144,6 +1149,68 @@ if (!PATTERN.exec(value)) { return message; } + }, + + forEachSingle: function(value, options, attribute, attributes) { + if (!v.isDefined(value)) { + return; + } + + var result = { + expand: function(ret, res) { + res.error.data.forEach(function(ary, i) { + if (ary) { + ary.forEach(function(r) { + var attribute = res.attribute + '[' + i + ']'; + var o = v.extend({}, r, { + attribute: attribute, + attributes: {} + }); + o.attributes[attribute] = r.attributes.single; + ret.push(o); + }); + } + }); + }, + data: [] + }; + for (var i = 0; i < value.length; i++) { + var r = v({single: value[i]}, {single: options}, + {fullMessages: false, format: 'detailed'}); + result.data.push(r); + } + return result; + }, + + forEach: function(value, options, attribute, attributes) { + if (!v.isDefined(value)) { + return; + } + + var result = { + expand: function(ret, res) { + res.error.data.forEach(function(ary, i) { + if (ary) { + ary.forEach(function(r) { + var attribute = res.attribute + '[' + i + '].' + r.attribute; + var o = v.extend({}, r, { + attribute: attribute, + attributes: {} + }); + o.attributes[attribute] = r.attributes; + ret.push(o); + }); + } + }); + }, + data: [] + }; + for (var i = 0; i < value.length; i++) { + var r = v(value[i], options, + {fullMessages: false, format: 'detailed'}); + result.data.push(r); + } + return result; } };