From 6ac4d8130551e862e23be62d156441526e02397f Mon Sep 17 00:00:00 2001 From: April Mintac Pineda Date: Wed, 28 Feb 2018 15:34:29 +0800 Subject: [PATCH] used regular for-loop again; --- build/index.js | 111 +++++++++++++++++++++---------------------------- src/index.js | 5 ++- 2 files changed, 51 insertions(+), 65 deletions(-) diff --git a/build/index.js b/build/index.js index 35bc5d3..6c684dd 100644 --- a/build/index.js +++ b/build/index.js @@ -34,74 +34,57 @@ function validator(inputs, rules) { Object.keys(newRules).forEach(function (field) { var targetField = field.replace(/\_$/, ''); - - var _iteratorNormalCompletion = true; - var _didIteratorError = false; - var _iteratorError = undefined; - - try { - for (var _iterator = rules[targetField].split('|')[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var segment = _step.value; - - // rule:value - var _segment$split = segment.split(':'), - _segment$split2 = _slicedToArray(_segment$split, 2), - rule = _segment$split2[0], - val = _segment$split2[1]; - - var validationResult = null; - var cb_params = []; - - if (typeof _rules2.default[rule] == 'undefined') throw new Error('input-validator-js: unknown rule `' + rule + '` provided for field `' + field + '`. Please refer to the docs for more info.'); - if (typeof inputs[targetField] == 'undefined') throw new Error('input-validator-js: unknown field ' + field + ' in inputs.'); - - // :value - if (val) { - if (val.includes(',')) { - // :value1,value2,value3 - var vals = val.split(','); - validationResult = _rules2.default[rule].apply(_rules2.default, [inputs[targetField]].concat(_toConsumableArray(vals))); - cb_params.push.apply(cb_params, [field].concat(_toConsumableArray(vals))); - } else if (inputs[val]) { - // :another_field - // where :another_field is in inputs[another_field] - validationResult = _rules2.default[rule](inputs[targetField], inputs[val]); - cb_params.push(field, inputs[val]); - } else { - // :value1 - validationResult = _rules2.default[rule](inputs[targetField], val); - cb_params.push(field, val); - } + var segments = rules[targetField].split('|'); + + for (var a = 0; a <= segments.length - 1; a++) { + var segment = segments[a]; + + // rule:value + + var _segment$split = segment.split(':'), + _segment$split2 = _slicedToArray(_segment$split, 2), + rule = _segment$split2[0], + val = _segment$split2[1]; + + var validationResult = null; + var cb_params = []; + + if (typeof _rules2.default[rule] == 'undefined') throw new Error('input-validator-js: unknown rule `' + rule + '` provided for field `' + field + '`. Please refer to the docs for more info.'); + if (typeof inputs[targetField] == 'undefined') throw new Error('input-validator-js: unknown field ' + field + ' in inputs.'); + + // :value + if (val) { + if (val.includes(',')) { + // :value1,value2,value3 + var vals = val.split(','); + validationResult = _rules2.default[rule].apply(_rules2.default, [inputs[targetField]].concat(_toConsumableArray(vals))); + cb_params.push.apply(cb_params, [field].concat(_toConsumableArray(vals))); + } else if (inputs[val]) { + // :another_field + // where :another_field is in inputs[another_field] + validationResult = _rules2.default[rule](inputs[targetField], inputs[val]); + cb_params.push(field, inputs[val]); } else { - // no :value was defined - validationResult = _rules2.default[rule](inputs[targetField]); - cb_params.push(field); - } - - if (validationResult == -1) { - if (customMesssages && customMesssages[targetField] && !customMesssages[targetField][rule] && customMesssages[targetField]._$all) { - errors.add(customMesssages[targetField]._$all); - } else if (customMesssages && customMesssages[targetField] && customMesssages[targetField][rule]) { - errors.add(customMesssages[targetField][rule]); - } else { - errors.add(_defaults.errMessages[rule].apply(_defaults.errMessages, cb_params)); - } - - if (options && options.stopAtFirstError) break; + // :value1 + validationResult = _rules2.default[rule](inputs[targetField], val); + cb_params.push(field, val); } + } else { + // no :value was defined + validationResult = _rules2.default[rule](inputs[targetField]); + cb_params.push(field); } - } catch (err) { - _didIteratorError = true; - _iteratorError = err; - } finally { - try { - if (!_iteratorNormalCompletion && _iterator.return) { - _iterator.return(); - } - } finally { - if (_didIteratorError) { - throw _iteratorError; + + if (validationResult == -1) { + if (customMesssages && customMesssages[targetField] && !customMesssages[targetField][rule] && customMesssages[targetField]._$all) { + errors.add(customMesssages[targetField]._$all); + } else if (customMesssages && customMesssages[targetField] && customMesssages[targetField][rule]) { + errors.add(customMesssages[targetField][rule]); + } else { + errors.add(_defaults.errMessages[rule].apply(_defaults.errMessages, cb_params)); } + + if (options && options.stopAtFirstError) break; } } }); diff --git a/src/index.js b/src/index.js index 8eedf31..3b959ca 100644 --- a/src/index.js +++ b/src/index.js @@ -15,8 +15,11 @@ export default function validator(inputs, rules, customMesssages = null) { Object.keys(newRules).forEach(field => { const targetField = field.replace(/\_$/, ''); + const segments = rules[targetField].split('|'); + + for (let a = 0; a <= segments.length - 1; a++) { + const segment = segments[a]; - for (let segment of rules[targetField].split('|')) { // rule:value const [rule, val] = segment.split(':'); let validationResult = null;