Skip to content

Commit

Permalink
Merge pull request mikeerickson#129 from richriscunha/sometimes-feature
Browse files Browse the repository at this point in the history
add sometimes feature including tests
  • Loading branch information
garygreen authored Sep 19, 2016
2 parents 036a3ac + eb417ab commit 61cee9a
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 4 deletions.
42 changes: 40 additions & 2 deletions dist/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! validatorjs - v3.6.0 - - 2016-08-28 */
/*! validatorjs - v3.6.0 - - 2016-09-14 */
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Validator = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
function AsyncResolvers(onFailedOne, onResolvedAll) {
this.onResolvedAll = onResolvedAll;
Expand Down Expand Up @@ -526,6 +526,10 @@ var rules = {
return typeof val === 'string';
},

sometimes: function(val) {
return true;
},

/**
* Compares the size of strings or the value of numbers if there is a truthy value
*/
Expand Down Expand Up @@ -970,6 +974,10 @@ Validator.prototype = {
var attributeRules = this.rules[attribute];
var inputValue = this._objectPath(this.input, attribute);

if (this._passesOptionalCheck(attributeRules) && !(this._suppliedWithData(attribute))) {
continue;
}

for (var i = 0, len = attributeRules.length, rule, ruleOptions, rulePassed; i < len; i++) {
ruleOptions = attributeRules[i];
rule = this.getRule(ruleOptions.name);
Expand Down Expand Up @@ -1031,6 +1039,10 @@ Validator.prototype = {
var attributeRules = this.rules[attribute];
var inputValue = this._objectPath(this.input, attribute);

if (this._passesOptionalCheck(attributeRules) && !(this._suppliedWithData(attribute))) {
continue;
}

for (var i = 0, len = attributeRules.length, rule, ruleOptions; i < len; i++) {
ruleOptions = attributeRules[i];

Expand Down Expand Up @@ -1101,7 +1113,7 @@ Validator.prototype = {
if (Object.prototype.hasOwnProperty.call(obj, path)) {
return obj[path];
}

var keys = path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".");
var copy = {};

Expand Down Expand Up @@ -1151,6 +1163,32 @@ Validator.prototype = {
return parsedRules;
},

/**
* Determines if the input value being validated is optional or not.
*
* @param {array} attributeRules
* @return {boolean}
*/
_passesOptionalCheck: function(attributeRules) {
for(var i = 0; i < attributeRules.length; i++) {
if (attributeRules[i].name === 'sometimes') {
return true;
}
}

return false;
},

/**
* Determines if the attribute is supplied with the original data object.
*
* @param {array} attribute
* @return {boolean}
*/
_suppliedWithData: function(attribute) {
return this.input.hasOwnProperty(attribute);
},

/**
* Extract a rule and a value from a ruleString (i.e. min:3), rule = min, value = 3
*
Expand Down
2 changes: 1 addition & 1 deletion dist/validator.min.js

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions spec/sometimes-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
if (typeof require !== 'undefined') {
var Validator = require('../src/validator.js');
var expect = require('chai').expect;
} else {
var Validator = window.Validator;
var expect = window.chai.expect;
}

describe('sometimes validation pass rules', function() {
it('should pass when the property is passed with data', function() {
var validator = new Validator({
firstname: 'Johnny',
lastname: 'Appleseed'
}, {
firstname: 'required',
lastname: 'required|sometimes'
});
expect(validator.passes()).to.be.true;
});

it('should pass when the property is not passed with data', function() {
var validator = new Validator({
firstname: 'Johnny'
}, {
firstname: 'required',
lastname: 'required|sometimes'
});
expect(validator.passes()).to.be.true;
});

it('should be able to register and pass async rule when the property is passed with data', function(done) {
Validator.registerAsync('username', function(desiredUsername, ruleValue, attribute, passes) {
setTimeout(function() {
if (desiredUsername == 'test') {
passes();
}
}, 50);
}, ':attribute is an invalid username');

var validator = new Validator({
username: 'test',
email: '[email protected]'
}, {
username: 'username',
email: 'email|sometimes'
});
validator.passes(done);
});

it('should be able to register and pass async rule when the property is not passed with data', function(done) {
Validator.registerAsync('username', function(desiredUsername, ruleValue, attribute, passes) {
setTimeout(function() {
if (desiredUsername == 'test') {
passes();
}
}, 50);
}, ':attribute is an invalid username');

var validator = new Validator({
username: 'test'
}, {
username: 'username',
email: 'email|sometimes'
});
validator.passes(done);
});
});
4 changes: 4 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ var rules = {
return typeof val === 'string';
},

sometimes: function(val) {
return true;
},

/**
* Compares the size of strings or the value of numbers if there is a truthy value
*/
Expand Down
36 changes: 35 additions & 1 deletion src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Validator.prototype = {
var attributeRules = this.rules[attribute];
var inputValue = this._objectPath(this.input, attribute);

if (this._passesOptionalCheck(attributeRules) && !(this._suppliedWithData(attribute))) {
continue;
}

for (var i = 0, len = attributeRules.length, rule, ruleOptions, rulePassed; i < len; i++) {
ruleOptions = attributeRules[i];
rule = this.getRule(ruleOptions.name);
Expand Down Expand Up @@ -117,6 +121,10 @@ Validator.prototype = {
var attributeRules = this.rules[attribute];
var inputValue = this._objectPath(this.input, attribute);

if (this._passesOptionalCheck(attributeRules) && !(this._suppliedWithData(attribute))) {
continue;
}

for (var i = 0, len = attributeRules.length, rule, ruleOptions; i < len; i++) {
ruleOptions = attributeRules[i];

Expand Down Expand Up @@ -187,7 +195,7 @@ Validator.prototype = {
if (Object.prototype.hasOwnProperty.call(obj, path)) {
return obj[path];
}

var keys = path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".");
var copy = {};

Expand Down Expand Up @@ -237,6 +245,32 @@ Validator.prototype = {
return parsedRules;
},

/**
* Determines if the input value being validated is optional or not.
*
* @param {array} attributeRules
* @return {boolean}
*/
_passesOptionalCheck: function(attributeRules) {
for(var i = 0; i < attributeRules.length; i++) {
if (attributeRules[i].name === 'sometimes') {
return true;
}
}

return false;
},

/**
* Determines if the attribute is supplied with the original data object.
*
* @param {array} attribute
* @return {boolean}
*/
_suppliedWithData: function(attribute) {
return this.input.hasOwnProperty(attribute);
},

/**
* Extract a rule and a value from a ruleString (i.e. min:3), rule = min, value = 3
*
Expand Down

0 comments on commit 61cee9a

Please sign in to comment.