como-validator.c3b2e2bb.js 7.5 KB

1
  1. var e={};var r=class{constructor(e){this._data=e}required(e){return this._data.hasOwnProperty(e)}chinese(e){if(!this.required(e))return!1;return/^[\u0391-\uFFE5]+$/.test(this._data[e])}letter(e){if(!this.required(e))return!1;return/^[a-zA-Z]*$/.test(this._data[e])}letter_number(e){if(!this.required(e))return!1;return/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/.test(this._data[e])}letterNumber(e){if(!this.required(e))return!1;return/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/.test(this._data[e])}qq(e){if(!this.required(e))return!1;return/^[1-9][0-9]{4,9}$/.test(this._data[e])}url(e){if(!this.required(e))return!1;return/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(this._data[e])}ip(e){if(!this.required(e))return!1;return!!/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(this._data[e])&&(RegExp.$1<256&&RegExp.$2<256&&RegExp.$3<256&&RegExp.$4<256)}min(e,r){return!!this.required(e)&&!(this._data[e]<r)}max(e,r){return!!this.required(e)&&!(this._data[e]>r)}max_length(e,r){return!!this.required(e)&&!((this._data[e]+"").length>r)}maxLength(e,r){return!!this.required(e)&&!((this._data[e]+"").length>r)}min_length(e,r){return!!this.required(e)&&!((this._data[e]+"").length<r)}minLength(e,r){return!!this.required(e)&&!((this._data[e]+"").length<r)}number(e){let r=this;try{if(!r.required(e))return!1;let t=Number(r._data[e]);return!isNaN(t)&&(r._data[e]=t,!0)}catch(t){return!1}}email(e){return!!this.required(e)&&/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this._data[e])}id_card(e){if(!this.required(e))return!1;return/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this._data[e])}idCard(e){if(!this.required(e))return!1;return/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this._data[e])}object(e){return!!this.required(e)&&this._data[e].constructor==Object}phone(e){if(!this._data.hasOwnProperty(e))return!0;return/^(1[3456789][0-9]{9})$/.test(this._data[e])}in_array(e,r){return!!this.required(e)&&-1!=r.indexOf(this._data[e])}inArray(e,r){return!!this.required(e)&&-1!=r.indexOf(this._data[e])}is_array(e){return!!this.required(e)&&this._data[e]instanceof Array}isArray(e){return!!this.required(e)&&this._data[e]instanceof Array}date(e){if(!this.required(e))return!1;let r=(this._data[e]+"").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);if(null==r)return!1;var t=new Date(r[1],r[3]-1,r[4]);return t.getFullYear()==r[1]&&t.getMonth()+1==r[3]&&t.getDate()==r[4]}notEmpty(e){if(!this.required(e))return!1;let r=this._data[e];return!!r&&!(r.toString().length<=0)}trim(e,r){let t,s=this._data[e];switch(r){case 2:t=s.replace(/(^\s*)|(\s*$)/g,"");case 3:t=s.replace(/(^\s*)/g,"");case 4:t=s.replace(/(\s*$)/g,"");default:t=s.replace(/\s+/g,"")}return this._data[e]=t,!0}is_json(e){let r=this._data[e];if("string"==typeof r)try{return JSON.parse(r),!0}catch(t){return!1}return!1}isJson(e){let r=this._data[e];if("string"==typeof r)try{return JSON.parse(r),!0}catch(t){return!1}return!1}ref(e,r){let t=this;return!!t.required(e)&&t._data[e]==t._data[r]}};class t{constructor(){}required(e){return e=e||"不存在",this._setErrorMessage("required",{message:e,params:null}),this}number(e){return e=e||"必须是一个数字",this._setErrorMessage("number",{message:e,params:null}),this}email(e){return e=e||"必须是一个邮箱格式",this._setErrorMessage("email",{message:e,params:null}),this}max_length(e,r){return r=r||"最大长度不能超过"+e+"字符",this._setErrorMessage("max_length",{message:r,params:e}),this}maxLength(e,r){return r=r||"最大长度不能超过"+e+"字符",this._setErrorMessage("maxLength",{message:r,params:e}),this}min_length(e,r){return r=r||"最小长度不能低"+e+"个字符",this._setErrorMessage("min_length",{message:r,params:e}),this}minLength(e,r){return r=r||"最小长度不能低"+e+"个字符",this._setErrorMessage("minLength",{message:r,params:e}),this}max(e,r){return r=r||"最大值不能超过"+e,this._setErrorMessage("max",{message:r,params:e}),this}min(e,r){return r=r||"最小值不能低于"+e,this._setErrorMessage("min",{message:r,params:e}),this}letter(e){return e=e||"必须是字母",this._setErrorMessage("letter",{message:e,params:null}),this}chinese(e){return e=e||"必须是中文",this._setErrorMessage("chinese",{message:e,params:null}),this}letter_number(e){return e=e||"必须是字母和数字",this._setErrorMessage("letter_number",{message:e,params:null}),this}letterNumber(e){return e=e||"必须是字母和数字",this._setErrorMessage("letterNumber",{message:e,params:null}),this}phone(e){return e=e||"必须是一个有效的手机号格式",this._setErrorMessage("phone",{message:e,params:null}),this}object(e){return e=e||"必须是一个对象",this._setErrorMessage("object",{message:e,params:null}),this}is_array(e){return e=e||"必须是一个数组",this._setErrorMessage("is_array",{message:e,params:null}),this}isArray(e){return e=e||"必须是一个数组",this._setErrorMessage("isArray",{message:e,params:null}),this}qq(e){return e=e||"不是一个有效的qq格式",this._setErrorMessage("qq",{message:e,params:null}),this}url(e){return e=e||"必须是一个有效的url",this._setErrorMessage("url",{message:e,params:null}),this}in_array(e,r){return r=r||"必须是["+e.join(",")+"]其中一个",this._setErrorMessage("in_array",{message:r,params:e}),this}inArray(e,r){return r=r||"必须是["+e.join(",")+"]其中一个",this._setErrorMessage("inArray",{message:r,params:e}),this}ip(e){return e=e||"不是一个有效的ip地址",this._setErrorMessage("ip",{message:e,params:null}),this}ref(e,r){return r=r||"两次不一致",this._setErrorMessage("ref",{message:r,params:e}),this}id_card(e){return e=e||"不是一个有效的格式",this._setErrorMessage("id_card",{message:e,params:null}),this}idCard(e){return e=e||"不是一个有效的格式",this._setErrorMessage("idCard",{message:e,params:null}),this}date(e){return e=e||"必须是一个日期格式",this._setErrorMessage("date",{message:e,params:null}),this}name(e){return e=e||!1,this.name=e,this}extend(e){return this._setErrorMessage("extend",e),this}default(e){return this.default=e,this}notEmpty(e){return e=e||"不能为空",this._setErrorMessage("notEmpty",{message:e,params:null}),this}trim(e=2,r="必须是一个字符串"){return this._setErrorMessage("trim",{message:r,params:e}),this}isJson(e){return e=e||"必须是json格式",this._setErrorMessage("isJson",{message:e,params:null}),this}is_json(e){return e=e||"必须是json格式",this._setErrorMessage("is_json",{message:e,params:null}),this}_setErrorMessage(e,r){let t=this;t.validate||(t.validate=[]),t.validate.push(Object.assign(r,{validator:e}))}static getInstance(){return new t}}const s=r,a=t;class i extends Error{constructor(e="",r="comovalidator",t={}){super(e),r&&"object"!=typeof r||(t=r,r="COMO_VALIDATOR_10000"),this.name=this.constructor.name,this.code=r,this.cause=null==t?void 0:t.cause}}e.comoRules=a,e.validate=async function(e,r){r=await async function(e,r){for(let t in e)e[t].hasOwnProperty("default")&&null==r[t]&&(e[t].default instanceof Function?r[t]=await e[t].default(r):r[t]=e[t].default);return r}(e,r);let t=new s(r);for(let s in e){if(!(e[s]instanceof a))throw new i("Validation rule must be an instance of rule type");{let a=s,n=e[s].validate,u=n.length;for(let h=0;h<u;h++){let u=n[h];if("object"==typeof u){if(u.validator&&"function"==typeof t[u.validator]){let r="";if(e[s].hasOwnProperty("name")&&(r=e[s].name),u.params){if(!t[u.validator].call(t,a,u.params))throw new i(r+u.message)}else{if(!t[u.validator].call(t,a))throw new i(r+u.message)}}}else if("function"==typeof u){let e=await u.call(t,a,r[a],r);if(e)throw new i(e)}}}}return t._data},e.ComoValidatorError=i;export{e as m};