@regle/rules 1.11.0 → 1.12.0
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/dist/regle-rules.d.ts +1 -1
- package/dist/regle-rules.js +9 -2
- package/dist/regle-rules.min.js +1 -1
- package/package.json +4 -4
package/dist/regle-rules.d.ts
CHANGED
|
@@ -170,7 +170,7 @@ declare function not<TValue, TParams extends any[] = [], TReturn extends RegleRu
|
|
|
170
170
|
* })
|
|
171
171
|
* ```
|
|
172
172
|
*/
|
|
173
|
-
declare function assignIf<TValue extends unknown = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>, TRulesDelc extends RegleRuleDecl<TValue, TCustomRules> = RegleRuleDecl<TValue, TCustomRules>>(_condition: MaybeRefOrGetter<Maybe<boolean>>, rules: TRulesDelc): ComputedRef<TRulesDelc>;
|
|
173
|
+
declare function assignIf<TValue extends unknown = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>, TRulesDelc extends RegleRuleDecl<TValue, TCustomRules> = RegleRuleDecl<TValue, TCustomRules>>(_condition: MaybeRefOrGetter<Maybe<boolean>>, rules: MaybeRefOrGetter<TRulesDelc>): ComputedRef<TRulesDelc>;
|
|
174
174
|
/**
|
|
175
175
|
* Allows only alphabetic characters.
|
|
176
176
|
*
|
package/dist/regle-rules.js
CHANGED
|
@@ -180,6 +180,11 @@ function isEmpty(value, considerEmptyArrayInvalid = true) {
|
|
|
180
180
|
return !String(value).length;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
function isObject(obj) {
|
|
184
|
+
if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) return false;
|
|
185
|
+
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
186
|
+
}
|
|
187
|
+
|
|
183
188
|
/**
|
|
184
189
|
* This is a useful helper that can check if the provided value is a Date, it is used internally for date rules. This can also check strings.
|
|
185
190
|
*/
|
|
@@ -450,8 +455,10 @@ function not(rule, message) {
|
|
|
450
455
|
*/
|
|
451
456
|
function assignIf(_condition, rules) {
|
|
452
457
|
return computed(() => {
|
|
453
|
-
|
|
454
|
-
|
|
458
|
+
return Object.fromEntries(Object.entries(toValue(rules)).map(([key, rule]) => {
|
|
459
|
+
if (typeof rule === "function" || isObject(rule) && "_validator" in rule) return [key, applyIf(_condition, rule)];
|
|
460
|
+
return [key, rule];
|
|
461
|
+
}));
|
|
455
462
|
});
|
|
456
463
|
}
|
|
457
464
|
|
package/dist/regle-rules.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s}=n;let c=t({type:i,validator:a,active:o,message:r}),l=[...s??[]];if(c._params=l,c._message_patched=!0,typeof c==`function`){if(s!=null){let e=c(...l);return e._message_patched=!0,e}return c}else return c}function s(n,r){let i,a,o,s,c;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c}=n;let l=t({type:i,validator:a,active:o,message:c,tooltip:r}),u=[...s??[]];if(l._params=u,l._tooltip_patched=!0,typeof l==`function`){let e=l(...u);return l._tooltip_patched=!0,e}else return l}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`||e?.constructor?.name==`FileList`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function p(e){if(f(e))return!1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}catch{return!1}}function m(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]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function h(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function g(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function _(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function v(e){let t=a(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 y(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function b(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function ee(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function x(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>h(e)?!await i(e,...t):!0:(e,...t)=>h(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function S(e,t){return r(()=>i(e)?t:{})}const C=/^[a-zA-Z]*$/,w=/^[\w.]+$/,T=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?_(e,w):_(e,C)},message:`The value is not alphabetical`}),E=/^[a-zA-Z0-9]*$/,D=/^[a-zA-Z0-9_]*$/,O=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?_(e,D):_(e,E)},message:`The value must be alpha-numeric`}),k=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(h(e)&&h(t)&&h(n)){let r=y(e),a=y(t),o=y(n);return g(r)&&g(a)&&g(o)?i?r>=a&&r<=o:r>a&&r<o:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${n}`),!1)}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),A=t({type:`boolean`,validator:e=>h(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),j=t({type:`checked`,validator:e=>h(e)?e===!0:!0,message:`The field must be checked`}),M=t({type:`contains`,validator(e,t){return h(e)&&h(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),N=t({type:`date`,validator:e=>h(e)?e instanceof Date:!0,message:`The value must be a native Date constructor`});function P(){return navigator.languages==null?navigator.language:navigator.languages[0]}function F(e){return e?new Intl.DateTimeFormat(P(),{dateStyle:`short`}).format(new Date(e)):`?`}const I=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e)&&h(t)?p(e)&&p(t)?(r?m(e).getTime()>=m(t).getTime():m(e).getTime()>m(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${F(e)}`}),L=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e)&&h(t)?p(e)&&p(t)?(r?m(e).getTime()<=m(t).getTime():m(e).getTime()<m(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${F(e)}`}),R=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return p(e)&&p(t)&&p(n)?i?m(e).getTime()>=m(t).getTime()&&m(e).getTime()<=m(n).getTime():m(e).getTime()>m(t).getTime()&&m(e).getTime()<m(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${F(e)} and ${F(t)}`}),z=/^[-]?\d*(\.\d+)?$/,B=t({type:`decimal`,validator(e){return f(e)?!0:_(e,z)},message:`The value must be decimal`}),V=/^(?:[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,H=t({type:`email`,validator(e){return f(e)?!0:_(e,V)},message:`The value must be an valid email address`}),U=t({type:`endsWith`,validator(e,t){return h(e)&&h(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),W=t({type:`exactLength`,validator:(e,t)=>h(e,!1)&&h(t)?g(t)?v(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),te=t({type:`exactValue`,validator:(e,t)=>h(e)&&h(t)?g(t)&&!isNaN(y(e))?y(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}`}),G=/^[a-fA-F0-9]*$/,K=t({type:`hexadecimal`,validator(e){return f(e)?!0:_(e,G)},message:`The value must be hexadecimal`}),q=/(^[0-9]*$)|(^-[0-9]+$)/,J=t({type:`integer`,validator(e){return f(e)?!0:_(e,q)},message:`The value must be an integer`});function Y(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}const X=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(Y)},message:`The value is not a valid IPv4 address`});function Z(e){return o(l((e,t)=>h(e)&&h(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const Q=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(ne)},message:`The value is not a valid MAC Address`}),ne=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),re=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e,!1)&&h(t)?g(t)?r?v(e)<=t:v(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 value length should not exceed ${t}`}),ie=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e)&&h(t)?!isNaN(y(e))&&!isNaN(y(t))?r?y(e)<=y(t):y(e)<y(t):(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),ae=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e,!1)&&h(t)?g(t)?r?v(e)>=t:v(e)>t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),oe=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return h(e)&&h(t)?!isNaN(y(e))&&!isNaN(y(t))?r?y(e)>=y(t):y(e)>y(t):(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function se(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function ce(e){return o(l((e,t)=>h(e)&&!f(t)?se(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const le=t({type:`number`,validator:e=>h(e)?g(e):!0,message:`The value must be a native number`}),ue=/^\d*(\.\d+)?$/,de=t({type:`numeric`,validator(e){return f(e)?!0:_(e,ue)},message:`The value must be numeric`}),fe=t({type:`oneOf`,validator(e,t){return h(e)&&h(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),pe=t({type:`regex`,validator(e,t){return h(e)?_(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),$=t({type:`required`,validator:e=>h(e),message:`This field is required`}),me=t({type:`required`,validator(e,t){return t?h(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),he=t({type:`required`,validator(e,t){return t?!0:h(e)},message:`This field is required`,active({$params:[e]}){return!e}}),ge=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),_e=t({type:`startsWith`,validator(e,t){return h(e)&&h(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),ve=t({type:`string`,validator:e=>h(e)?typeof e==`string`:!0,message:`The value must be a string`});function ye(){return(()=>!0)}const be=/^(?:(?:(?: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,xe=t({type:`url`,validator(e){return f(e)?!0:_(e,be)},message:`The value is not a valid URL address`});export{T as alpha,O as alphaNum,b as and,u as applyIf,S as assignIf,k as between,A as boolean,j as checked,M as contains,N as date,I as dateAfter,L as dateBefore,R as dateBetween,B as decimal,H as email,U as endsWith,W as exactLength,te as exactValue,v as getSize,K as hexadecimal,J as integer,X as ipv4Address,p as isDate,f as isEmpty,h as isFilled,g as isNumber,Z as literal,Q as macAddress,_ as matchRegex,re as maxLength,ie as maxValue,ae as minLength,oe as minValue,ce as nativeEnum,x as not,le as number,de as numeric,fe as oneOf,ee as or,pe as regex,$ as required,me as requiredIf,he as requiredUnless,ge as sameAs,_e as startsWith,ve as string,m as toDate,y as toNumber,ye as type,xe as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
|
1
|
+
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s}=n;let c=t({type:i,validator:a,active:o,message:r}),l=[...s??[]];if(c._params=l,c._message_patched=!0,typeof c==`function`){if(s!=null){let e=c(...l);return e._message_patched=!0,e}return c}else return c}function s(n,r){let i,a,o,s,c;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c}=n;let l=t({type:i,validator:a,active:o,message:c,tooltip:r}),u=[...s??[]];if(l._params=u,l._tooltip_patched=!0,typeof l==`function`){let e=l(...u);return l._tooltip_patched=!0,e}else return l}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`||e?.constructor?.name==`FileList`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){if(f(e))return!1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}catch{return!1}}function h(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]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function g(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function _(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function v(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function y(e){let t=a(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 b(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function ee(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function x(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function S(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>g(e)?!await i(e,...t):!0:(e,...t)=>g(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function C(e,t){return r(()=>Object.fromEntries(Object.entries(i(t)).map(([t,n])=>typeof n==`function`||p(n)&&`_validator`in n?[t,u(e,n)]:[t,n])))}const w=/^[a-zA-Z]*$/,T=/^[\w.]+$/,E=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,T):v(e,w)},message:`The value is not alphabetical`}),D=/^[a-zA-Z0-9]*$/,O=/^[a-zA-Z0-9_]*$/,k=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,O):v(e,D)},message:`The value must be alpha-numeric`}),A=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(g(e)&&g(t)&&g(n)){let r=b(e),a=b(t),o=b(n);return _(r)&&_(a)&&_(o)?i?r>=a&&r<=o:r>a&&r<o:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${n}`),!1)}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),j=t({type:`boolean`,validator:e=>g(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),M=t({type:`checked`,validator:e=>g(e)?e===!0:!0,message:`The field must be checked`}),N=t({type:`contains`,validator(e,t){return g(e)&&g(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),P=t({type:`date`,validator:e=>g(e)?e instanceof Date:!0,message:`The value must be a native Date constructor`});function F(){return navigator.languages==null?navigator.language:navigator.languages[0]}function I(e){return e?new Intl.DateTimeFormat(F(),{dateStyle:`short`}).format(new Date(e)):`?`}const L=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?m(e)&&m(t)?(r?h(e).getTime()>=h(t).getTime():h(e).getTime()>h(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${I(e)}`}),R=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?m(e)&&m(t)?(r?h(e).getTime()<=h(t).getTime():h(e).getTime()<h(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${I(e)}`}),z=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return m(e)&&m(t)&&m(n)?i?h(e).getTime()>=h(t).getTime()&&h(e).getTime()<=h(n).getTime():h(e).getTime()>h(t).getTime()&&h(e).getTime()<h(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${I(e)} and ${I(t)}`}),B=/^[-]?\d*(\.\d+)?$/,V=t({type:`decimal`,validator(e){return f(e)?!0:v(e,B)},message:`The value must be decimal`}),H=/^(?:[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,U=t({type:`email`,validator(e){return f(e)?!0:v(e,H)},message:`The value must be an valid email address`}),W=t({type:`endsWith`,validator(e,t){return g(e)&&g(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),te=t({type:`exactLength`,validator:(e,t)=>g(e,!1)&&g(t)?_(t)?y(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),G=t({type:`exactValue`,validator:(e,t)=>g(e)&&g(t)?_(t)&&!isNaN(b(e))?b(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}`}),K=/^[a-fA-F0-9]*$/,q=t({type:`hexadecimal`,validator(e){return f(e)?!0:v(e,K)},message:`The value must be hexadecimal`}),J=/(^[0-9]*$)|(^-[0-9]+$)/,Y=t({type:`integer`,validator(e){return f(e)?!0:v(e,J)},message:`The value must be an integer`});function X(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}const Z=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(X)},message:`The value is not a valid IPv4 address`});function Q(e){return o(l((e,t)=>g(e)&&g(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const ne=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(re)},message:`The value is not a valid MAC Address`}),re=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),ie=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)<=t:y(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 value length should not exceed ${t}`}),ae=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)<=b(t):b(e)<b(t):(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),oe=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)>=t:y(e)>t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),se=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)>=b(t):b(e)>b(t):(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function ce(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function le(e){return o(l((e,t)=>g(e)&&!f(t)?ce(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const ue=t({type:`number`,validator:e=>g(e)?_(e):!0,message:`The value must be a native number`}),de=/^\d*(\.\d+)?$/,fe=t({type:`numeric`,validator(e){return f(e)?!0:v(e,de)},message:`The value must be numeric`}),pe=t({type:`oneOf`,validator(e,t){return g(e)&&g(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),$=t({type:`regex`,validator(e,t){return g(e)?v(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),me=t({type:`required`,validator:e=>g(e),message:`This field is required`}),he=t({type:`required`,validator(e,t){return t?g(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),ge=t({type:`required`,validator(e,t){return t?!0:g(e)},message:`This field is required`,active({$params:[e]}){return!e}}),_e=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),ve=t({type:`startsWith`,validator(e,t){return g(e)&&g(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),ye=t({type:`string`,validator:e=>g(e)?typeof e==`string`:!0,message:`The value must be a string`});function be(){return(()=>!0)}const xe=/^(?:(?:(?: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,Se=t({type:`url`,validator(e){return f(e)?!0:v(e,xe)},message:`The value is not a valid URL address`});export{E as alpha,k as alphaNum,ee as and,u as applyIf,C as assignIf,A as between,j as boolean,M as checked,N as contains,P as date,L as dateAfter,R as dateBefore,z as dateBetween,V as decimal,U as email,W as endsWith,te as exactLength,G as exactValue,y as getSize,q as hexadecimal,Y as integer,Z as ipv4Address,m as isDate,f as isEmpty,g as isFilled,_ as isNumber,Q as literal,ne as macAddress,v as matchRegex,ie as maxLength,ae as maxValue,oe as minLength,se as minValue,le as nativeEnum,S as not,ue as number,fe as numeric,pe as oneOf,x as or,$ as regex,me as required,he as requiredIf,ge as requiredUnless,_e as sameAs,ve as startsWith,ye as string,h as toDate,b as toNumber,be as type,Se as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/rules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Collection of rules and helpers for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"type-fest": "5.2.0",
|
|
7
|
-
"@regle/core": "1.
|
|
7
|
+
"@regle/core": "1.12.0"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"@types/node": "22.19.1",
|
|
11
11
|
"@vue/reactivity": "3.5.25",
|
|
12
12
|
"@vue/test-utils": "2.4.6",
|
|
13
|
-
"prettier": "3.
|
|
14
|
-
"tsdown": "0.16.
|
|
13
|
+
"prettier": "3.7.3",
|
|
14
|
+
"tsdown": "0.16.8",
|
|
15
15
|
"type-fest": "5.2.0",
|
|
16
16
|
"typescript": "5.9.3",
|
|
17
17
|
"vitest": "4.0.13",
|