ivl 0.3.0 → 0.3.2

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.
@@ -15,15 +15,7 @@ export declare const stringBetween: (max?: number, min?: number) => RULE_SYNC;
15
15
  export declare const numberBetween: (max?: number, min?: number) => RULE_SYNC;
16
16
  export declare const acceptAnyAsync: (rules?: RULE[]) => RULE;
17
17
  export declare const acceptAnySync: (rules?: RULE_SYNC[]) => RULE_SYNC;
18
- declare const allowUndefined: (rules: RULES) => {};
19
- declare const preprocess: (fn: Function, rules: RULES) => {};
20
-
21
- declare namespace ruleSet {
22
- export { allowUndefined, preprocess };
23
- }
24
-
25
- export {
26
- ruleSet,
27
- };
18
+ export declare const allowUndefined: (rules: RULES) => RULES;
19
+ export declare const preprocess: (fn: Function, rules: RULES) => RULES;
28
20
 
29
21
  export {};
@@ -1 +1 @@
1
- var i=Object.defineProperty;var m=(e,n)=>{for(var t in n)i(e,t,{get:n[t],enumerable:!0,configurable:!0,set:(o)=>n[t]=()=>o})};var u=(e)=>typeof e==="string"||e!==null&&typeof e==="object"&&("length"in e)&&typeof e.length==="number",a=(e)=>(n)=>typeof n===e,L=(e)=>(n)=>typeof n==="string"&&e.test(n),g=(e=1/0)=>(n)=>typeof n==="number"&&n<=e,h=(e=-1/0)=>(n)=>typeof n==="number"&&n>=e,l=(e=1/0)=>(n)=>u(n)&&n.length<=e,R=(e=0)=>(n)=>u(n)&&n.length>=e,x=(e=1/0,n=0)=>(t)=>typeof t==="string"&&t.length>=n&&t.length<=e,E=(e=1/0,n=-1/0)=>(t)=>typeof t==="number"&&t>=n&&t<=e,U=(e=[])=>async(n,...t)=>(await Promise.all(e.map((o)=>o(n,...t)))).some((o)=>o),b=(e=[])=>(n,...t)=>e.map((o)=>o(n,...t)).some((o)=>o);var p={};m(p,{preprocess:()=>f,allowUndefined:()=>y});var y=(e)=>Object.entries(e).reduce((n,[t,o])=>({...n,[t]:(r,...s)=>typeof r==="undefined"?!0:o(r,...s)}),{}),f=(e,n)=>Object.entries(n).reduce((t,[o,r])=>({...t,[o]:(s,...c)=>r(e(s),...c)}),{});export{x as stringBetween,p as ruleSet,E as numberBetween,R as minLength,h as min,l as maxLength,g as max,L as matchesRegex,a as isType,b as acceptAnySync,U as acceptAnyAsync};
1
+ var U=(n)=>typeof n==="string"||n!==null&&typeof n==="object"&&("length"in n)&&typeof n.length==="number",Y=(n)=>(f)=>typeof f===n,N=(n)=>(f)=>typeof f==="string"&&n.test(f),g=(n=1/0)=>(f)=>typeof f==="number"&&f<=n,I=(n=-1/0)=>(f)=>typeof f==="number"&&f>=n,q=(n=1/0)=>(f)=>U(f)&&f.length<=n,z=(n=0)=>(f)=>U(f)&&f.length>=n,B=(n=1/0,f=0)=>(w)=>typeof w==="string"&&w.length>=f&&w.length<=n,G=(n=1/0,f=-1/0)=>(w)=>typeof w==="number"&&w>=f&&w<=n,Q=(n=[])=>async(f,...w)=>(await Promise.all(n.map((R)=>R(f,...w)))).some((R)=>R),W=(n=[])=>(f,...w)=>n.map((R)=>R(f,...w)).some((R)=>R);var p=(n)=>typeof n==="function"&&n.constructor.name==="AsyncFunction";var J=(n)=>Object.entries(n).reduce((f,[w,R])=>{if(p(R))return{...f,[w]:async(L,...x)=>typeof L==="undefined"?!0:R(L,...x)};return{...f,[w]:(L,...x)=>typeof L==="undefined"?!0:R(L,...x)}},{}),K=(n,f)=>Object.entries(f).reduce((w,[R,L])=>({...w,[R]:(x,...t)=>L(n(x),...t)}),{});export{B as stringBetween,K as preprocess,G as numberBetween,z as minLength,I as min,q as maxLength,g as max,N as matchesRegex,Y as isType,J as allowUndefined,W as acceptAnySync,Q as acceptAnyAsync};
package/lib/index.d.ts CHANGED
@@ -27,13 +27,20 @@ export type CHECKED_SCHEMA_SYNC<T extends keyof CHECKABLE_OBJECT> = {
27
27
  [K in T]: string[] | string[][];
28
28
  };
