This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verja.min.js
1 lines (1 loc) · 4.94 KB
/
verja.min.js
1
(function(undefined){"use strict";var validators={doesNotHave:function(val,config,callback){if(!val)return callback(true);var returnVal=true;if(Array.isArray(config)){config.forEach(function(item){if(val.indexOf(item)>-1)returnVal=false})}else{if(val.indexOf(config)>-1)returnVal=false}return callback(returnVal)},email:function(val,config,callback){if(!val)return callback(true);if(typeof val!=="string"){return callback(false)}var regex=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;if(regex.test(val)){return callback(true)}callback(false)},equals:function(val,config,callback){if(val===config){return callback(true)}callback(false)},hasLowercaseLetter:function(val,config,callback){var regex=/[a-z]/;if(regex.test(val))return callback(config);callback(!config)},hasCapitalLetter:function(val,config,callback){var regex=/[A-Z]/;if(regex.test(val))return callback(config);callback(!config)},hasNumber:function(val,config,callback){var regex=/[0-9]/;if(regex.test(val))return callback(config);callback(!config)},"int":function(val,config,callback){if(Math.round(val)===val){return callback(true)}callback(false)},max:function(val,config,callback){if(typeof val!=="number"||!val||val>config){return callback(false)}callback(true)},maxlength:function(val,config,callback){try{if(val.length<=config){return callback(true)}else{return callback(false)}}catch(e){return callback(false)}},min:function(val,config,callback){if(typeof val!=="number"||!val||val<config){return callback(false)}callback(true)},minlength:function(val,config,callback){if(!val||!val.length||val.length<config){return callback(false)}callback(true)},regex:function(val,config,callback){if(val.search(config)>-1){return callback(true)}callback(false)},required:function(val,config,callback){if(config){if(!val||typeof val===undefined){return callback(false)}}callback(true)},type:function(val,config,callback){var valtype=Object.prototype.toString.call(val);valtype=valtype.substr(8,valtype.length-9).toLowerCase();if(valtype===config){return callback(true)}callback(false,"type")},url:function(val,config,callback){var regex=/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/;if(regex.test(val)||!val){return callback(true)}callback(false)}};function addValidator(name,func){validators[name]=func}function validate(object,schema,callback){var s=new Schema(schema,callback);s.accumulateValidators(object);s.run()}function Schema(schema,userCallback){this.schema=schema;this.errors={};this.validationStatus={totalValidators:0,totalValidated:0,errorTotal:0};this.validateFuncs=[];this.userCallback=userCallback}Schema.prototype.accumulateValidators=function(object,schema,errors){var self=this;if(!schema){schema=self.schema}if(!errors){errors=self.errors}if(schema instanceof Field){if(schema.itemSchema&&Array.isArray(object)){object.forEach(function(arrayValue,index){self.addPropertyToErrors(errors,index);self.accumulateValidators(object[index],schema.itemSchema,errors[index])})}Object.keys(schema).forEach(function(validatorName){if(validators[validatorName]){self.addValidationFunction(object,schema,errors,validatorName)}else{}})}else if(schema instanceof Object&&object instanceof Object){Object.keys(schema).forEach(function(property){self.addPropertyToErrors(errors,property);self.accumulateValidators(object[property],schema[property],errors[property])})}else{throw new Error("Internal Validation error for ",object,schema,errors)}};Schema.prototype.addValidationFunction=function(object,schema,errors,validatorName){var self=this;self.validateFuncs.push(function(){var validatorCallback=self.generateValidatorCallback(object,schema,errors,validatorName);validators[validatorName](object,schema[validatorName],validatorCallback)});self.validationStatus.totalValidators++};Schema.prototype.generateValidatorCallback=function(object,schema,errors,validatorName){var self=this;return function(valid){if(!valid){errors[validatorName]=true;self.validationStatus.errorTotal++}self.validationStatus.totalValidated++;self.checkValidationComplete()}};Schema.prototype.addPropertyToErrors=function(errors,property){if(!errors[property]){errors[property]={}}};Schema.prototype.run=function(){this.validateFuncs.forEach(function(func){func()})};Schema.prototype.checkValidationComplete=function(){var self=this;if(self.validationStatus.totalValidated===self.validationStatus.totalValidators){if(!self.validationStatus.errorTotal){return self.userCallback(null)}self.userCallback(self.errors)}};function Field(props){for(var prop in props){this[prop]=props[prop]}}function strip(obj,isRecurse){if(!isRecurse){obj=new Field(obj)}Object.keys(obj).forEach(function(key){if(obj[key]instanceof Object&&!Object.keys(obj[key]).length){delete obj[key];obj=strip(obj,true)}else if(obj[key]instanceof Object){obj[key]=strip(obj[key],true)}});return obj}var exports={addValidator:addValidator,validate:validate,Field:Field,validators:validators,strip:strip};if(typeof window!=="undefined"){window.verja=window.verja||exports}else if(module){module.exports=exports}})();