@regle/rules 0.5.9 → 0.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/regle-rules.cjs +43 -0
- package/dist/regle-rules.d.cts +15 -1
- package/dist/regle-rules.d.ts +15 -1
- package/dist/regle-rules.min.cjs +1 -1
- package/dist/regle-rules.min.mjs +1 -1
- package/dist/regle-rules.mjs +43 -2
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -20,7 +20,6 @@ It's heavily inspired by Vuelidate.
|
|
|
20
20
|
| [](https://stackblitz.com/~/github.com/victorgarciaesgi/regle-examples/tree/main/examples/simple-example?file=examples/simple-example/src/App.vue&configPath=examples/simple-example) | [](https://stackblitz.com/~/github.com/victorgarciaesgi/regle-examples/tree/main/examples/advanced-example?file=examples/advanced-example/src/App.vue&configPath=examples/advanced-example) |
|
|
21
21
|
|
|
22
22
|
## 🧰 Features
|
|
23
|
-
- ✅ Typescript first
|
|
24
23
|
- 🤖 100% type inference
|
|
25
24
|
- 📖 Model based validation
|
|
26
25
|
- 🛒 Collection validation
|
package/dist/regle-rules.cjs
CHANGED
|
@@ -862,6 +862,47 @@ var regex = core.createRule({
|
|
|
862
862
|
},
|
|
863
863
|
message: "This field does not match the required pattern"
|
|
864
864
|
});
|
|
865
|
+
function oneOf(options) {
|
|
866
|
+
const params = vue.computed(() => vue.toValue(options));
|
|
867
|
+
const rule = withMessage(
|
|
868
|
+
withParams(
|
|
869
|
+
(value, options2) => {
|
|
870
|
+
if (isFilled(value) && isFilled(options2)) {
|
|
871
|
+
return options2.includes(value);
|
|
872
|
+
}
|
|
873
|
+
return true;
|
|
874
|
+
},
|
|
875
|
+
[params]
|
|
876
|
+
),
|
|
877
|
+
({ $params: [options2] }) => `Value should be one of those options: ${options2.join(", ")}.`
|
|
878
|
+
);
|
|
879
|
+
return rule;
|
|
880
|
+
}
|
|
881
|
+
function getValidEnumValues(obj) {
|
|
882
|
+
const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
883
|
+
const filtered = {};
|
|
884
|
+
for (const k of validKeys) {
|
|
885
|
+
filtered[k] = obj[k];
|
|
886
|
+
}
|
|
887
|
+
return Object.values(filtered);
|
|
888
|
+
}
|
|
889
|
+
function nativeEnum(enumLike) {
|
|
890
|
+
const params = vue.computed(() => vue.toValue(enumLike));
|
|
891
|
+
const rule = withMessage(
|
|
892
|
+
withParams(
|
|
893
|
+
(value, enumLike2) => {
|
|
894
|
+
if (isFilled(value) && !isEmpty(enumLike2)) {
|
|
895
|
+
const validValues = getValidEnumValues(enumLike2);
|
|
896
|
+
return validValues.includes(value);
|
|
897
|
+
}
|
|
898
|
+
return true;
|
|
899
|
+
},
|
|
900
|
+
[params]
|
|
901
|
+
),
|
|
902
|
+
({ $params: [enumLike2] }) => `Value should be one of those options: ${Object.values(enumLike2).join(", ")}.`
|
|
903
|
+
);
|
|
904
|
+
return rule;
|
|
905
|
+
}
|
|
865
906
|
|
|
866
907
|
exports.alpha = alpha;
|
|
867
908
|
exports.alphaNum = alphaNum;
|
|
@@ -891,8 +932,10 @@ exports.maxLength = maxLength;
|
|
|
891
932
|
exports.maxValue = maxValue;
|
|
892
933
|
exports.minLength = minLength;
|
|
893
934
|
exports.minValue = minValue;
|
|
935
|
+
exports.nativeEnum = nativeEnum;
|
|
894
936
|
exports.not = not;
|
|
895
937
|
exports.numeric = numeric;
|
|
938
|
+
exports.oneOf = oneOf;
|
|
896
939
|
exports.or = or;
|
|
897
940
|
exports.regex = regex;
|
|
898
941
|
exports.required = required;
|
package/dist/regle-rules.d.cts
CHANGED
|
@@ -424,4 +424,18 @@ declare const endsWith: RegleRuleWithParamsDefinition<string, [part: Maybe<strin
|
|
|
424
424
|
*/
|
|
425
425
|
declare const regex: RegleRuleWithParamsDefinition<string | number, [...regexp: RegExp[]], false, boolean>;
|
|
426
426
|
|
|
427
|
-
|
|
427
|
+
/**
|
|
428
|
+
* Allow only one of the values from a fixed Array of possible entries.
|
|
429
|
+
*/
|
|
430
|
+
declare function oneOf<const TValues extends [string | number, ...(string | number)[]]>(options: MaybeRefOrGetter<[...TValues]>): RegleRuleDefinition<TValues[number], [options: TValues], false, boolean, string | number>;
|
|
431
|
+
|
|
432
|
+
type EnumLike = {
|
|
433
|
+
[k: string]: string | number;
|
|
434
|
+
[nu: number]: string;
|
|
435
|
+
};
|
|
436
|
+
/**
|
|
437
|
+
* Validate against a native Typescript enum value.
|
|
438
|
+
*/
|
|
439
|
+
declare function nativeEnum<T extends EnumLike>(enumLike: T): RegleRuleDefinition<T[keyof T], [enumLike: T], false, boolean, string | number>;
|
|
440
|
+
|
|
441
|
+
export { type EnumLike, alpha, alphaNum, and, applyIf, between, checked, contains, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, integer, ipAddress, isDate, isEmpty, isFilled, isNumber, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, toDate, toNumber, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.d.ts
CHANGED
|
@@ -424,4 +424,18 @@ declare const endsWith: RegleRuleWithParamsDefinition<string, [part: Maybe<strin
|
|
|
424
424
|
*/
|
|
425
425
|
declare const regex: RegleRuleWithParamsDefinition<string | number, [...regexp: RegExp[]], false, boolean>;
|
|
426
426
|
|
|
427
|
-
|
|
427
|
+
/**
|
|
428
|
+
* Allow only one of the values from a fixed Array of possible entries.
|
|
429
|
+
*/
|
|
430
|
+
declare function oneOf<const TValues extends [string | number, ...(string | number)[]]>(options: MaybeRefOrGetter<[...TValues]>): RegleRuleDefinition<TValues[number], [options: TValues], false, boolean, string | number>;
|
|
431
|
+
|
|
432
|
+
type EnumLike = {
|
|
433
|
+
[k: string]: string | number;
|
|
434
|
+
[nu: number]: string;
|
|
435
|
+
};
|
|
436
|
+
/**
|
|
437
|
+
* Validate against a native Typescript enum value.
|
|
438
|
+
*/
|
|
439
|
+
declare function nativeEnum<T extends EnumLike>(enumLike: T): RegleRuleDefinition<T[keyof T], [enumLike: T], false, boolean, string | number>;
|
|
440
|
+
|
|
441
|
+
export { type EnumLike, alpha, alphaNum, and, applyIf, between, checked, contains, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, integer, ipAddress, isDate, isEmpty, isFilled, isNumber, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, toDate, toNumber, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.min.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var core=require('@regle/core'),vue=require('vue');function V(e,t){let i,o,u,f;typeof e=="function"&&!("_validator"in e)?(i=core.InternalRuleType.Inline,o=e):{_type:i,validator:o,_active:u,_params:f}=e;let m=core.createRule({type:i,validator:o,active:u,message:t}),R=[...f??[]];if(m._params=R,m._message_patched=!0,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function v(e,t){let i,o,u,f,m;typeof e=="function"&&!("_validator"in e)?(i=core.InternalRuleType.Inline,o=e):{_type:i,validator:o,_active:u,_params:f,_message:m}=e;let R=core.createRule({type:i,validator:o,active:u,message:m,tooltip:t}),r=[...f??[]];if(R._params=r,R._tooltip_patched=!0,typeof R=="function"){let s=R(...r);return R._tooltip_patched=!0,s}else return R}function N(e,t){let i=async(u,...f)=>e(u,...f),o=core.createRule({type:core.InternalRuleType.Async,validator:i,message:""});return o._params=o._params?.concat(t),o(...t??[])}function k(e,t){let i=(u,...f)=>e(u,...f),o=core.createRule({type:core.InternalRuleType.Inline,validator:i,message:""});return o._params=o._params?.concat(t),o(...t)}function C(e,t){let i,o,u=[],f="";typeof t=="function"?(i=core.InternalRuleType.Inline,o=t,u=[e]):({_type:i,validator:o,_message:f}=t,u=t._params?.concat([e]));function m(n,...p){let[b]=core.unwrapRuleParameters([e]);return b?o(n,...p):!0}function R(n){let[p]=core.unwrapRuleParameters([e]);return p}let r=core.createRule({type:i,validator:m,active:R,message:f}),s=[...u??[]];if(r._params=s,typeof r=="function"){let n=r(...s);return n._message_patched=!0,n}else return r}function l(e){return e==null?!0:e instanceof Date?isNaN(e.getTime()):Array.isArray(e)?!1:typeof e=="object"&&e!=null?Object.keys(e).length===0:!String(e).length}function a(e){return !l(typeof e=="string"?e.trim():e)}function y(e){return e==null||typeof e!="number"?!1:!isNaN(e)}function g(e,...t){if(l(e))return !0;let i=typeof e=="number"?e.toString():e;return t.every(o=>(o.lastIndex=0,o.test(i)))}function D(e){let t=vue.unref(e);return Array.isArray(t)?t.length:typeof t=="object"?Object.keys(t).length:typeof t=="number"?isNaN(t)?0:t:String(t).length}function x(e){if(l(e))return !1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e=="number"&&!isNaN(e))t=new Date(e);else if(typeof e=="string"){let i=new Date(e);if(i.toString()==="Invalid Date")return !1;t=i;}return !!t}catch{return !1}}function T(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e=="object"&&t==="[object Date]"?new Date(e.getTime()):typeof e=="number"||t==="[object Number]"?new Date(e):typeof e=="string"||t==="[object String]"?new Date(e):new Date(NaN)}function d(e){return typeof e=="number"?e:e!=null?typeof e=="string"?e.trim()!==e?NaN:+e:NaN:NaN}function q(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>{if(typeof r=="function")return null;{let s=r._params;return s?.length?s:[]}}).flat().filter(r=>!!r);function o(r,s,...n){let p=[],b=0;for(let c of r)if(typeof c=="function")p.push(c(s)),b++;else {let M=c._params?.length??0;p.push(c.validator(s,...n.slice(b,M))),M&&(b+=M);}return p}function u(r){return r?.some(n=>typeof n!="boolean")?{$valid:r.every(n=>typeof n=="boolean"?!!n:n.$valid),...r.reduce((n,p)=>{if(typeof p=="boolean")return n;let{$valid:b,...c}=p;return {...n,...c}},{})}:r.every(n=>!!n)}let f;e.length?f=t?async(r,...s)=>{let n=await Promise.all(o(e,r,...s));return u(n)}:(r,...s)=>{let n=o(e,r,...s);return u(n)}:f=r=>!1;let m=core.createRule({type:"and",validator:f,message:"The value does not match all of the provided validators"}),R=[...i??[]];if(m._params=R,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function O(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>typeof r=="function"?null:r._params).flat().filter(r=>!!r);function o(r,s,...n){let p=[],b=0;for(let c of r)if(typeof c=="function")p.push(c(s)),b++;else {let M=c._params?.length??0;p.push(c.validator(s,...n.slice(b,M))),M&&(b+=M);}return p}function u(r){return r.some(n=>typeof n!="boolean")?{$valid:r.some(n=>typeof n=="boolean"?!!n:n.$valid),...r.reduce((n,p)=>{if(typeof p=="boolean")return n;let{$valid:b,...c}=p;return {...n,...c}},{})}:r.some(n=>!!n)}let m=core.createRule({type:"or",validator:t?async(r,...s)=>{let n=await Promise.all(o(e,r,...s));return u(n)}:(r,...s)=>{let n=o(e,r,...s);return u(n)},message:"The value does not match any of the provided validators"}),R=[...i??[]];if(m._params=R,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function j(e,t){let i,o,u,f,m;typeof e=="function"?(o=e,m=e.constructor.name==="AsyncFunction"):({_type:i,validator:o,_params:f}=e,m=e._async),m?u=async(s,...n)=>a(s)?!await o(s,...n):!0:u=(s,...n)=>a(s)?!o(s,...n):!0;let R=core.createRule({type:"not",validator:u,message:t??"Error"}),r=[...f??[]];if(R._params=r,typeof R=="function"){let s=R(...r);return s._message_patched=!0,s}else return R}var zt=core.createRule({type:"required",validator:e=>a(e),message:"This field is required"});var Ut=core.createRule({type:"maxLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)<=t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The maximum length allowed is ${t}`});var Kt=core.createRule({type:"required",validator(e,t){return t?a(e):!0},message:"This field is required",active({$params:[e]}){return e}});var Q=/^[a-zA-Z]*$/,Ht=core.createRule({type:"alpha",validator(e){return l(e)?!0:g(e,Q)},message:"The value is not alphabetical"});var Y=/^[a-zA-Z0-9]*$/,Yt=core.createRule({type:"alpha",validator(e){return l(e)?!0:g(e,Y)},message:"The value must be alpha-numeric"});var ar=core.createRule({type:"between",validator:(e,t,i)=>{if(a(e)&&a(t)&&a(i)){let o=d(e),u=d(t),f=d(i);return y(o)&&y(u)&&y(f)?o>=u&&o<=f:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${i}`),!1)}return !0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`});var re=/^[-]?\d*(\.\d+)?$/,sr=core.createRule({type:"decimal",validator(e){return l(e)?!0:g(e,re)},message:"Value must be decimal"});var ne=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,fr=core.createRule({type:"email",validator(e){return l(e)?!0:g(e,ne)},message:"Value must be an valid email address"});var oe=/(^[0-9]*$)|(^-[0-9]+$)/,gr=core.createRule({type:"integer",validator(e){return l(e)?!0:g(e,oe)},message:"Value must be an integer"});var xr=core.createRule({type:"maxValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)<=t:(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The maximum value allowed is ${e}`});var hr=core.createRule({type:"minLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)>=t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have at least ${t} items`:`This field should be at least ${t} characters long`});var Vr=core.createRule({type:"minValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)>=t:(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The minimum value allowed is ${e}`});var _r=core.createRule({type:"exactValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`});var Er=core.createRule({type:"exactLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`This field should be exactly ${e} characters long`});var pe=/^\d*(\.\d+)?$/,Cr=core.createRule({type:"numeric",validator(e){return l(e)?!0:g(e,pe)},message:"This field must be numeric"});var Gr=core.createRule({type:"required",validator(e,t){return t?!0:a(e)},message:"This field is required",active({$params:[e]}){return !e}});var Br=core.createRule({type:"sameAs",validator(e,t,i="other"){return l(e)?!0:e===t},message({$params:[e,t]}){return `Value must be equal to the ${t} value`}});var ce=/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,Qr=core.createRule({type:"url",validator(e){return l(e)?!0:g(e,ce)},message:"The value is not a valid URL address"});function Te(){return navigator.languages!=null?navigator.languages[0]:navigator.language}function h(e){return e?new Intl.DateTimeFormat(Te(),{dateStyle:"short"}).format(new Date(e)):"?"}var aa=core.createRule({type:"dateAfter",validator:(e,t)=>a(e)&&a(t)?x(e)&&x(t)?T(e).getTime()>T(t).getTime()?!0:{$valid:!1,error:"date-not-after"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The inputs must be Dates":`The date must be after ${h(e)}`});var la=core.createRule({type:"dateBefore",validator:(e,t)=>a(e)&&a(t)?x(e)&&x(t)?T(e).getTime()<T(t).getTime()?!0:{$valid:!1,error:"date-not-before"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The fields must be Dates":`The date must be before ${h(e)}`});var pa=core.createRule({type:"dateBetween",validator:(e,t,i)=>x(e)&&x(t)&&x(i)?T(e).getTime()>T(t).getTime()&&T(e).getTime()<T(i).getTime():!0,message:({$params:[e,t]})=>`The date must be between ${h(e)} and ${h(t)}`});function he(e){if(e.length>3||e.length===0||e[0]==="0"&&e!=="0"||!e.match(/^\d+$/))return !1;let t=+e|0;return t>=0&&t<=255}var ca=core.createRule({type:"ipAddress",validator(e){if(l(e))return !0;if(typeof e!="string")return !1;let t=e.split(".");return t.length===4&&t.every(he)},message:"The value is not a valid IP address"});var Da=core.createRule({type:"macAddress",validator(e,t=":"){if(l(e))return !0;if(typeof e!="string")return !1;let i=typeof t=="string"&&t!==""?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return i!==null&&(i.length===6||i.length===8)&&i.every(we)},message:"The value is not a valid MAC Address"}),we=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/);var wa=core.createRule({type:"checked",validator:e=>a(e)?e===!0:!0,message:"This field must be checked"});var Wa=core.createRule({type:"contains",validator(e,t){return a(e)&&a(t)?e.includes(t):!0},message({$params:[e]}){return `Field must contain ${e}`}});var Na=core.createRule({type:"startsWith",validator(e,t){return a(e)&&a(t)?e.startsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var za=core.createRule({type:"endsWith",validator(e,t){return a(e)&&a(t)?e.endsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var Ua=core.createRule({type:"regex",validator(e,...t){return a(e)?g(e,...t):!0},message:"This field does not match the required pattern"});exports.alpha=Ht;exports.alphaNum=Yt;exports.and=q;exports.applyIf=C;exports.between=ar;exports.checked=wa;exports.contains=Wa;exports.dateAfter=aa;exports.dateBefore=la;exports.dateBetween=pa;exports.decimal=sr;exports.email=fr;exports.endsWith=za;exports.exactLength=Er;exports.exactValue=_r;exports.getSize=D;exports.integer=gr;exports.ipAddress=ca;exports.isDate=x;exports.isEmpty=l;exports.isFilled=a;exports.isNumber=y;exports.macAddress=Da;exports.matchRegex=g;exports.maxLength=Ut;exports.maxValue=xr;exports.minLength=hr;exports.minValue=Vr;exports.not=j;exports.numeric=Cr;exports.or=O;exports.regex=Ua;exports.required=zt;exports.requiredIf=Kt;exports.requiredUnless=Gr;exports.sameAs=Br;exports.startsWith=Na;exports.toDate=T;exports.toNumber=d;exports.url=Qr;exports.withAsync=N;exports.withMessage=V;exports.withParams=k;exports.withTooltip=v;
|
|
1
|
+
'use strict';var core=require('@regle/core'),vue=require('vue');function P(e,t){let i,n,s,m;typeof e=="function"&&!("_validator"in e)?(i=core.InternalRuleType.Inline,n=e):{_type:i,validator:n,_active:s,_params:m}=e;let f=core.createRule({type:i,validator:n,active:s,message:t}),R=[...m??[]];if(f._params=R,f._message_patched=!0,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function F(e,t){let i,n,s,m,f;typeof e=="function"&&!("_validator"in e)?(i=core.InternalRuleType.Inline,n=e):{_type:i,validator:n,_active:s,_params:m,_message:f}=e;let R=core.createRule({type:i,validator:n,active:s,message:f,tooltip:t}),r=[...m??[]];if(R._params=r,R._tooltip_patched=!0,typeof R=="function"){let l=R(...r);return R._tooltip_patched=!0,l}else return R}function I(e,t){let i=async(s,...m)=>e(s,...m),n=core.createRule({type:core.InternalRuleType.Async,validator:i,message:""});return n._params=n._params?.concat(t),n(...t??[])}function w(e,t){let i=(s,...m)=>e(s,...m),n=core.createRule({type:core.InternalRuleType.Inline,validator:i,message:""});return n._params=n._params?.concat(t),n(...t)}function L(e,t){let i,n,s=[],m="";typeof t=="function"?(i=core.InternalRuleType.Inline,n=t,s=[e]):({_type:i,validator:n,_message:m}=t,s=t._params?.concat([e]));function f(o,...p){let[x]=core.unwrapRuleParameters([e]);return x?n(o,...p):!0}function R(o){let[p]=core.unwrapRuleParameters([e]);return p}let r=core.createRule({type:i,validator:f,active:R,message:m}),l=[...s??[]];if(r._params=l,typeof r=="function"){let o=r(...l);return o._message_patched=!0,o}else return r}function u(e){return e==null?!0:e instanceof Date?isNaN(e.getTime()):Array.isArray(e)?!1:typeof e=="object"&&e!=null?Object.keys(e).length===0:!String(e).length}function a(e){return !u(typeof e=="string"?e.trim():e)}function y(e){return e==null||typeof e!="number"?!1:!isNaN(e)}function g(e,...t){if(u(e))return !0;let i=typeof e=="number"?e.toString():e;return t.every(n=>(n.lastIndex=0,n.test(i)))}function D(e){let t=vue.unref(e);return Array.isArray(t)?t.length:typeof t=="object"?Object.keys(t).length:typeof t=="number"?isNaN(t)?0:t:String(t).length}function T(e){if(u(e))return !1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e=="number"&&!isNaN(e))t=new Date(e);else if(typeof e=="string"){let i=new Date(e);if(i.toString()==="Invalid Date")return !1;t=i;}return !!t}catch{return !1}}function b(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e=="object"&&t==="[object Date]"?new Date(e.getTime()):typeof e=="number"||t==="[object Number]"?new Date(e):typeof e=="string"||t==="[object String]"?new Date(e):new Date(NaN)}function c(e){return typeof e=="number"?e:e!=null?typeof e=="string"?e.trim()!==e?NaN:+e:NaN:NaN}function U(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>{if(typeof r=="function")return null;{let l=r._params;return l?.length?l:[]}}).flat().filter(r=>!!r);function n(r,l,...o){let p=[],x=0;for(let d of r)if(typeof d=="function")p.push(d(l)),x++;else {let M=d._params?.length??0;p.push(d.validator(l,...o.slice(x,M))),M&&(x+=M);}return p}function s(r){return r?.some(o=>typeof o!="boolean")?{$valid:r.every(o=>typeof o=="boolean"?!!o:o.$valid),...r.reduce((o,p)=>{if(typeof p=="boolean")return o;let{$valid:x,...d}=p;return {...o,...d}},{})}:r.every(o=>!!o)}let m;e.length?m=t?async(r,...l)=>{let o=await Promise.all(n(e,r,...l));return s(o)}:(r,...l)=>{let o=n(e,r,...l);return s(o)}:m=r=>!1;let f=core.createRule({type:"and",validator:m,message:"The value does not match all of the provided validators"}),R=[...i??[]];if(f._params=R,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function q(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>typeof r=="function"?null:r._params).flat().filter(r=>!!r);function n(r,l,...o){let p=[],x=0;for(let d of r)if(typeof d=="function")p.push(d(l)),x++;else {let M=d._params?.length??0;p.push(d.validator(l,...o.slice(x,M))),M&&(x+=M);}return p}function s(r){return r.some(o=>typeof o!="boolean")?{$valid:r.some(o=>typeof o=="boolean"?!!o:o.$valid),...r.reduce((o,p)=>{if(typeof p=="boolean")return o;let{$valid:x,...d}=p;return {...o,...d}},{})}:r.some(o=>!!o)}let f=core.createRule({type:"or",validator:t?async(r,...l)=>{let o=await Promise.all(n(e,r,...l));return s(o)}:(r,...l)=>{let o=n(e,r,...l);return s(o)},message:"The value does not match any of the provided validators"}),R=[...i??[]];if(f._params=R,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function j(e,t){let i,n,s,m,f;typeof e=="function"?(n=e,f=e.constructor.name==="AsyncFunction"):({_type:i,validator:n,_params:m}=e,f=e._async),f?s=async(l,...o)=>a(l)?!await n(l,...o):!0:s=(l,...o)=>a(l)?!n(l,...o):!0;let R=core.createRule({type:"not",validator:s,message:t??"Error"}),r=[...m??[]];if(R._params=r,typeof R=="function"){let l=R(...r);return l._message_patched=!0,l}else return R}var qt=core.createRule({type:"required",validator:e=>a(e),message:"This field is required"});var Zt=core.createRule({type:"maxLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)<=t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The maximum length allowed is ${t}`});var Xt=core.createRule({type:"required",validator(e,t){return t?a(e):!0},message:"This field is required",active({$params:[e]}){return e}});var Q=/^[a-zA-Z]*$/,rr=core.createRule({type:"alpha",validator(e){return u(e)?!0:g(e,Q)},message:"The value is not alphabetical"});var Y=/^[a-zA-Z0-9]*$/,or=core.createRule({type:"alpha",validator(e){return u(e)?!0:g(e,Y)},message:"The value must be alpha-numeric"});var mr=core.createRule({type:"between",validator:(e,t,i)=>{if(a(e)&&a(t)&&a(i)){let n=c(e),s=c(t),m=c(i);return y(n)&&y(s)&&y(m)?n>=s&&n<=m:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${i}`),!1)}return !0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`});var re=/^[-]?\d*(\.\d+)?$/,yr=core.createRule({type:"decimal",validator(e){return u(e)?!0:g(e,re)},message:"Value must be decimal"});var ne=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,br=core.createRule({type:"email",validator(e){return u(e)?!0:g(e,ne)},message:"Value must be an valid email address"});var oe=/(^[0-9]*$)|(^-[0-9]+$)/,Mr=core.createRule({type:"integer",validator(e){return u(e)?!0:g(e,oe)},message:"Value must be an integer"});var $r=core.createRule({type:"maxValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)<=t:(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The maximum value allowed is ${e}`});var Wr=core.createRule({type:"minLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)>=t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have at least ${t} items`:`This field should be at least ${t} characters long`});var Ir=core.createRule({type:"minValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)>=t:(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The minimum value allowed is ${e}`});var Sr=core.createRule({type:"exactValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`});var Ur=core.createRule({type:"exactLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`This field should be exactly ${e} characters long`});var pe=/^\d*(\.\d+)?$/,jr=core.createRule({type:"numeric",validator(e){return u(e)?!0:g(e,pe)},message:"This field must be numeric"});var Jr=core.createRule({type:"required",validator(e,t){return t?!0:a(e)},message:"This field is required",active({$params:[e]}){return !e}});var ea=core.createRule({type:"sameAs",validator(e,t,i="other"){return u(e)?!0:e===t},message({$params:[e,t]}){return `Value must be equal to the ${t} value`}});var de=/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,na=core.createRule({type:"url",validator(e){return u(e)?!0:g(e,de)},message:"The value is not a valid URL address"});function be(){return navigator.languages!=null?navigator.languages[0]:navigator.language}function h(e){return e?new Intl.DateTimeFormat(be(),{dateStyle:"short"}).format(new Date(e)):"?"}var ma=core.createRule({type:"dateAfter",validator:(e,t)=>a(e)&&a(t)?T(e)&&T(t)?b(e).getTime()>b(t).getTime()?!0:{$valid:!1,error:"date-not-after"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The inputs must be Dates":`The date must be after ${h(e)}`});var ga=core.createRule({type:"dateBefore",validator:(e,t)=>a(e)&&a(t)?T(e)&&T(t)?b(e).getTime()<b(t).getTime()?!0:{$valid:!1,error:"date-not-before"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The fields must be Dates":`The date must be before ${h(e)}`});var xa=core.createRule({type:"dateBetween",validator:(e,t,i)=>T(e)&&T(t)&&T(i)?b(e).getTime()>b(t).getTime()&&b(e).getTime()<b(i).getTime():!0,message:({$params:[e,t]})=>`The date must be between ${h(e)} and ${h(t)}`});function he(e){if(e.length>3||e.length===0||e[0]==="0"&&e!=="0"||!e.match(/^\d+$/))return !1;let t=+e|0;return t>=0&&t<=255}var Pa=core.createRule({type:"ipAddress",validator(e){if(u(e))return !0;if(typeof e!="string")return !1;let t=e.split(".");return t.length===4&&t.every(he)},message:"The value is not a valid IP address"});var Aa=core.createRule({type:"macAddress",validator(e,t=":"){if(u(e))return !0;if(typeof e!="string")return !1;let i=typeof t=="string"&&t!==""?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return i!==null&&(i.length===6||i.length===8)&&i.every(we)},message:"The value is not a valid MAC Address"}),we=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/);var _a=core.createRule({type:"checked",validator:e=>a(e)?e===!0:!0,message:"This field must be checked"});var Ea=core.createRule({type:"contains",validator(e,t){return a(e)&&a(t)?e.includes(t):!0},message({$params:[e]}){return `Field must contain ${e}`}});var Ca=core.createRule({type:"startsWith",validator(e,t){return a(e)&&a(t)?e.startsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var qa=core.createRule({type:"endsWith",validator(e,t){return a(e)&&a(t)?e.endsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var Za=core.createRule({type:"regex",validator(e,...t){return a(e)?g(e,...t):!0},message:"This field does not match the required pattern"});function Xa(e){let t=vue.computed(()=>vue.toValue(e));return P(w((n,s)=>a(n)&&a(s)?s.includes(n):!0,[t]),({$params:[n]})=>`Value should be one of those options: ${n.join(", ")}.`)}function ke(e){let t=Object.keys(e).filter(n=>typeof e[e[n]]!="number"),i={};for(let n of t)i[n]=e[n];return Object.values(i)}function rn(e){let t=vue.computed(()=>vue.toValue(e));return P(w((n,s)=>a(n)&&!u(s)?ke(s).includes(n):!0,[t]),({$params:[n]})=>`Value should be one of those options: ${Object.values(n).join(", ")}.`)}exports.alpha=rr;exports.alphaNum=or;exports.and=U;exports.applyIf=L;exports.between=mr;exports.checked=_a;exports.contains=Ea;exports.dateAfter=ma;exports.dateBefore=ga;exports.dateBetween=xa;exports.decimal=yr;exports.email=br;exports.endsWith=qa;exports.exactLength=Ur;exports.exactValue=Sr;exports.getSize=D;exports.integer=Mr;exports.ipAddress=Pa;exports.isDate=T;exports.isEmpty=u;exports.isFilled=a;exports.isNumber=y;exports.macAddress=Aa;exports.matchRegex=g;exports.maxLength=Zt;exports.maxValue=$r;exports.minLength=Wr;exports.minValue=Ir;exports.nativeEnum=rn;exports.not=j;exports.numeric=jr;exports.oneOf=Xa;exports.or=q;exports.regex=Za;exports.required=qt;exports.requiredIf=Xt;exports.requiredUnless=Jr;exports.sameAs=ea;exports.startsWith=Ca;exports.toDate=b;exports.toNumber=c;exports.url=na;exports.withAsync=I;exports.withMessage=P;exports.withParams=w;exports.withTooltip=F;
|
package/dist/regle-rules.min.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {createRule,InternalRuleType,unwrapRuleParameters}from'@regle/core';import {unref}from'vue';function V(e,t){let i,o,u,f;typeof e=="function"&&!("_validator"in e)?(i=InternalRuleType.Inline,o=e):{_type:i,validator:o,_active:u,_params:f}=e;let m=createRule({type:i,validator:o,active:u,message:t}),R=[...f??[]];if(m._params=R,m._message_patched=!0,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function v(e,t){let i,o,u,f,m;typeof e=="function"&&!("_validator"in e)?(i=InternalRuleType.Inline,o=e):{_type:i,validator:o,_active:u,_params:f,_message:m}=e;let R=createRule({type:i,validator:o,active:u,message:m,tooltip:t}),r=[...f??[]];if(R._params=r,R._tooltip_patched=!0,typeof R=="function"){let s=R(...r);return R._tooltip_patched=!0,s}else return R}function N(e,t){let i=async(u,...f)=>e(u,...f),o=createRule({type:InternalRuleType.Async,validator:i,message:""});return o._params=o._params?.concat(t),o(...t??[])}function k(e,t){let i=(u,...f)=>e(u,...f),o=createRule({type:InternalRuleType.Inline,validator:i,message:""});return o._params=o._params?.concat(t),o(...t)}function C(e,t){let i,o,u=[],f="";typeof t=="function"?(i=InternalRuleType.Inline,o=t,u=[e]):({_type:i,validator:o,_message:f}=t,u=t._params?.concat([e]));function m(n,...p){let[b]=unwrapRuleParameters([e]);return b?o(n,...p):!0}function R(n){let[p]=unwrapRuleParameters([e]);return p}let r=createRule({type:i,validator:m,active:R,message:f}),s=[...u??[]];if(r._params=s,typeof r=="function"){let n=r(...s);return n._message_patched=!0,n}else return r}function l(e){return e==null?!0:e instanceof Date?isNaN(e.getTime()):Array.isArray(e)?!1:typeof e=="object"&&e!=null?Object.keys(e).length===0:!String(e).length}function a(e){return !l(typeof e=="string"?e.trim():e)}function y(e){return e==null||typeof e!="number"?!1:!isNaN(e)}function g(e,...t){if(l(e))return !0;let i=typeof e=="number"?e.toString():e;return t.every(o=>(o.lastIndex=0,o.test(i)))}function D(e){let t=unref(e);return Array.isArray(t)?t.length:typeof t=="object"?Object.keys(t).length:typeof t=="number"?isNaN(t)?0:t:String(t).length}function x(e){if(l(e))return !1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e=="number"&&!isNaN(e))t=new Date(e);else if(typeof e=="string"){let i=new Date(e);if(i.toString()==="Invalid Date")return !1;t=i;}return !!t}catch{return !1}}function T(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e=="object"&&t==="[object Date]"?new Date(e.getTime()):typeof e=="number"||t==="[object Number]"?new Date(e):typeof e=="string"||t==="[object String]"?new Date(e):new Date(NaN)}function d(e){return typeof e=="number"?e:e!=null?typeof e=="string"?e.trim()!==e?NaN:+e:NaN:NaN}function q(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>{if(typeof r=="function")return null;{let s=r._params;return s?.length?s:[]}}).flat().filter(r=>!!r);function o(r,s,...n){let p=[],b=0;for(let c of r)if(typeof c=="function")p.push(c(s)),b++;else {let M=c._params?.length??0;p.push(c.validator(s,...n.slice(b,M))),M&&(b+=M);}return p}function u(r){return r?.some(n=>typeof n!="boolean")?{$valid:r.every(n=>typeof n=="boolean"?!!n:n.$valid),...r.reduce((n,p)=>{if(typeof p=="boolean")return n;let{$valid:b,...c}=p;return {...n,...c}},{})}:r.every(n=>!!n)}let f;e.length?f=t?async(r,...s)=>{let n=await Promise.all(o(e,r,...s));return u(n)}:(r,...s)=>{let n=o(e,r,...s);return u(n)}:f=r=>!1;let m=createRule({type:"and",validator:f,message:"The value does not match all of the provided validators"}),R=[...i??[]];if(m._params=R,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function O(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>typeof r=="function"?null:r._params).flat().filter(r=>!!r);function o(r,s,...n){let p=[],b=0;for(let c of r)if(typeof c=="function")p.push(c(s)),b++;else {let M=c._params?.length??0;p.push(c.validator(s,...n.slice(b,M))),M&&(b+=M);}return p}function u(r){return r.some(n=>typeof n!="boolean")?{$valid:r.some(n=>typeof n=="boolean"?!!n:n.$valid),...r.reduce((n,p)=>{if(typeof p=="boolean")return n;let{$valid:b,...c}=p;return {...n,...c}},{})}:r.some(n=>!!n)}let m=createRule({type:"or",validator:t?async(r,...s)=>{let n=await Promise.all(o(e,r,...s));return u(n)}:(r,...s)=>{let n=o(e,r,...s);return u(n)},message:"The value does not match any of the provided validators"}),R=[...i??[]];if(m._params=R,typeof m=="function"){let r=m(...R);return r._message_patched=!0,r}else return m}function j(e,t){let i,o,u,f,m;typeof e=="function"?(o=e,m=e.constructor.name==="AsyncFunction"):({_type:i,validator:o,_params:f}=e,m=e._async),m?u=async(s,...n)=>a(s)?!await o(s,...n):!0:u=(s,...n)=>a(s)?!o(s,...n):!0;let R=createRule({type:"not",validator:u,message:t??"Error"}),r=[...f??[]];if(R._params=r,typeof R=="function"){let s=R(...r);return s._message_patched=!0,s}else return R}var zt=createRule({type:"required",validator:e=>a(e),message:"This field is required"});var Ut=createRule({type:"maxLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)<=t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The maximum length allowed is ${t}`});var Kt=createRule({type:"required",validator(e,t){return t?a(e):!0},message:"This field is required",active({$params:[e]}){return e}});var Q=/^[a-zA-Z]*$/,Ht=createRule({type:"alpha",validator(e){return l(e)?!0:g(e,Q)},message:"The value is not alphabetical"});var Y=/^[a-zA-Z0-9]*$/,Yt=createRule({type:"alpha",validator(e){return l(e)?!0:g(e,Y)},message:"The value must be alpha-numeric"});var ar=createRule({type:"between",validator:(e,t,i)=>{if(a(e)&&a(t)&&a(i)){let o=d(e),u=d(t),f=d(i);return y(o)&&y(u)&&y(f)?o>=u&&o<=f:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${i}`),!1)}return !0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`});var re=/^[-]?\d*(\.\d+)?$/,sr=createRule({type:"decimal",validator(e){return l(e)?!0:g(e,re)},message:"Value must be decimal"});var ne=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,fr=createRule({type:"email",validator(e){return l(e)?!0:g(e,ne)},message:"Value must be an valid email address"});var oe=/(^[0-9]*$)|(^-[0-9]+$)/,gr=createRule({type:"integer",validator(e){return l(e)?!0:g(e,oe)},message:"Value must be an integer"});var xr=createRule({type:"maxValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)<=t:(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The maximum value allowed is ${e}`});var hr=createRule({type:"minLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)>=t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have at least ${t} items`:`This field should be at least ${t} characters long`});var Vr=createRule({type:"minValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)>=t:(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The minimum value allowed is ${e}`});var _r=createRule({type:"exactValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(d(e))?d(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`});var Er=createRule({type:"exactLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`This field should be exactly ${e} characters long`});var pe=/^\d*(\.\d+)?$/,Cr=createRule({type:"numeric",validator(e){return l(e)?!0:g(e,pe)},message:"This field must be numeric"});var Gr=createRule({type:"required",validator(e,t){return t?!0:a(e)},message:"This field is required",active({$params:[e]}){return !e}});var Br=createRule({type:"sameAs",validator(e,t,i="other"){return l(e)?!0:e===t},message({$params:[e,t]}){return `Value must be equal to the ${t} value`}});var ce=/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,Qr=createRule({type:"url",validator(e){return l(e)?!0:g(e,ce)},message:"The value is not a valid URL address"});function Te(){return navigator.languages!=null?navigator.languages[0]:navigator.language}function h(e){return e?new Intl.DateTimeFormat(Te(),{dateStyle:"short"}).format(new Date(e)):"?"}var aa=createRule({type:"dateAfter",validator:(e,t)=>a(e)&&a(t)?x(e)&&x(t)?T(e).getTime()>T(t).getTime()?!0:{$valid:!1,error:"date-not-after"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The inputs must be Dates":`The date must be after ${h(e)}`});var la=createRule({type:"dateBefore",validator:(e,t)=>a(e)&&a(t)?x(e)&&x(t)?T(e).getTime()<T(t).getTime()?!0:{$valid:!1,error:"date-not-before"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The fields must be Dates":`The date must be before ${h(e)}`});var pa=createRule({type:"dateBetween",validator:(e,t,i)=>x(e)&&x(t)&&x(i)?T(e).getTime()>T(t).getTime()&&T(e).getTime()<T(i).getTime():!0,message:({$params:[e,t]})=>`The date must be between ${h(e)} and ${h(t)}`});function he(e){if(e.length>3||e.length===0||e[0]==="0"&&e!=="0"||!e.match(/^\d+$/))return !1;let t=+e|0;return t>=0&&t<=255}var ca=createRule({type:"ipAddress",validator(e){if(l(e))return !0;if(typeof e!="string")return !1;let t=e.split(".");return t.length===4&&t.every(he)},message:"The value is not a valid IP address"});var Da=createRule({type:"macAddress",validator(e,t=":"){if(l(e))return !0;if(typeof e!="string")return !1;let i=typeof t=="string"&&t!==""?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return i!==null&&(i.length===6||i.length===8)&&i.every(we)},message:"The value is not a valid MAC Address"}),we=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/);var wa=createRule({type:"checked",validator:e=>a(e)?e===!0:!0,message:"This field must be checked"});var Wa=createRule({type:"contains",validator(e,t){return a(e)&&a(t)?e.includes(t):!0},message({$params:[e]}){return `Field must contain ${e}`}});var Na=createRule({type:"startsWith",validator(e,t){return a(e)&&a(t)?e.startsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var za=createRule({type:"endsWith",validator(e,t){return a(e)&&a(t)?e.endsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var Ua=createRule({type:"regex",validator(e,...t){return a(e)?g(e,...t):!0},message:"This field does not match the required pattern"});export{Ht as alpha,Yt as alphaNum,q as and,C as applyIf,ar as between,wa as checked,Wa as contains,aa as dateAfter,la as dateBefore,pa as dateBetween,sr as decimal,fr as email,za as endsWith,Er as exactLength,_r as exactValue,D as getSize,gr as integer,ca as ipAddress,x as isDate,l as isEmpty,a as isFilled,y as isNumber,Da as macAddress,g as matchRegex,Ut as maxLength,xr as maxValue,hr as minLength,Vr as minValue,j as not,Cr as numeric,O as or,Ua as regex,zt as required,Kt as requiredIf,Gr as requiredUnless,Br as sameAs,Na as startsWith,T as toDate,d as toNumber,Qr as url,N as withAsync,V as withMessage,k as withParams,v as withTooltip};
|
|
1
|
+
import {createRule,InternalRuleType,unwrapRuleParameters}from'@regle/core';import {unref,computed,toValue}from'vue';function P(e,t){let i,n,s,m;typeof e=="function"&&!("_validator"in e)?(i=InternalRuleType.Inline,n=e):{_type:i,validator:n,_active:s,_params:m}=e;let f=createRule({type:i,validator:n,active:s,message:t}),R=[...m??[]];if(f._params=R,f._message_patched=!0,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function F(e,t){let i,n,s,m,f;typeof e=="function"&&!("_validator"in e)?(i=InternalRuleType.Inline,n=e):{_type:i,validator:n,_active:s,_params:m,_message:f}=e;let R=createRule({type:i,validator:n,active:s,message:f,tooltip:t}),r=[...m??[]];if(R._params=r,R._tooltip_patched=!0,typeof R=="function"){let l=R(...r);return R._tooltip_patched=!0,l}else return R}function I(e,t){let i=async(s,...m)=>e(s,...m),n=createRule({type:InternalRuleType.Async,validator:i,message:""});return n._params=n._params?.concat(t),n(...t??[])}function w(e,t){let i=(s,...m)=>e(s,...m),n=createRule({type:InternalRuleType.Inline,validator:i,message:""});return n._params=n._params?.concat(t),n(...t)}function L(e,t){let i,n,s=[],m="";typeof t=="function"?(i=InternalRuleType.Inline,n=t,s=[e]):({_type:i,validator:n,_message:m}=t,s=t._params?.concat([e]));function f(o,...p){let[x]=unwrapRuleParameters([e]);return x?n(o,...p):!0}function R(o){let[p]=unwrapRuleParameters([e]);return p}let r=createRule({type:i,validator:f,active:R,message:m}),l=[...s??[]];if(r._params=l,typeof r=="function"){let o=r(...l);return o._message_patched=!0,o}else return r}function u(e){return e==null?!0:e instanceof Date?isNaN(e.getTime()):Array.isArray(e)?!1:typeof e=="object"&&e!=null?Object.keys(e).length===0:!String(e).length}function a(e){return !u(typeof e=="string"?e.trim():e)}function y(e){return e==null||typeof e!="number"?!1:!isNaN(e)}function g(e,...t){if(u(e))return !0;let i=typeof e=="number"?e.toString():e;return t.every(n=>(n.lastIndex=0,n.test(i)))}function D(e){let t=unref(e);return Array.isArray(t)?t.length:typeof t=="object"?Object.keys(t).length:typeof t=="number"?isNaN(t)?0:t:String(t).length}function T(e){if(u(e))return !1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e=="number"&&!isNaN(e))t=new Date(e);else if(typeof e=="string"){let i=new Date(e);if(i.toString()==="Invalid Date")return !1;t=i;}return !!t}catch{return !1}}function b(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e=="object"&&t==="[object Date]"?new Date(e.getTime()):typeof e=="number"||t==="[object Number]"?new Date(e):typeof e=="string"||t==="[object String]"?new Date(e):new Date(NaN)}function c(e){return typeof e=="number"?e:e!=null?typeof e=="string"?e.trim()!==e?NaN:+e:NaN:NaN}function U(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>{if(typeof r=="function")return null;{let l=r._params;return l?.length?l:[]}}).flat().filter(r=>!!r);function n(r,l,...o){let p=[],x=0;for(let d of r)if(typeof d=="function")p.push(d(l)),x++;else {let M=d._params?.length??0;p.push(d.validator(l,...o.slice(x,M))),M&&(x+=M);}return p}function s(r){return r?.some(o=>typeof o!="boolean")?{$valid:r.every(o=>typeof o=="boolean"?!!o:o.$valid),...r.reduce((o,p)=>{if(typeof p=="boolean")return o;let{$valid:x,...d}=p;return {...o,...d}},{})}:r.every(o=>!!o)}let m;e.length?m=t?async(r,...l)=>{let o=await Promise.all(n(e,r,...l));return s(o)}:(r,...l)=>{let o=n(e,r,...l);return s(o)}:m=r=>!1;let f=createRule({type:"and",validator:m,message:"The value does not match all of the provided validators"}),R=[...i??[]];if(f._params=R,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function q(...e){let t=e.some(r=>typeof r=="function"?r.constructor.name==="AsyncFunction":r._async),i=e.map(r=>typeof r=="function"?null:r._params).flat().filter(r=>!!r);function n(r,l,...o){let p=[],x=0;for(let d of r)if(typeof d=="function")p.push(d(l)),x++;else {let M=d._params?.length??0;p.push(d.validator(l,...o.slice(x,M))),M&&(x+=M);}return p}function s(r){return r.some(o=>typeof o!="boolean")?{$valid:r.some(o=>typeof o=="boolean"?!!o:o.$valid),...r.reduce((o,p)=>{if(typeof p=="boolean")return o;let{$valid:x,...d}=p;return {...o,...d}},{})}:r.some(o=>!!o)}let f=createRule({type:"or",validator:t?async(r,...l)=>{let o=await Promise.all(n(e,r,...l));return s(o)}:(r,...l)=>{let o=n(e,r,...l);return s(o)},message:"The value does not match any of the provided validators"}),R=[...i??[]];if(f._params=R,typeof f=="function"){let r=f(...R);return r._message_patched=!0,r}else return f}function j(e,t){let i,n,s,m,f;typeof e=="function"?(n=e,f=e.constructor.name==="AsyncFunction"):({_type:i,validator:n,_params:m}=e,f=e._async),f?s=async(l,...o)=>a(l)?!await n(l,...o):!0:s=(l,...o)=>a(l)?!n(l,...o):!0;let R=createRule({type:"not",validator:s,message:t??"Error"}),r=[...m??[]];if(R._params=r,typeof R=="function"){let l=R(...r);return l._message_patched=!0,l}else return R}var qt=createRule({type:"required",validator:e=>a(e),message:"This field is required"});var Zt=createRule({type:"maxLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)<=t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The maximum length allowed is ${t}`});var Xt=createRule({type:"required",validator(e,t){return t?a(e):!0},message:"This field is required",active({$params:[e]}){return e}});var Q=/^[a-zA-Z]*$/,rr=createRule({type:"alpha",validator(e){return u(e)?!0:g(e,Q)},message:"The value is not alphabetical"});var Y=/^[a-zA-Z0-9]*$/,or=createRule({type:"alpha",validator(e){return u(e)?!0:g(e,Y)},message:"The value must be alpha-numeric"});var mr=createRule({type:"between",validator:(e,t,i)=>{if(a(e)&&a(t)&&a(i)){let n=c(e),s=c(t),m=c(i);return y(n)&&y(s)&&y(m)?n>=s&&n<=m:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${i}`),!1)}return !0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`});var re=/^[-]?\d*(\.\d+)?$/,yr=createRule({type:"decimal",validator(e){return u(e)?!0:g(e,re)},message:"Value must be decimal"});var ne=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,br=createRule({type:"email",validator(e){return u(e)?!0:g(e,ne)},message:"Value must be an valid email address"});var oe=/(^[0-9]*$)|(^-[0-9]+$)/,Mr=createRule({type:"integer",validator(e){return u(e)?!0:g(e,oe)},message:"Value must be an integer"});var $r=createRule({type:"maxValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)<=t:(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The maximum value allowed is ${e}`});var Wr=createRule({type:"minLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)>=t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have at least ${t} items`:`This field should be at least ${t} characters long`});var Ir=createRule({type:"minValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)>=t:(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The minimum value allowed is ${e}`});var Sr=createRule({type:"exactValue",validator:(e,t)=>a(e)&&a(t)?y(t)&&!isNaN(c(e))?c(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`});var Ur=createRule({type:"exactLength",validator:(e,t)=>a(e)&&a(t)?y(t)?D(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`This field should be exactly ${e} characters long`});var pe=/^\d*(\.\d+)?$/,jr=createRule({type:"numeric",validator(e){return u(e)?!0:g(e,pe)},message:"This field must be numeric"});var Jr=createRule({type:"required",validator(e,t){return t?!0:a(e)},message:"This field is required",active({$params:[e]}){return !e}});var ea=createRule({type:"sameAs",validator(e,t,i="other"){return u(e)?!0:e===t},message({$params:[e,t]}){return `Value must be equal to the ${t} value`}});var de=/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,na=createRule({type:"url",validator(e){return u(e)?!0:g(e,de)},message:"The value is not a valid URL address"});function be(){return navigator.languages!=null?navigator.languages[0]:navigator.language}function h(e){return e?new Intl.DateTimeFormat(be(),{dateStyle:"short"}).format(new Date(e)):"?"}var ma=createRule({type:"dateAfter",validator:(e,t)=>a(e)&&a(t)?T(e)&&T(t)?b(e).getTime()>b(t).getTime()?!0:{$valid:!1,error:"date-not-after"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The inputs must be Dates":`The date must be after ${h(e)}`});var ga=createRule({type:"dateBefore",validator:(e,t)=>a(e)&&a(t)?T(e)&&T(t)?b(e).getTime()<b(t).getTime()?!0:{$valid:!1,error:"date-not-before"}:{$valid:!1,error:"value-or-paramater-not-a-date"}:!0,message:({$params:[e],error:t})=>t==="value-or-paramater-not-a-date"?"The fields must be Dates":`The date must be before ${h(e)}`});var xa=createRule({type:"dateBetween",validator:(e,t,i)=>T(e)&&T(t)&&T(i)?b(e).getTime()>b(t).getTime()&&b(e).getTime()<b(i).getTime():!0,message:({$params:[e,t]})=>`The date must be between ${h(e)} and ${h(t)}`});function he(e){if(e.length>3||e.length===0||e[0]==="0"&&e!=="0"||!e.match(/^\d+$/))return !1;let t=+e|0;return t>=0&&t<=255}var Pa=createRule({type:"ipAddress",validator(e){if(u(e))return !0;if(typeof e!="string")return !1;let t=e.split(".");return t.length===4&&t.every(he)},message:"The value is not a valid IP address"});var Aa=createRule({type:"macAddress",validator(e,t=":"){if(u(e))return !0;if(typeof e!="string")return !1;let i=typeof t=="string"&&t!==""?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return i!==null&&(i.length===6||i.length===8)&&i.every(we)},message:"The value is not a valid MAC Address"}),we=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/);var _a=createRule({type:"checked",validator:e=>a(e)?e===!0:!0,message:"This field must be checked"});var Ea=createRule({type:"contains",validator(e,t){return a(e)&&a(t)?e.includes(t):!0},message({$params:[e]}){return `Field must contain ${e}`}});var Ca=createRule({type:"startsWith",validator(e,t){return a(e)&&a(t)?e.startsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var qa=createRule({type:"endsWith",validator(e,t){return a(e)&&a(t)?e.endsWith(t):!0},message({$params:[e]}){return `Field must end with ${e}`}});var Za=createRule({type:"regex",validator(e,...t){return a(e)?g(e,...t):!0},message:"This field does not match the required pattern"});function Xa(e){let t=computed(()=>toValue(e));return P(w((n,s)=>a(n)&&a(s)?s.includes(n):!0,[t]),({$params:[n]})=>`Value should be one of those options: ${n.join(", ")}.`)}function ke(e){let t=Object.keys(e).filter(n=>typeof e[e[n]]!="number"),i={};for(let n of t)i[n]=e[n];return Object.values(i)}function rn(e){let t=computed(()=>toValue(e));return P(w((n,s)=>a(n)&&!u(s)?ke(s).includes(n):!0,[t]),({$params:[n]})=>`Value should be one of those options: ${Object.values(n).join(", ")}.`)}export{rr as alpha,or as alphaNum,U as and,L as applyIf,mr as between,_a as checked,Ea as contains,ma as dateAfter,ga as dateBefore,xa as dateBetween,yr as decimal,br as email,qa as endsWith,Ur as exactLength,Sr as exactValue,D as getSize,Mr as integer,Pa as ipAddress,T as isDate,u as isEmpty,a as isFilled,y as isNumber,Aa as macAddress,g as matchRegex,Zt as maxLength,$r as maxValue,Wr as minLength,Ir as minValue,rn as nativeEnum,j as not,jr as numeric,Xa as oneOf,q as or,Za as regex,qt as required,Xt as requiredIf,Jr as requiredUnless,ea as sameAs,Ca as startsWith,b as toDate,c as toNumber,na as url,I as withAsync,P as withMessage,w as withParams,F as withTooltip};
|
package/dist/regle-rules.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRule, InternalRuleType, unwrapRuleParameters } from '@regle/core';
|
|
2
|
-
import { unref } from 'vue';
|
|
2
|
+
import { unref, computed, toValue } from 'vue';
|
|
3
3
|
|
|
4
4
|
// src/helpers/withMessage.ts
|
|
5
5
|
function withMessage(rule, newMessage) {
|
|
@@ -860,5 +860,46 @@ var regex = createRule({
|
|
|
860
860
|
},
|
|
861
861
|
message: "This field does not match the required pattern"
|
|
862
862
|
});
|
|
863
|
+
function oneOf(options) {
|
|
864
|
+
const params = computed(() => toValue(options));
|
|
865
|
+
const rule = withMessage(
|
|
866
|
+
withParams(
|
|
867
|
+
(value, options2) => {
|
|
868
|
+
if (isFilled(value) && isFilled(options2)) {
|
|
869
|
+
return options2.includes(value);
|
|
870
|
+
}
|
|
871
|
+
return true;
|
|
872
|
+
},
|
|
873
|
+
[params]
|
|
874
|
+
),
|
|
875
|
+
({ $params: [options2] }) => `Value should be one of those options: ${options2.join(", ")}.`
|
|
876
|
+
);
|
|
877
|
+
return rule;
|
|
878
|
+
}
|
|
879
|
+
function getValidEnumValues(obj) {
|
|
880
|
+
const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
881
|
+
const filtered = {};
|
|
882
|
+
for (const k of validKeys) {
|
|
883
|
+
filtered[k] = obj[k];
|
|
884
|
+
}
|
|
885
|
+
return Object.values(filtered);
|
|
886
|
+
}
|
|
887
|
+
function nativeEnum(enumLike) {
|
|
888
|
+
const params = computed(() => toValue(enumLike));
|
|
889
|
+
const rule = withMessage(
|
|
890
|
+
withParams(
|
|
891
|
+
(value, enumLike2) => {
|
|
892
|
+
if (isFilled(value) && !isEmpty(enumLike2)) {
|
|
893
|
+
const validValues = getValidEnumValues(enumLike2);
|
|
894
|
+
return validValues.includes(value);
|
|
895
|
+
}
|
|
896
|
+
return true;
|
|
897
|
+
},
|
|
898
|
+
[params]
|
|
899
|
+
),
|
|
900
|
+
({ $params: [enumLike2] }) => `Value should be one of those options: ${Object.values(enumLike2).join(", ")}.`
|
|
901
|
+
);
|
|
902
|
+
return rule;
|
|
903
|
+
}
|
|
863
904
|
|
|
864
|
-
export { alpha, alphaNum, and, applyIf, between, checked, contains, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, integer, ipAddress, isDate, isEmpty, isFilled, isNumber, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, not, numeric, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, toDate, toNumber, url, withAsync, withMessage, withParams, withTooltip };
|
|
905
|
+
export { alpha, alphaNum, and, applyIf, between, checked, contains, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, integer, ipAddress, isDate, isEmpty, isFilled, isNumber, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, toDate, toNumber, url, withAsync, withMessage, withParams, withTooltip };
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/rules",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.11",
|
|
4
4
|
"description": "Collection of rules and helpers for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@regle/core": "0.5.
|
|
6
|
+
"@regle/core": "0.5.11"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@typescript-eslint/eslint-plugin": "8.19.
|
|
10
|
-
"@typescript-eslint/parser": "8.19.
|
|
9
|
+
"@typescript-eslint/eslint-plugin": "8.19.1",
|
|
10
|
+
"@typescript-eslint/parser": "8.19.1",
|
|
11
11
|
"@vue/reactivity": "3.5.13",
|
|
12
12
|
"@vue/test-utils": "2.4.6",
|
|
13
|
-
"bumpp": "9.
|
|
13
|
+
"bumpp": "9.10.0",
|
|
14
14
|
"changelogithub": "0.13.11",
|
|
15
15
|
"cross-env": "7.0.3",
|
|
16
16
|
"eslint": "9.15.0",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"eslint-plugin-vue": "9.31.0",
|
|
19
19
|
"prettier": "3.3.3",
|
|
20
20
|
"tsup": "8.3.5",
|
|
21
|
-
"type-fest": "4.
|
|
21
|
+
"type-fest": "4.32.0",
|
|
22
22
|
"typescript": "5.6.3",
|
|
23
23
|
"vitest": "2.1.8",
|
|
24
24
|
"vue": "3.5.13",
|