29
29
  export type CHECKED_SCHEMA<T extends keyof CHECKABLE_OBJECT> = Promise<CHECKED_SCHEMA_SYNC<T>>;
30
+ /**
31
+ * Detect if a function is async or not
32
+ *
33
+ * @param {Function} fn a rules object
34
+ * @returns {boolean} true if any of the rules is an async function otherwise false
35
+ */
36
+ export declare const isAsyncFunction: (fn: Function) => boolean;
30
37
  /**
31
38
  * Detect if a RULES object has any async rules in it
32
39
  *
33
- * @param {Record<string, RULE>|Record<string, RULE>[]} rules a rules object
40
+ * @param {RULES|RULES[]} rules a rules object
34
41
  * @returns {boolean} true if any of the rules is an async function otherwise false
35
42
  */
36
- export declare const hasAsyncFunction: (rules: Record<string, RULE> | Record<string, RULE>[]) => boolean;
43
+ export declare const hasAsyncFunction: (rules: RULES | RULES[]) => boolean;
37
44
  /**
38
45
  * Check the input against all provided validation rules asynchronously
39
46
  *
@@ -54,7 +61,7 @@ export declare const getValueErrorsAsync: (value: unknown, rules: RULES, ...over
54
61
  */
55
62
  export declare const getSchemaErrorsAsync: <T extends keyof CHECKABLE_OBJECT>(object_to_check: Pick<CHECKABLE_OBJECT, T>, schema: {
56
63
  [K in T]: RULES | RULES[];
57
- }, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => Promise<CHECKED_SCHEMA_SYNC<T>>;
64
+ }, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA<T>;
58
65
  /**
59
66
  *
60
67
  * @param {unknown} value the value to check
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- var K={strict:!1},U=(x)=>{if(Array.isArray(x))return x.some((z)=>Object.values(z).some((W)=>typeof W==="function"&&W.constructor.name==="AsyncFunction"));return Object.values(x).some((z)=>typeof z==="function"&&z.constructor.name==="AsyncFunction")},Y=async(x,z,...W)=>{const Q={};return Object.entries(z).forEach(([G,X])=>{Q[G]=Promise.resolve(!1).then(()=>X(x,...W)).then((q)=>Q[G]=q).catch(()=>{return Q[G]=!1})}),await Promise.allSettled(Object.values(Q)),Object.entries(Q).reduce((G,[X,q])=>q?G:[...G,X],[])},I=async(x,z,W=K,...Q)=>{const G={},X=[];if(Object.entries(z).forEach(([q,$])=>{let Z;if(Array.isArray($))Z=Promise.allSettled($.map((R)=>Y(x[q],R,...Q))).then((R)=>{const f=R.map((J)=>{if(J.status==="fulfilled")return J.value;return["Unknown error occurred"]},[]);if(f.some((J)=>!J.length))G[q]=[];else G[q]=f}).catch((R)=>{if(R instanceof Error)G[q]=[R.message];else G[q]=["Unknown error occurred"]});else Z=Y(x[q],$,...Q).then((R)=>{G[q]=R}).catch((R)=>{G[q]=R});X.push(Z)}),W.strict){const q=Object.keys(x),$=new Set(Object.keys(z));q.filter((R)=>!$.has(R)).forEach((R)=>{G[R]=["Key not allowed"]})}return await Promise.allSettled(X),G},B=(x,z,...W)=>Object.entries(z).reduce((Q,[G,X])=>{try{return X(x,...W)?Q:[...Q,G]}catch(q){return[...Q,G]}},[]),L=(x,z,W=K,...Q)=>{const G=Object.entries(z).reduce((X,[q,$])=>{if(Array.isArray($)){const Z=$.map((R)=>B(x[q],R,...Q));if(Z.some((R)=>!R.length))return{...X,[q]:[]};return{...X,[q]:Z}}return{...X,[q]:B(x[q],$,...Q)}},{});if(W.strict){const X=Object.keys(x),q=new Set(Object.keys(z));X.filter((Z)=>!q.has(Z)).forEach((Z)=>{G[Z]=["Key not allowed"]})}return G},D=(x,z,...W)=>{if(U(z))return Y(x,z,...W);return B(x,z,...W)},w=(x)=>Object.values(x).some(U),F=(x,z,W=K,...Q)=>{if(w(z))return I(x,z,W,...Q);return L(x,z,W,...Q)};export{U as hasAsyncFunction,B as getValueErrorsSync,Y as getValueErrorsAsync,D as getValueErrors,L as getSchemaErrorsSync,I as getSchemaErrorsAsync,F as getSchemaErrors};
1
+ var K={strict:!1},I=(x)=>typeof x==="function"&&x.constructor.name==="AsyncFunction",L=(x)=>{if(Array.isArray(x))return x.some((G)=>Object.values(G).some(I));return Object.values(x).some(I)},Y=async(x,G,...X)=>{let Q={};return Object.entries(G).forEach(([z,W])=>{Q[z]=Promise.resolve(!1).then(()=>W(x,...X)).then((q)=>Q[z]=q).catch(()=>{return Q[z]=!1})}),await Promise.allSettled(Object.values(Q)),Object.entries(Q).reduce((z,[W,q])=>q?z:[...z,W],[])},w=async(x,G,X=K,...Q)=>{let z={},W=[];if(Object.entries(G).forEach(([q,$])=>{let Z;if(Array.isArray($))Z=Promise.allSettled($.map((R)=>Y(x[q],R,...Q))).then((R)=>{let U=R.map((J)=>{if(J.status==="fulfilled")return J.value;return["Unknown error occurred"]},[]);if(U.some((J)=>!J.length))z[q]=[];else z[q]=U}).catch((R)=>{if(R instanceof Error)z[q]=[R.message];else z[q]=["Unknown error occurred"]});else Z=Y(x[q],$,...Q).then((R)=>{z[q]=R}).catch((R)=>{z[q]=R});W.push(Z)}),X.strict){let q=Object.keys(x),$=new Set(Object.keys(G));q.filter((R)=>!$.has(R)).forEach((R)=>{z[R]=["Key not allowed"]})}return await Promise.allSettled(W),z},B=(x,G,...X)=>Object.entries(G).reduce((Q,[z,W])=>{try{return W(x,...X)?Q:[...Q,z]}catch(q){return[...Q,z]}},[]),D=(x,G,X=K,...Q)=>{let z=Object.entries(G).reduce((W,[q,$])=>{if(Array.isArray($)){let Z=$.map((R)=>B(x[q],R,...Q));if(Z.some((R)=>!R.length))return{...W,[q]:[]};return{...W,[q]:Z}}return{...W,[q]:B(x[q],$,...Q)}},{});if(X.strict){let W=Object.keys(x),q=new Set(Object.keys(G));W.filter((Z)=>!q.has(Z)).forEach((Z)=>{z[Z]=["Key not allowed"]})}return z},P=(x,G,...X)=>{if(L(G))return Y(x,G,...X);return B(x,G,...X)},N=(x)=>Object.values(x).some(L),V=(x,G,X=K,...Q)=>{if(N(G))return w(x,G,X,...Q);return D(x,G,X,...Q)};export{I as isAsyncFunction,L as hasAsyncFunction,B as getValueErrorsSync,Y as getValueErrorsAsync,P as getValueErrors,D as getSchemaErrorsSync,w as getSchemaErrorsAsync,V as getSchemaErrors};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ivl",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "author": {
5
5
  "name": "Oskar Voorel",
6
6
  "email": "oskar@voorel.com"