Skip to content

Commit

Permalink
used regular for-loop again;
Browse files Browse the repository at this point in the history
  • Loading branch information
aprilmintacpineda committed Feb 28, 2018
1 parent 46a82e2 commit 6ac4d81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 65 deletions.
111 changes: 47 additions & 64 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6ac4d81

Please sign in to comment.