warble
Version:
An universal javascript validation library.
3 lines (2 loc) • 7.82 kB
JavaScript
;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}{var specificTypes={};['Array','Boolean','Date','Error','Function','Number','Object','RegExp','String','Symbol'].forEach(function(value){return specificTypes['[object '+value+']']=value.toLowerCase();});var Core=function(){function Core(){_classCallCheck(this,Core);this.regex={email:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,numeric:/^\-?\d+(?:\.\d+)?$/,integer:/^\-?\d+$/,positive:/^\d+(?:\.\d+)?$/,negative:/^\-\d+(?:\.\d+)?$/};var regex=this.regex;this.subtypes={email:function email(value){return regex.email.test(value);},numeric:function numeric(value){return regex.numeric.test(value);},integer:function integer(value){return regex.integer.test(value);},positive:function positive(value){return regex.positive.test(value);},negative:function negative(value){return regex.negative.test(value);},even:function even(value){return value%2===0;},odd:function odd(value){return value%2>0;}};this.validations={required:function required(value,isRequired){return isRequired?value!==undefined:true;},pattern:function pattern(value,_pattern){return(core.is(_pattern,'regexp')?_pattern:new RegExp(_pattern)).test(value);},min:function min(value){var _min=arguments.length>1&&arguments[1]!==undefined?arguments[1]:-Infinity;return(Number(value)||0)>=(Number(_min)||0);},max:function max(value){var _max=arguments.length>1&&arguments[1]!==undefined?arguments[1]:Infinity;return(Number(value)||0)<=(Number(_max)||0);},minlength:function minlength(value){var min=arguments.length>1&&arguments[1]!==undefined?arguments[1]:0;return typeof value==='string'?value.length>=(Number(min)||0):false;},maxlength:function maxlength(value){var max=arguments.length>1&&arguments[1]!==undefined?arguments[1]:Infinity;return typeof value==='string'?value.length<=(Number(max)||0):false;},type:function type(value,_type){return core.type(value)===_type;},is:function is(value,types){return core.is(value,types,true);},range:function range(value){var _range=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[-Infinity,Infinity];return core.is(_range,'array')&&_range.length>1?Number(value)>=Number(_range[0])&&Number(value)<=Number(_range[1]):false;},equal:function equal(value,to){return _typeof(this)==='object'&&this.hasOwnProperty(to)?value===this[to]:false;},conditional:function conditional(value,_conditional){return _conditional(value);},options:function options(value,_options){if(core.is(_options,'array'))for(var index=0,size=_options.length;index<size;index++){if(value===_options[index])return true;}return false;},instance:function instance(value,object){return value instanceof object;}};}_createClass(Core,[{key:'type',value:function type(value){return value===null?'null':(typeof value==='undefined'?'undefined':_typeof(value))==='object'||typeof value==='function'?specificTypes[specificTypes.toString.call(value)]||'object':typeof value==='undefined'?'undefined':_typeof(value);}},{key:'is',value:function is(value,types,getDetails){var response={},subtypes=this.subtypes,type=this.type;if(type(types)==='array'){for(var index=0,size=types.length;index<size;index++){var test=subtypes[types[index]];if(typeof test==='function')response[types[index]]=test.call(this,value);else response[types[index]]=type(value)===types[index];}}else if(typeof subtypes[types]==='function')response[types]=subtypes[types].call(this,value);else response[types]=type(value)===types;if(getDetails)return response;for(var _index in response){if(!response[_index])return false;}return true;}}]);return Core;}();var core=new Core();var Response=function(){function Response(){_classCallCheck(this,Response);this.data={};this.valid=true;}_createClass(Response,[{key:'setData',value:function setData(index,value){this.data[index]=value;if(!value.valid)this.valid=false;}}]);return Response;}();var ResponseFragment=function(){function ResponseFragment(value){_classCallCheck(this,ResponseFragment);this.value=value;this.valid=true;this.status={};}_createClass(ResponseFragment,[{key:'validate',value:function validate(name,param,parent){var response=typeof core.validations[name]==='function'?core.validations[name].call(parent,this.value,param):true;if(!response)this.valid=false;if((typeof response==='undefined'?'undefined':_typeof(response))==='object'){var status=true;for(var index in response){this.status[name+index[0].toUpperCase()+index.substr(1)]=response[index];if(!response[index])status=false;}this.status[name]=status;}else this.status[name]=response;return this;}}]);return ResponseFragment;}();var Model=function(){function Model(model){_classCallCheck(this,Model);if((typeof model==='undefined'?'undefined':_typeof(model))==='object')this.model=model;else throw new Error('The expected param must be an object.');}_createClass(Model,[{key:'validate',value:function validate(data){if((typeof data==='undefined'?'undefined':_typeof(data))==='object'){var response=new Response();for(var index in this.model){var value=new ResponseFragment(data[index]);if(this.model[index]instanceof Model)response.setData(index,this.model[index].validate(data[index]));else if(_typeof(this.model[index])==='object'){for(var validation in this.model[index]){value.validate(validation,this.model[index][validation],data);}response.setData(index,value);}}return response;}else throw new Error('The expected param must be an object.');}}]);return Model;}();var Warble=function(_Core){_inherits(Warble,_Core);function Warble(){_classCallCheck(this,Warble);return _possibleConstructorReturn(this,(Warble.__proto__||Object.getPrototypeOf(Warble)).apply(this,arguments));}_createClass(Warble,[{key:'model',value:function model(_model){return new Model(_model);}},{key:'validate',value:function validate(value,options){if((typeof options==='undefined'?'undefined':_typeof(options))==='object'){var value=new ResponseFragment(value);for(var index in options){value.validate(index,options[index]);}return value;}else throw error('The expected param must be an object.');}}]);return Warble;}(Core);var warble=new Warble();if((typeof module==='undefined'?'undefined':_typeof(module))==='object'&&_typeof(module.exports)==='object')module.exports=warble;}
//# sourceMappingURL=warble.min.js.map