ivl 0.2.6 → 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Oskar-V
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,11 +1,10 @@
1
1
  # Super lightweight input validation library
2
2
  This is a lightweight library for user input validation.
3
- Main focus is on speed and flexibility of the validation rules
3
+ Main focus is on speed and flexibility of the validation rules.
4
4
 
5
- By default it automatically detects and supports async rules in your rule sets.
5
+ By default `getInputErrors` and `getSchemaErrors` automatically detects and chooses the most performant checking method for your rule set.
6
6
 
7
- If your rule set and/or object are very large or complex, you may want to use
8
- `getInputErrorsSync`/`getSchemaErrorsSync` or `getInputErrorsAsync`/`getSchemaErrorsAsync` for improved performance on synchronous and asynchronous rule sets respectively.
7
+ If your rule set and/or schema are very large or complex, you may want to directly use `getInputErrorsSync`/`getSchemaErrorsSync` or `getInputErrorsAsync`/`getSchemaErrorsAsync` for improved performance on synchronous and asynchronous rule sets respectively.
9
8
 
10
9
  Use synchronous versions of the functions for better performance if you don't need to support asynchronous checks on your inputs.
11
10
 
@@ -20,7 +19,6 @@ const my_rules = {
20
19
  console.log(getInputErrors(50, my_rules)); // []
21
20
  console.log(getInputErrors(11, my_rules)); // ["Input must be more than 40", "Input must be divisible by 10"]
22
21
  console.log(getInputErrors(30, my_rules)) // ["Input must be more than 40"]
23
-
24
22
  ```
25
23
  # Installing
26
24
  ```typescript
@@ -35,9 +33,9 @@ pnpm install ivl // pnpm
35
33
  ---
36
34
  # Usage examples
37
35
  ## Frontend example
38
- ```javascript
39
- // src/index.ts
40
- import { getInputErrorsSync, getInputErrors } from 'ivl';
36
+ ### `index.ts`
37
+ ```typescript
38
+ import { getInputErrors } from 'ivl';
41
39
  import type { RULES } from 'ivl';
42
40
  import { matchesRegex, minLength, maxLength, isType } from 'ivl/helpers';
43
41
 
@@ -58,23 +56,23 @@ const EMAIL_REQUIREMENTS: RULES = {
58
56
  const PASSWORD_REQUIREMENTS: RULES = {
59
57
  "Must be string": isType('string'),
60
58
  "Must be at least 8 characters": minLength(8),
61
- "Must contain at least one upper case character": matchesRegex(/(?=.*[A-Z])/),
62
- "Must contain at least one lower case character": matchesRegex(/(?=.*[a-z])/),
59
+ "Must contain at least one upper case character": matchesRegex(/[A-Z]/),
60
+ "Must contain at least one lower case character": matchesRegex(/[a-z]/),
63
61
  };
64
62
 
65
63
  // You can of course expand on your existing rules:
66
64
  const STRONG_PASSWORD_REQUIREMENTS: RULES = {
67
65
  ...PASSWORD_REQUIREMENTS,
68
- "Must contain at least one digit": matchesRegex(/(?=.*\d)/),
69
- "Must contain at least one symbol": matchesRegex(/[^\w]/),
66
+ "Must contain at least one digit": matchesRegex(/\d/),
67
+ "Must contain at least one symbol": matchesRegex(/[^\w\s]/),
70
68
  };
71
69
 
72
70
  const email_value = "some-value";
73
71
  const pw_value = "Passesweakpw";
74
72
 
75
73
  const email_errors = getInputErrors(email_value, EMAIL_REQUIREMENTS);
76
- const pw_errors = getInputErrorsSync(pw_value, PASSWORD_REQUIREMENTS);
77
- const strong_pw_errors = getInputErrorsSync(pw_value, STRONG_PASSWORD_REQUIREMENTS);
74
+ const pw_errors = getInputErrors(pw_value, PASSWORD_REQUIREMENTS);
75
+ const strong_pw_errors = getInputErrors(pw_value, STRONG_PASSWORD_REQUIREMENTS);
78
76
 
79
77
  console.log({email_errors, pw_errors, strong_pw_errors});
80
78
  ```
@@ -82,10 +80,11 @@ console.log({email_errors, pw_errors, strong_pw_errors});
82
80
 
83
81
  ## Backend example with Hono & Bun.js
84
82
 
83
+ ### `rules.ts`
85
84
  ```typescript
86
- // rules.ts
87
-
88
85
  import { isType, minLength, maxLength, matchesRegex } from 'ivl/helpers';
86
+ import { EMAIL_PATTERN } from 'ivl/patterns';
87
+ import type { SCHEMA } from 'ivl';
89
88
  import { checkValueInDatabase } from 'my-database-controller';
90
89
 
91
90
  const existsInDatabase = (key: string, table: string, exists: boolean = true): RULE =>
@@ -101,10 +100,10 @@ const EMAIL_REQUIREMENTS = {
101
100
  const PASSWORD_REQUIREMENTS = {
102
101
  "Must be string": isType('string'),
103
102
  "Must be at least 8 characters": minLength(8),
104
- "Must contain at least one upper case character": matchesRegex(/(?=.*[A-Z])/),
105
- "Must contain at least one lower case character": matchesRegex(/(?=.*[a-z])/),
106
- "Must contain at least one digit": matchesRegex(/(?=.*\d)/),
107
- "Must contain at least one symbol": matchesRegex(/[^\w]/),
103
+ "Must contain at least one upper case character": matchesRegex(/[A-Z]/),
104
+ "Must contain at least one lower case character": matchesRegex(/[a-z]/),
105
+ "Must contain at least one digit": matchesRegex(/\d/),
106
+ "Must contain at least one symbol": matchesRegex(/[^\w\s]/),
108
107
  };
109
108
 
110
109
  export const LOGIN_SCHEMA: SCHEMA = {
@@ -113,7 +112,9 @@ export const LOGIN_SCHEMA: SCHEMA = {
113
112
  };
114
113
 
115
114
  export const REGISTER_SCHEMA: SCHEMA = {
116
- organization_name: {} // Pass in an empty rule set, so the value can be whatever
115
+ // We can pass in an empty rule set to allow any value
116
+ // Or we can omit the argument entirely and set the strict flag to false when checking the schema
117
+ organization_name: {}
117
118
  email: {
118
119
  ...EMAIL_REQUIREMENTS,
119
120
  "Email already registered": existsInDatabase('email','users', false)
@@ -121,61 +122,55 @@ export const REGISTER_SCHEMA: SCHEMA = {
121
122
  password: PASSWORD_REQUIREMENTS
122
123
  };
123
124
 
125
+ // Registering via invitation needs all the same values except organization name
126
+ // inherit parts of rule sets, as opposed to extending the rule set as show in the
127
+ // frontend example
128
+ export const INVITE_REGISTER_SCHEMA = (({ organization_name, ...invite_schema }) => invite_schema)(REGISTRATION_SCHEMA)
129
+
124
130
  export const INVITATION_PARAM: SCHEMA = {
125
- code: {
131
+ invitation_code: {
126
132
  "Not a valid invitation": existsInDatabase('code', 'invitation')
127
133
  }
128
134
  };
129
135
 
130
136
  ```
131
137
 
138
+ ### `index.ts`
132
139
  ```typescript
133
- // index.ts
134
- import { Hono } from 'hono';
135
- import { jwt } from 'hono/jwt';
140
+ import { Hono, ValidationTargets } from 'hono';
136
141
  import { validator } from 'hono/validator';
137
142
  import { getSchemaErrors } from 'ivl';
138
- import type { SCHEMA } from 'ivl';
139
- import { LOGIN_SCHEMA, REGISTER_SCHEMA, INVITATION_PARAM } from './rules.ts';
143
+ import { LOGIN_SCHEMA, REGISTER_SCHEMA, INVITE_REGISTER_SCHEMA, INVITATION_PARAM } from './rules.ts';
140
144
 
141
145
  // Wrapper for hono validator middleware
142
- const validateWrapper = (schema: SCHEMA, error: ClientErrorStatusCode | RedirectStatusCode = 400, strict = true) =>
143
- async (value: any, c: Context) => {
144
- const jwt = c.get('jwtPayload'); // Pass jwt to the validator so we could use its value when checking input validation rules
145
- const errors = await getSchemaErrors(value, schema, {strict:true}, jwt);
146
+ export const validate = (input_type: keyof ValidationTargets, schema: SCHEMA) =>
147
+ validator(input_type, async (value: CHECKABLE_OBJECT) => {
148
+ const errors = await getSchemaErrors(value, schema, { strict: true });
146
149
  if (Object.values(errors).filter((e) => e.length).length) {
147
- throw new HTTPException(error, {
150
+ throw new HTTPException(400, {
148
151
  message: JSON.stringify(errors),
149
152
  });
150
153
  }
151
154
  return value;
152
- }
155
+ })
153
156
 
154
157
  const app = new Hono();
155
158
 
156
- app.use(jwt({
157
- secret: "some-secret-value",
158
- cookie: 'token'
159
- }));
160
-
161
159
  app.post('/login',
162
- validator('json', validateWrapper(LOGIN_SCHEMA)),
160
+ validate('json', LOGIN_SCHEMA),
163
161
  (c) => c.text("Success"));
164
162
 
165
163
  app.all('/logout', (c) => c.text("Success"));
166
164
 
167
165
  app.put('/register',
168
- validator('json', validateWrapper(REGISTER_SCHEMA)),
166
+ validate('json', REGISTER_SCHEMA),
169
167
  (c) => c.text("Success"));
170
168
 
171
169
  // This first validates the invitation parameter against our database
172
170
  // And then validates the body of the request
173
171
  app.put('/register/:invitation_code',
174
- validator('param', validateWrapper(INVITATION_PARAM)),
175
- validator('json', validateWrapper(
176
- // Registering via invitation needs all the same values except organization name - inherit parts of rule sets, as opposed to extending the rule set as shown above
177
- (({ organization_name, ...invite_schema }) => invite_schema)(REGISTRATION_SCHEMA)
178
- )),
172
+ validate('param', INVITATION_PARAM),
173
+ validate('json', INVITE_REGISTER_SCHEMA ),
179
174
  (c)=> c.text("Success"));
180
175
 
181
176
  export default app;
@@ -13,9 +13,17 @@ export declare const maxLength: (max_length?: number) => RULE_SYNC;
13
13
  export declare const minLength: (min_length?: number) => RULE_SYNC;
14
14
  export declare const stringBetween: (max?: number, min?: number) => RULE_SYNC;
15
15
  export declare const numberBetween: (max?: number, min?: number) => RULE_SYNC;
16
- export declare const acceptAny: (rules?: RULE[]) => RULE;
16
+ export declare const acceptAnyAsync: (rules?: RULE[]) => RULE;
17
17
  export declare const acceptAnySync: (rules?: RULE_SYNC[]) => RULE_SYNC;
18
- export declare const allowUndefined: (rules: RULES) => {};
19
- export declare const preprocess: (fn: Function, rules: RULES) => {};
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
+ };
20
28
 
21
29
  export {};
@@ -1 +1 @@
1
- var s=(n)=>typeof n==="string"||n!==null&&typeof n==="object"&&("length"in n)&&typeof n.length==="number",i=(n)=>(e)=>typeof e===n,p=(n)=>(e)=>typeof e==="string"&&n.test(e),f=(n=1/0)=>(e)=>typeof e==="number"&&e<=n,m=(n=-1/0)=>(e)=>typeof e==="number"&&e>=n,w=(n=1/0)=>(e)=>s(e)&&e.length<=n,y=(n=0)=>(e)=>s(e)&&e.length>=n,a=(n=1/0,e=0)=>(t)=>typeof t==="string"&&t.length>=e&&t.length<=n,L=(n=1/0,e=-1/0)=>(t)=>typeof t==="number"&&t>=e&&t<=n,g=(n=[])=>async(e,...t)=>(await Promise.all(n.map((o)=>o(void 0,e,...t)))).some((o)=>o),h=(n=[])=>(e,...t)=>n.map((o)=>o(void 0,e,...t)).some((o)=>o);var l=(n)=>Object.entries(n).reduce((e,[t,o])=>({...e,[t]:(r,...u)=>typeof r==="undefined"?!0:o(r,...u)}),{}),x=(n,e)=>Object.entries(e).reduce((t,[o,r])=>({...t,[o]:(u,...c)=>r(n(u),...c)}),{});export{a as stringBetween,x as preprocess,L as numberBetween,y as minLength,m as min,w as maxLength,f as max,p as matchesRegex,i as isType,l as allowUndefined,h as acceptAnySync,g as acceptAny};
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};
package/lib/index.d.ts CHANGED
@@ -9,31 +9,31 @@ export type RULES = {
9
9
  [index: string]: RULE;
10
10
  };
11
11
  export type SCHEMA_SYNC = {
12
- [index: string]: RULES_SYNC;
12
+ [index: string]: RULES_SYNC | RULES_SYNC[];
13
13
  };
14
14
  export type SCHEMA = {
15
- [index: string]: RULES;
16
- };
17
- export type CHECKABLE_OBJECT = {
18
- [index: string]: unknown;
15
+ [index: string]: RULES | RULES[];
19
16
  };
20
17
  /**
21
18
  * @arg {boolean} strict --- Don't allow object to have keys not included in the schema
22
- */
19
+ */
23
20
  export type SCHEMA_OPTIONS = {
24
21
  strict?: boolean;
25
22
  };
26
- export type CHECKED_SCHEMA_SYNC = {
27
- [index: string]: string[];
23
+ export type CHECKABLE_OBJECT = {
24
+ [index: string]: unknown;
28
25
  };
29
- export type CHECKED_SCHEMA = Promise<CHECKED_SCHEMA_SYNC>;
26
+ export type CHECKED_SCHEMA_SYNC<T extends keyof CHECKABLE_OBJECT> = {
27
+ [K in T]: string[] | string[][];
28
+ };
29
+ export type CHECKED_SCHEMA<T extends keyof CHECKABLE_OBJECT> = Promise<CHECKED_SCHEMA_SYNC<T>>;
30
30
  /**
31
31
  * Detect if a RULES object has any async rules in it
32
32
  *
33
- * @param {Record<string, RULE>} rules a rules object
33
+ * @param {Record<string, RULE>|Record<string, RULE>[]} rules a rules object
34
34
  * @returns {boolean} true if any of the rules is an async function otherwise false
35
35
  */
36
- export declare const hasAsyncFunction: (rules: Record<string, RULE>) => boolean;
36
+ export declare const hasAsyncFunction: (rules: Record<string, RULE> | Record<string, RULE>[]) => boolean;
37
37
  /**
38
38
  * Check the input against all provided validation rules asynchronously
39
39
  *
@@ -46,13 +46,15 @@ export declare const getValueErrorsAsync: (value: unknown, rules: RULES, ...over
46
46
  /**
47
47
  * Check a schema against all provided validation rules asynchronously
48
48
  *
49
- * @param {CHECKABLE_OBJECT} object object to be validated
49
+ * @param {CHECKABLE_OBJECT} object_to_check object to be validated
50
50
  * @param {SCHEMA} schema schema to validate against
51
51
  * @param {SCHEMA_OPTIONS} options options affecting how the rules are run
52
52
  * @param {unknown[]} overload array of extra values the validator rules might need to properly validate the value
53
- * @returns {CHECKED_SCHEMA} an object containing keys and their respective lists of failed rule names
53
+ * @returns {CHECKED_SCHEMA<T>} an object containing keys and their respective lists of failed rule names
54
54
  */
55
- export declare const getSchemaErrorsAsync: (object: CHECKABLE_OBJECT, schema: SCHEMA, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA;
55
+ export declare const getSchemaErrorsAsync: <T extends keyof CHECKABLE_OBJECT>(object_to_check: Pick<CHECKABLE_OBJECT, T>, schema: {
56
+ [K in T]: RULES | RULES[];
57
+ }, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => Promise<CHECKED_SCHEMA_SYNC<T>>;
56
58
  /**
57
59
  *
58
60
  * @param {unknown} value the value to check
@@ -68,26 +70,25 @@ export declare const getValueErrorsSync: (value: unknown, rules: RULES_SYNC, ...
68
70
  * @param {SCHEMA} schema schema to validate against
69
71
  * @param {SCHEMA_OPTIONS} options options affecting how the rules are run
70
72
  * @param {unknown[]} overload array of extra values the validator rules might need to properly validate the value
71
- * @returns {CHECKED_SCHEMA} an object containing keys and their respective lists of failed rule names
73
+ * @returns {CHECKED_SCHEMA_SYNC<T>} an object containing keys and their respective lists of failed rule names
72
74
  */
73
- export declare const getSchemaErrorsSync: (object: CHECKABLE_OBJECT, schema: SCHEMA_SYNC, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA_SYNC;
75
+ export declare const getSchemaErrorsSync: <T extends keyof CHECKABLE_OBJECT>(object: CHECKABLE_OBJECT, schema: SCHEMA_SYNC, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA_SYNC<T>;
74
76
  /**
75
- *
76
77
  * @param {unknown} value the value to check
77
78
  * @param {RULES|RULES_SYNC} rules rules object to use for validating the provided value
78
79
  * @param {unknown[]} overload array of extra values the validator rules might need to properly validate the value
79
- * @returns {string[]} a list of rule names which failed validation
80
+ * @returns {Promise<string[]>|string[]} a list of rule names which failed validation
80
81
  */
81
- export declare const getValueErrors: (value: unknown, rules: RULES | RULES_SYNC, ...overload: unknown[]) => string[] | Promise<string[]>;
82
+ export declare const getValueErrors: (value: unknown, rules: RULES | RULES_SYNC, ...overload: unknown[]) => Promise<string[]> | string[];
82
83
  /**
83
84
  * Check a schema against all provided validation rules
84
85
  *
85
86
  * @param {CHECKABLE_OBJECT} object object to be validated
86
- * @param {SCHEMA} schema schema to validate against
87
+ * @param {SCHEMA|SCHEMA_SYNC} schema schema to validate against
87
88
  * @param {SCHEMA_OPTIONS} options options affecting how the rules are run
88
89
  * @param {unknown[]} overload array of extra values the validator rules might need to properly validate the value
89
90
  * @returns {CHECKED_SCHEMA} an object containing keys and their respective lists of failed rule names
90
91
  */
91
- export declare const getSchemaErrors: (object: CHECKABLE_OBJECT, schema: SCHEMA_SYNC | SCHEMA, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA_SYNC | CHECKED_SCHEMA;
92
+ export declare const getSchemaErrors: <T extends keyof CHECKABLE_OBJECT>(object: CHECKABLE_OBJECT, schema: SCHEMA_SYNC | SCHEMA, options?: SCHEMA_OPTIONS, ...overload: unknown[]) => CHECKED_SCHEMA<T> | CHECKED_SCHEMA_SYNC<T>;
92
93
 
93
94
  export {};
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- var Z={strict:!1},$=(z)=>Object.values(z).some((q)=>typeof q==="function"&&q.constructor.name==="AsyncFunction"),J=async(z,q,...W)=>{const G={};return Object.entries(q).forEach(([x,Q])=>{G[x]=Promise.resolve(!1).then(()=>Q(z,...W)).then((R)=>G[x]=R).catch((R)=>{return console.warn(`Rule validation functions for "${x}" didn't exit cleanly`,R),G[x]=!1})}),await Promise.allSettled(Object.values(G)),Object.entries(G).reduce((x,[Q,R])=>R?x:[...x,Q],[])},K=async(z,q,W=Z,...G)=>{const x={};if(Object.entries(q).forEach(([Q,R])=>{x[Q]=J(z[Q],R,...G).then((X)=>{x[Q]=X}).catch((X)=>{x[Q]=X})}),W.strict){const Q=Object.keys(z),R=new Set(Object.keys(q));Q.filter((Y)=>!R.has(Y)).forEach((Y)=>{x[Y]=["Key not allowed"]})}return await Promise.allSettled(Object.values(x)),x},B=(z,q,...W)=>Object.entries(q).reduce((G,[x,Q])=>{try{return Q(z,...W)?G:[...G,x]}catch(R){return console.warn(`Rule validation function for "${x}" didn't exit cleanly`,R),[...G,x]}},[]),f=(z,q,W=Z,...G)=>{const x=Object.entries(q).reduce((Q,[R,X])=>({...Q,[R]:B(z[R],X,...G)}),{});if(W.strict){const Q=Object.keys(z),R=new Set(Object.keys(q));Q.filter((Y)=>!R.has(Y)).forEach((Y)=>{x[Y]=["Key not allowed"]})}return x},U=(z,q,...W)=>{if($(q))return J(z,q,...W);return B(z,q,...W)},I=(z,q,W=Z,...G)=>{if(Object.values(q).some($))return K(z,q,W,...G);return f(z,q,W,...G)};export{$ as hasAsyncFunction,B as getValueErrorsSync,J as getValueErrorsAsync,U as getValueErrors,f as getSchemaErrorsSync,K as getSchemaErrorsAsync,I as getSchemaErrors};
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};
@@ -6,7 +6,6 @@ export declare const URL_PATTERN: RegExp;
6
6
  export declare const ISO_8601_DATETIME_PATTERN_STRICT: RegExp;
7
7
  export declare const ISO_8601_DATETIME_PATTERN: RegExp;
8
8
  export declare const ISO_8601_TIME_PATTERN: RegExp;
9
- export declare const RFC_2822_TIMESTAMP_PATTERN: RegExp;
10
9
  export declare const CONTAINS_LOWERCASE_CHARACTER_PATTERN: RegExp;
11
10
  export declare const CONTAINS_UPPERCASE_CHARACTER_PATTERN: RegExp;
12
11
  export declare const CONTAINS_DIGIT_CHARACTER_PATTERN: RegExp;
@@ -1 +1 @@
1
- var x=/^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{1,}$/,c=/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,d=/^(https?:\/\/)?([^\s$.?#].[^\s]*)\.[a-z]{2,}(\/[^ \t\r\n\v\f]*)?$/i,f=/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|Z)?)$/,j=/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d(?::[0-5]\d(?:\.\d+)?)?)$/,k=/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(Z|[+-](?:2[0-3]|[01][0-9]):([0-5][0-9]))?$/,l=/^(?:(Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s)?(0[1-9]|[1-2]?[0-9]|3[01])\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(19[0-9]{2}|[2-9][0-9]{3})\s(2[0-3]|[0-1][0-9]):([0-5][0-9])(?::(60|[0-5][0-9]))?\s([-\+][0-9]{2}[0-5][0-9]|(?:UT|GMT|(?:E|C|M|P)(?:ST|DT)|[A-IK-Z]))(?:\s|\((?:[^()\\]+|\\.)*?\))*$/,m=/[a-z]/,p=/[A-Z]/,q=/\d/,r=/[^\w\s]/;export{d as URL_PATTERN,l as RFC_2822_TIMESTAMP_PATTERN,c as MYSQL_TIMESTAMP_PATTERN,k as ISO_8601_TIME_PATTERN,f as ISO_8601_DATETIME_PATTERN_STRICT,j as ISO_8601_DATETIME_PATTERN,x as EMAIL_PATTERN,p as CONTAINS_UPPERCASE_CHARACTER_PATTERN,r as CONTAINS_SYMBOL_CHARACTER_PATTERN,m as CONTAINS_LOWERCASE_CHARACTER_PATTERN,q as CONTAINS_DIGIT_CHARACTER_PATTERN};
1
+ var x=/^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{1,}$/,c=/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,d=/^(https?:\/\/)?([^\s$.?#].[^\s]*)\.[a-z]{2,}(\/[^ \t\r\n\v\f]*)?$/i,f=/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|Z)?)$/,j=/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d(?::[0-5]\d(?:\.\d+)?)?)$/,k=/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(Z|[+-](?:2[0-3]|[01][0-9]):([0-5][0-9]))?$/,l=/[a-z]/,m=/[A-Z]/,p=/\d/,q=/[^\w\s]/;export{d as URL_PATTERN,c as MYSQL_TIMESTAMP_PATTERN,k as ISO_8601_TIME_PATTERN,f as ISO_8601_DATETIME_PATTERN_STRICT,j as ISO_8601_DATETIME_PATTERN,x as EMAIL_PATTERN,m as CONTAINS_UPPERCASE_CHARACTER_PATTERN,q as CONTAINS_SYMBOL_CHARACTER_PATTERN,l as CONTAINS_LOWERCASE_CHARACTER_PATTERN,p as CONTAINS_DIGIT_CHARACTER_PATTERN};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ivl",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "author": {
5
5
  "name": "Oskar Voorel",
6
6
  "email": "oskar@voorel.com"