forked from guillaumepotier/validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.min.js
25 lines (25 loc) · 12.6 KB
/
validator.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*! Validator.js validator.min.js build version 0.3.1 http://validatorjs.org */
(function(l){function j(a){return"[object Array]"===Object.prototype.toString.call(a)}var f=function(a){this.__version__="0.3.1";this.__class__="Validator";this.options=a||{};this.bindingKey=this.options.bindingKey||"_validatorjsConstraint";return this};f.prototype={constructor:f,validate:function(a,b,c){return"string"===typeof a?this._validateString(a,b,c):"object"===typeof a&&this.isBinded(a)?this._validateBindedObject(a,b):this._validateObject(a,b,c)},version:function(){return this.__version__},
bind:function(a,b){if("object"!==typeof a)throw Error("Must bind a Constraint to an object");a[this.bindingKey]=new g(b);return this},unbind:function(a){if("undefined"===typeof a._validatorjsConstraint)return this;delete a[this.bindingKey];return this},isBinded:function(a){return"undefined"!==typeof a[this.bindingKey]},getBinded:function(a){return this.isBinded(a)?a[this.bindingKey]:null},_validateString:function(a,b,c){var e,k=[];j(b)||(b=[b]);for(var f=0;f<b.length;f++){if(!(b[f]instanceof h))throw Error("You must give an Assert or an Asserts array to validate a string");
e=b[f].check(a,c);e instanceof d&&k.push(e)}return k},_validateObject:function(a,b,c){return(new g(b)).check(a,c)},_validateBindedObject:function(a,b){return a[this.bindingKey].check(a,b)}};f.const={must_be_a_string:"must_be_a_string",must_be_an_array:"must_be_an_array",must_be_a_number:"must_be_a_number"};var g=function(a){this.__class__="Constraint";this.nodes={};if(a)try{this._bootstrap(a)}catch(b){throw Error("Should give a valid mapping object to Constraint",b,a);}return this};g.prototype={constructor:g,
check:function(a,b){var c,e={},d;for(d in a)if(this.has(d)&&(c=this._check(d,a[d],b),j(c)&&0<c.length||!j(c)&&!_isEmptyObject(c)))e[d]=c;return e},add:function(a,b){if(b instanceof h||j(b)&&b[0]instanceof h)return this.nodes[a]=b,this;if("object"===typeof b&&!j(b))return this.nodes[a]=b instanceof g?b:new g(b),this;throw Error("Should give an Assert, an Asserts array, a Constraint",b);},has:function(a){return"undefined"!==typeof this.nodes[a.toLowerCase()]},get:function(a,b){return"undefined"!==typeof this.nodes[a]?
this.nodes[a]:b||null},remove:function(a){var b=[],c;for(c in this.nodes)c!==a&&(b[c]=this.nodes[c]);this.nodes=b;return this},_bootstrap:function(a){if(a instanceof g)return this.nodes=a.nodes;for(var b in a)this.add(b,a[b])},_check:function(a,b,c){if(this.nodes[a]instanceof h)return this._checkAsserts(b,[this.nodes[a]],c);if(j(this.nodes[a]))return this._checkAsserts(b,this.nodes[a],c);if(this.nodes[a]instanceof g)return this.nodes[a].check(b,c);throw Error("Invalid node",this.nodes[a]);},_checkAsserts:function(a,
b,c){for(var e,d=[],f=0;f<b.length;f++)e=b[f].check(a,c),"undefined"!==typeof e&&!0!==e&&d.push(e);return d}};var d=function(a,b,c){this.__class__="Violation";if(!(a instanceof h))throw Error("Should give an assertion implementing the Assert interface");this.assert=a.__class__;this.value=b;"undefined"!==typeof c&&(this.violation=c)};d.prototype={show:function(){var a={assert:this.assert,value:this.value};this.violation&&(a.violation=this.violation);return a},__toString:function(){if("undefined"!==
typeof this.violation)var a='", '+this.getViolation().constraint+" expected was "+this.getViolation().expected;return this.assert+' assert failed for "'+this.value+a||""},getViolation:function(){var a,b;for(a in this.violation)b=this.violation[a];return{constraint:a,expected:b}}};var h=function(a){this.__parentClass__=this.__class__="Assert";this.groups=[];"undefined"!==typeof a&&this.addGroup(a);return this};h.prototype={construct:h,check:function(a,b){if(!b||this.hasGroup(b))if(b||!this.hasGroups())try{return this.validate(a,
b)}catch(c){return c}},hasGroup:function(a){return"string"!==typeof a?this.hasOneOf(a):!this.hasGroups()?"Default"===a:-1!==this.groups.indexOf(a)},hasOneOf:function(a){for(var b=0;b<a.length;b++)if(this.hasGroup(a[b]))return!0;return!1},hasGroups:function(){return 0<this.groups.length},addGroup:function(a){if(j(a))return this.addGroups(a);this.hasGroup(a)||this.groups.push(a);return this},removeGroup:function(a){for(var b=[],c=0;c<this.groups.length;c++)a!==this.groups[c]&&b.push(this.groups[c]);
this.groups=b;return this},addGroups:function(a){for(var b=0;b<a.length;b++)this.addGroup(a[b]);return this},Blank:function(){this.__class__="Blank";this.validate=function(a){if("string"!==typeof a)throw new d(this,a,{value:f.const.must_be_a_string});if(""!==a.replace(/^\s+/g,"").replace(/\s+$/g,""))throw new d(this,a);return!0};return this},Callback:function(a){this.__class__="Callback";if("function"!==typeof a)throw Error("Callback must be instanciated with a function");this.fn=a;this.validate=
function(b){var c=a(b,this);if(!0!==c)throw new d(this,b,{result:c});return!0};return this},Choice:function(a){this.__class__="Choice";if(!j(a)&&"function"!==typeof a)throw Error("Choice must be instanciated with an array or a function");this.list=a;this.validate=function(a){for(var c="function"===typeof this.list?this.list():this.list,e=0;e<c.length;e++)if(a===c[e])return!0;throw new d(this,a,{choices:c});};return this},Collection:function(a){this.__class__="Collection";this.constraint="undefined"!==
typeof a?new g(a):!1;this.validate=function(a,c){var e,k=new f,g=0,h={},l=this.groups.length?this.groups:c;if(!j(a))throw new d(this,array,{value:f.const.must_be_an_array});for(var m=0;m<a.length;m++)e=this.constraint?k.validate(a[m],this.constraint,l):k.validate(a[m],l),_isEmptyObject(e)||(h[g]=e),g++;return!_isEmptyObject(h)?h:!0};return this},Count:function(a){this.__class__="Count";this.count=a;this.validate=function(a){if(!j(a))throw new d(this,a,{value:f.const.must_be_an_array});var c="function"===
typeof this.count?this.count(a):this.count;if(isNaN(Number(c)))throw Error("Count must be a valid interger",c);if(c!==a.length)throw new d(this,a,{count:c});return!0};return this},Email:function(){this.__class__="Email";this.validate=function(a){if("string"!==typeof a)throw new d(this,a,{value:f.const.must_be_a_string});if(!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(a))throw new d(this,
a);return!0};return this},Eql:function(a){this.__class__="Eql";if("undefined"===typeof a)throw Error("Equal must be instanciated with an Array or an Object");this.eql=a;this.validate=function(a){var c="function"===typeof this.eql?this.eql(a):this.eql;if(!expect.eql(c,a))throw new d(this,a,{eql:c});return!0};return this},EqualTo:function(a){this.__class__="EqualTo";if("undefined"===typeof a)throw Error("EqualTo must be instanciated with a value or a function");this.reference=a;this.validate=function(a){var c=
"function"===typeof this.reference?this.reference(a):this.reference;if(c!==a)throw new d(this,a,{value:c});return!0};return this},GreaterThan:function(a){this.__class__="GreaterThan";if("undefined"===typeof a)throw Error("Should give a threshold value");this.threshold=a;this.validate=function(a){if(isNaN(Number(a)))throw new d(this,a,{value:f.const.must_be_a_number});if(this.threshold>=a)throw new d(this,a,{threshold:this.threshold});return!0};return this},GreaterThanOrEqual:function(a){this.__class__=
"GreaterThanOrEqual";if("undefined"===typeof a)throw Error("Should give a threshold value");this.threshold=a;this.validate=function(a){if(this.threshold>a)throw new d(this,a,{threshold:this.threshold});return!0};return this},Length:function(a){this.__class__="Length";if(!a.min&&!a.max)throw Error("Lenth assert must be instanciated with a { min: x, max: y } object");this.min=a.min;this.max=a.max;this.validate=function(a){if("string"!==typeof a)throw new d(this,a,{value:f.const.must_be_a_string});if("undefined"!==
typeof this.min&&this.min===this.max&&a.length!==this.min)throw new d(this,a,{min:this.min,max:this.max});if("undefined"!==typeof this.max&&a.length>this.max)throw new d(this,a,{max:this.max});if("undefined"!==typeof this.min&&a.length<this.min)throw new d(this,a,{min:this.min});return!0};return this},LessThan:function(a){this.__class__="LessThan";if("undefined"===typeof a)throw Error("Should give a threshold value");this.threshold=a;this.validate=function(a){if(isNaN(Number(a)))throw new d(this,
a,{value:f.const.must_be_a_number});if(this.threshold<=a)throw new d(this,a,{threshold:this.threshold});return!0};return this},LessThanOrEqual:function(a){this.__class__="LessThanOrEqual";if("undefined"===typeof a)throw Error("Should give a threshold value");this.threshold=a;this.validate=function(a){if(this.threshold<a)throw new d(this,a,{threshold:this.threshold});return!0};return this},NotNull:function(){this.__class__="NotNull";this.validate=function(a){if(null===a||"undefined"===typeof a)throw new d(this,
a);return!0};return this},NotBlank:function(){this.__class__="NotBlank";this.validate=function(a){if("string"!==typeof a)throw new d(this,a,{value:f.const.must_be_a_string});if(""===a.replace(/^\s+/g,"").replace(/\s+$/g,""))throw new d(this,a);return!0};return this},Null:function(){this.__class__="Null";this.validate=function(a){if(null!==a)throw new d(this,a);return!0};return this},Range:function(a,b){if(!a||!b)throw Error("Range assert expects min and max values");this.LengthValidator=(new h).Length({min:a,
max:b});this.__class__="Range";this.validate=function(a){try{this.LengthValidator.validate(a)}catch(b){throw new d(this,a,b.violation);}return!0};return this},Regexp:function(a,b){this.__class__="Regexp";if("undefined"===typeof a)throw Error("You must give a regexp");this.regexp=a;this.flag=b||"";this.validate=function(a){if("string"!==typeof a)throw new d(this,a,{value:f.const.must_be_a_string});if(!RegExp(this.regexp).test(a,this.flag))throw new d(this,a,{regexp:this.regexp,flag:this.flag});return!0};
return this},Required:function(){this.__class__="Required";this.validate=function(a){if("undefined"===typeof a)throw new d(this,a);if("string"===typeof a)try{this.NotNull.validate(a)&&this.NotBlank.validate(a)}catch(b){throw new d(this,a);}return!0};return this},Unique:function(a){this.__class__="Unique";"object"===typeof a&&(this.key=a.key);this.validate=function(a){var c,e=[];if(!j(a))throw new d(this,a,{value:f.const.must_be_an_array});for(var k=0;k<a.length;k++)if(c="object"===typeof a[k]?a[k][this.key]:
a[k],"undefined"!==typeof c){if(-1!==e.indexOf(c))throw new d(this,a,{value:c});e.push(c)}return!0};return this}};l.Assert=h;l.Validator=f;l.Violation=d;l.Constraint=g;Array.prototype.indexOf||(Array.prototype.indexOf=function(a){if(null==this)throw new TypeError;var b=Object(this),c=b.length>>>0;if(0===c)return-1;var e=0;1<arguments.length&&(e=Number(arguments[1]),e!=e?e=0:0!=e&&(Infinity!=e&&-Infinity!=e)&&(e=(0<e||-1)*Math.floor(Math.abs(e))));if(e>=c)return-1;for(e=0<=e?e:Math.max(c-Math.abs(e),
0);e<c;e++)if(e in b&&b[e]===a)return e;return-1});_isEmptyObject=function(a){for(var b in a)return!1;return!0};expect={eql:function(a,b){if(a===b)return!0;if("undefined"!==typeof Buffer&&Buffer.isBuffer(a)&&Buffer.isBuffer(b)){if(a.length!==b.length)return!1;for(var c=0;c<a.length;c++)if(a[c]!==b[c])return!1;return!0}return a instanceof Date&&b instanceof Date?a.getTime()===b.getTime():"object"!==typeof a&&"object"!==typeof b?a==b:this.objEquiv(a,b)},isUndefinedOrNull:function(a){return null===a||
"undefined"===typeof a},isArguments:function(a){return"[object Arguments]"==Object.prototype.toString.call(a)},keys:function(a){if(Object.keys)return Object.keys(a);var b=[],c;for(c in a)Object.prototype.hasOwnProperty.call(a,c)&&b.push(c);return b},objEquiv:function(a,b){if(this.isUndefinedOrNull(a)||this.isUndefinedOrNull(b)||a.prototype!==b.prototype)return!1;if(this.isArguments(a))return!this.isArguments(b)?!1:eql(pSlice.call(a),pSlice.call(b));try{var c=this.keys(a),e=this.keys(b),d}catch(f){return!1}if(c.length!==
e.length)return!1;c.sort();e.sort();for(d=c.length-1;0<=d;d--)if(c[d]!=e[d])return!1;for(d=c.length-1;0<=d;d--)if(e=c[d],!this.eql(a[e],b[e]))return!1;return!0}}})("undefined"===typeof exports?this["undefined"!==typeof validatorjs_ns?validatorjs_ns:"Validator"]={}:exports);