Skip to content

Commit

Permalink
v2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
garygreen committed Feb 15, 2016
1 parent aa07c9d commit 84e6aee
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "validatorjs",
"version": "2.0.4",
"version": "2.0.5",
"homepage": "https://github.com/skaterdav85/validatorjs",
"authors": [
"David Tang"
Expand Down
90 changes: 77 additions & 13 deletions dist/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! validatorjs - v2.0.1 - - 2015-10-29 */
/*! validatorjs - v2.0.5 - - 2016-02-15 */
(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 @@ -430,14 +430,7 @@ Messages.prototype = {
}

if (typeof template === 'object') {
switch (typeof rule.inputValue) {
case 'number':
template = template['numeric'];
break;
case 'string':
template = template['string'];
break;
}
template = template[rule._getValueType()];
}

return template;
Expand Down Expand Up @@ -765,6 +758,21 @@ Rule.prototype = {
return value.length;
},

/**
* Get the type of value being checked; numeric or string.
*
* @return {string}
*/
_getValueType: function() {

if (typeof this.inputValue === 'number' || this.validator._hasNumericRule(this.attribute))
{
return 'numeric';
}

return 'string';
},

/**
* Set the async callback response
*
Expand Down Expand Up @@ -878,8 +886,8 @@ module.exports = manager;
var Rules = require('./rules');
var Lang = require('./lang');
var Errors = require('./errors');
var AsyncResolvers = require('./async');
var Attributes = require('./attributes');
var AsyncResolvers = require('./async');

var Validator = function(input, rules, customMessages) {
var lang = Validator.getDefaultLang();
Expand Down Expand Up @@ -933,23 +941,35 @@ Validator.prototype = {
var attributeRules = this.rules[attribute];
var inputValue = this.input[attribute]; // if it doesnt exist in input, it will be undefined

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

if (!this._isValidatable(rule, inputValue)) {
continue;
}

if (!rule.validate(inputValue, ruleOptions.value, attribute)) {
rulePassed = rule.validate(inputValue, ruleOptions.value, attribute);
if (!rulePassed) {
this._addFailure(rule);
}

if (this._shouldStopValidating(attribute, rulePassed)) {
break;
}
}
}

return this.errorCount === 0;
},

/**
* Run async validator
*
* @param {function} passes
* @param {function} fails
* @return {void}
*/
/**
* Run async validator
*
Expand All @@ -959,6 +979,8 @@ Validator.prototype = {
*/
checkAsync: function(passes, fails) {
var _this = this;
passes = passes || function() {};
fails = fails || function() {};

var failsOne = function(rule, message) {
_this._addFailure(rule, message);
Expand Down Expand Up @@ -1105,6 +1127,28 @@ Validator.prototype = {
return this.getRule('required').validate(value);
},


/**
* Determine if we should stop validating.
*
* @param {string} attribute
* @param {boolean} rulePassed
* @return {boolean}
*/
_shouldStopValidating: function(attribute, rulePassed) {

var stopOnAttributes = this.stopOnAttributes;
if (stopOnAttributes === false || rulePassed === true) {
return false;
}

if (stopOnAttributes instanceof Array) {
return stopOnAttributes.indexOf(attribute) > -1;
}

return true;
},

/**
* Set custom attribute names.
*
Expand Down Expand Up @@ -1135,6 +1179,16 @@ Validator.prototype = {
return Rules.make(name, this);
},

/**
* Stop on first error.
*
* @param {boolean|array} An array of attributes or boolean true/false for all attributes.
* @return {void}
*/
stopOnError: function(attributes) {
this.stopOnAttributes = attributes;
},

/**
* Determine if validation passes
*
Expand All @@ -1158,7 +1212,7 @@ Validator.prototype = {
fails: function(fails) {
var async = this._checkAsync('fails', fails);
if (async) {
return this.checkAsync(undefined, fails);
return this.checkAsync(function() {}, fails);
}
return !this.check();
},
Expand Down Expand Up @@ -1232,6 +1286,16 @@ Validator.setAttributeFormatter = function(func) {
this.prototype.attributeFormatter = func;
};

/**
* Stop on first error.
*
* @param {boolean|array} An array of attributes or boolean true/false for all attributes.
* @return {void}
*/
Validator.stopOnError = function(attributes) {
this.prototype.stopOnAttributes = attributes;
};

/**
* Register custom validation rule
*
Expand Down
Loading

0 comments on commit 84e6aee

Please sign in to comment.