@keysdown/form-wrapper 0.0.12 → 0.0.14
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 +35 -5
- package/dist/core/Form.d.ts +2 -2
- package/dist/form-wrapper.cjs +1 -1
- package/dist/form-wrapper.iife.js +1 -1
- package/dist/form-wrapper.js +35 -35
- package/dist/form-wrapper.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -296,7 +296,7 @@ console.log(form.awaiting) // false
|
|
|
296
296
|
Method used to validate the entire form or a specific field.
|
|
297
297
|
|
|
298
298
|
```js
|
|
299
|
-
|
|
299
|
+
const form = createForm({
|
|
300
300
|
username: {
|
|
301
301
|
value: null,
|
|
302
302
|
rules: ['required'],
|
|
@@ -330,7 +330,7 @@ form.validate()
|
|
|
330
330
|
Method used to validate a specific field.
|
|
331
331
|
|
|
332
332
|
```js
|
|
333
|
-
|
|
333
|
+
const form = createForm({
|
|
334
334
|
username: {
|
|
335
335
|
value: null,
|
|
336
336
|
rules: ['required'],
|
|
@@ -364,7 +364,7 @@ form.validate('username')
|
|
|
364
364
|
Method used to validate the entire form.
|
|
365
365
|
|
|
366
366
|
```js
|
|
367
|
-
|
|
367
|
+
const form = createForm({
|
|
368
368
|
username: {
|
|
369
369
|
value: null,
|
|
370
370
|
rules: ['required'],
|
|
@@ -400,7 +400,7 @@ form.validate()
|
|
|
400
400
|
Method used to access all form values in json format.
|
|
401
401
|
|
|
402
402
|
```js
|
|
403
|
-
|
|
403
|
+
const form = createForm({
|
|
404
404
|
username: null
|
|
405
405
|
})
|
|
406
406
|
|
|
@@ -409,12 +409,27 @@ form.username = 'keysdown'
|
|
|
409
409
|
axios.post('some-api', form.values())
|
|
410
410
|
```
|
|
411
411
|
|
|
412
|
+
#### Filtering values
|
|
413
|
+
|
|
414
|
+
It is also possible to filter all accessed form values.
|
|
415
|
+
|
|
416
|
+
```js
|
|
417
|
+
const form = createForm({
|
|
418
|
+
first_name: null,
|
|
419
|
+
last_name: null
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
form.first_name = 'keysdown'
|
|
423
|
+
|
|
424
|
+
axios.post('some-api', form.values(['first_name']))
|
|
425
|
+
```
|
|
426
|
+
|
|
412
427
|
### valuesAsFormData()
|
|
413
428
|
|
|
414
429
|
Method used to access all form values as form data.
|
|
415
430
|
|
|
416
431
|
```js
|
|
417
|
-
|
|
432
|
+
const form = createForm({
|
|
418
433
|
username: null
|
|
419
434
|
})
|
|
420
435
|
|
|
@@ -427,6 +442,21 @@ axios.post('some-api', form.valuesAsFormData(), {
|
|
|
427
442
|
})
|
|
428
443
|
```
|
|
429
444
|
|
|
445
|
+
#### Filtering values
|
|
446
|
+
|
|
447
|
+
It is also possible to filter all accessed form values.
|
|
448
|
+
|
|
449
|
+
```js
|
|
450
|
+
const form = createForm({
|
|
451
|
+
first_name: null,
|
|
452
|
+
last_name: null
|
|
453
|
+
})
|
|
454
|
+
|
|
455
|
+
form.first_name = 'keysdown'
|
|
456
|
+
|
|
457
|
+
axios.post('some-api', form.valuesAsFormData(['first_name']))
|
|
458
|
+
```
|
|
459
|
+
|
|
430
460
|
## Validation
|
|
431
461
|
|
|
432
462
|
There is a property in the form object dedicated to validations, you can access everything related to validations through the `validation` property:
|
package/dist/core/Form.d.ts
CHANGED
|
@@ -22,6 +22,6 @@ export declare class Form {
|
|
|
22
22
|
validate(field?: string | null): Promise<this | void>;
|
|
23
23
|
validateField(field: string): Promise<void>;
|
|
24
24
|
validateForm(): Promise<this>;
|
|
25
|
-
values(): Values;
|
|
26
|
-
valuesAsFormData(): FormData;
|
|
25
|
+
values(only?: string[]): Values;
|
|
26
|
+
valuesAsFormData(only?: string[]): FormData;
|
|
27
27
|
}
|
package/dist/form-wrapper.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var m=Object.defineProperty;var F=(s,t,e)=>t in s?m(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var a=(s,t,e)=>F(s,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=s=>typeof s=="string"?s.split("|"):s,b=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?p(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class u{constructor(){a(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const r=Array.isArray(e)?e[0]:e;if(r)return String(r)}return null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class
|
|
1
|
+
"use strict";var m=Object.defineProperty;var F=(s,t,e)=>t in s?m(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var a=(s,t,e)=>F(s,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=s=>typeof s=="string"?s.split("|"):s,b=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?p(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class u{constructor(){a(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const r=Array.isArray(e)?e[0]:e;if(r)return String(r)}return null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class f extends u{}class j extends u{}class w extends u{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class y{constructor(){a(this,"errors",new w);a(this,"messages",new f);a(this,"rules",new j)}}const O=s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()}),c={required:O},P=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),h=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const n=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(n)>-1)){if(P(n)&&!(n instanceof File)||Array.isArray(n)){h(n,r,i);return}r.append(i,n)}}),r};class d{constructor(t){a(this,"awaiting",!1);a(this,"originalValues",{});a(this,"validation",new y);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){const r=b(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const n=i.split(":"),l=n[0],g=n.length===2?n[1].split(","):[];return l in c?c[l](this[t],g).catch(v=>{const o=this.validation.messages.get(t);return o&&l in o&&this.validation.errors.push(t,o[l]),Promise.reject(v)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(t){const e={};return Object.keys(this.originalValues).forEach(r=>{(!t||t.includes(r))&&(e[r]=this[r])}),e}valuesAsFormData(t){return h(this.values(t))}}const V=s=>new d(s);exports.createForm=V;exports.default=d;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var FormWrapper=function(n){"use strict";var P=Object.defineProperty;var V=(n,l,u)=>l in n?P(n,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):n[l]=u;var o=(n,l,u)=>V(n,typeof l!="symbol"?l+"":l,u);const l=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?l(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class h{constructor(){o(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const r=Array.isArray(e)?e[0]:e;if(r)return String(r)}return null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class F extends h{}class p extends h{}class
|
|
1
|
+
var FormWrapper=function(n){"use strict";var P=Object.defineProperty;var V=(n,l,u)=>l in n?P(n,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):n[l]=u;var o=(n,l,u)=>V(n,typeof l!="symbol"?l+"":l,u);const l=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?l(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class h{constructor(){o(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const r=Array.isArray(e)?e[0]:e;if(r)return String(r)}return null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class F extends h{}class p extends h{}class f extends h{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class b{constructor(){o(this,"errors",new f);o(this,"messages",new F);o(this,"rules",new p)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},j=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),v=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const a=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(a)>-1)){if(j(a)&&!(a instanceof File)||Array.isArray(a)){v(a,r,i);return}r.append(i,a)}}),r};class m{constructor(t){o(this,"awaiting",!1);o(this,"originalValues",{});o(this,"validation",new b);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){const r=u(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const a=i.split(":"),c=a[0],y=a.length===2?a[1].split(","):[];return c in g?g[c](this[t],y).catch(O=>{const d=this.validation.messages.get(t);return d&&c in d&&this.validation.errors.push(t,d[c]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(t){const e={};return Object.keys(this.originalValues).forEach(r=>{(!t||t.includes(r))&&(e[r]=this[r])}),e}valuesAsFormData(t){return v(this.values(t))}}const w=s=>new m(s);return n.createForm=w,n.default=m,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({});
|
package/dist/form-wrapper.js
CHANGED
|
@@ -21,9 +21,9 @@ class h {
|
|
|
21
21
|
first(t) {
|
|
22
22
|
const s = this.get(t);
|
|
23
23
|
if (s) {
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
26
|
-
return String(
|
|
24
|
+
const i = Array.isArray(s) ? s[0] : s;
|
|
25
|
+
if (i)
|
|
26
|
+
return String(i);
|
|
27
27
|
}
|
|
28
28
|
return null;
|
|
29
29
|
}
|
|
@@ -52,16 +52,16 @@ class h {
|
|
|
52
52
|
return this.collection = {}, this;
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
+
class f extends h {
|
|
56
|
+
}
|
|
55
57
|
class j extends h {
|
|
56
58
|
}
|
|
57
59
|
class b extends h {
|
|
58
|
-
}
|
|
59
|
-
class f extends h {
|
|
60
60
|
push(t, s) {
|
|
61
|
-
const
|
|
61
|
+
const i = this.get(t) || [];
|
|
62
62
|
return this.collection = {
|
|
63
63
|
...this.collection,
|
|
64
|
-
[t]: [...
|
|
64
|
+
[t]: [...i, s]
|
|
65
65
|
}, this;
|
|
66
66
|
}
|
|
67
67
|
get(t) {
|
|
@@ -70,9 +70,9 @@ class f extends h {
|
|
|
70
70
|
}
|
|
71
71
|
class w {
|
|
72
72
|
constructor() {
|
|
73
|
-
a(this, "errors", new
|
|
74
|
-
a(this, "messages", new
|
|
75
|
-
a(this, "rules", new
|
|
73
|
+
a(this, "errors", new b());
|
|
74
|
+
a(this, "messages", new f());
|
|
75
|
+
a(this, "rules", new j());
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
const y = (e) => new Promise((t, s) => {
|
|
@@ -80,17 +80,17 @@ const y = (e) => new Promise((t, s) => {
|
|
|
80
80
|
}), c = {
|
|
81
81
|
required: y
|
|
82
82
|
}, V = (e) => e !== null && typeof e == "object" && !Array.isArray(e), u = (e, t, s = null) => {
|
|
83
|
-
const
|
|
84
|
-
return Object.keys(e).forEach((
|
|
85
|
-
const n = e[
|
|
86
|
-
if (
|
|
83
|
+
const i = t || new FormData();
|
|
84
|
+
return Object.keys(e).forEach((r) => {
|
|
85
|
+
const n = e[r];
|
|
86
|
+
if (r = s ? `${s}[${r}]` : r, !([void 0, null].indexOf(n) > -1)) {
|
|
87
87
|
if (V(n) && !(n instanceof File) || Array.isArray(n)) {
|
|
88
|
-
u(n,
|
|
88
|
+
u(n, i, r);
|
|
89
89
|
return;
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
i.append(r, n);
|
|
92
92
|
}
|
|
93
|
-
}),
|
|
93
|
+
}), i;
|
|
94
94
|
};
|
|
95
95
|
class O {
|
|
96
96
|
constructor(t) {
|
|
@@ -100,9 +100,9 @@ class O {
|
|
|
100
100
|
this.addFields(t);
|
|
101
101
|
}
|
|
102
102
|
addField(t, s) {
|
|
103
|
-
if (s !== null && typeof s == "object" && "value" in s) {
|
|
104
|
-
const
|
|
105
|
-
this[t] =
|
|
103
|
+
if (s !== null && typeof s == "object" && "value" in s && !(s instanceof File)) {
|
|
104
|
+
const i = p(s);
|
|
105
|
+
this[t] = i.value, this.originalValues[t] = i.value, this.validation.messages.push(t, i.validation.messages), this.validation.rules.push(t, i.validation.rules);
|
|
106
106
|
} else
|
|
107
107
|
this[t] = s, this.originalValues[t] = s;
|
|
108
108
|
return this;
|
|
@@ -116,9 +116,9 @@ class O {
|
|
|
116
116
|
return this.validation.errors;
|
|
117
117
|
}
|
|
118
118
|
fill(t, s = !1) {
|
|
119
|
-
return Object.keys(t).forEach((
|
|
120
|
-
let
|
|
121
|
-
s && (this.originalValues[
|
|
119
|
+
return Object.keys(t).forEach((i) => {
|
|
120
|
+
let r = t[i];
|
|
121
|
+
s && (this.originalValues[i] = r), this[i] = r;
|
|
122
122
|
}), this;
|
|
123
123
|
}
|
|
124
124
|
get messages() {
|
|
@@ -150,16 +150,16 @@ class O {
|
|
|
150
150
|
this.validation.errors.unset(t);
|
|
151
151
|
const s = this.validation.rules.get(t);
|
|
152
152
|
if (s && s.length > 0) {
|
|
153
|
-
const
|
|
154
|
-
(
|
|
155
|
-
const n =
|
|
153
|
+
const i = s.map(
|
|
154
|
+
(r) => {
|
|
155
|
+
const n = r.split(":"), l = n[0], d = n.length === 2 ? n[1].split(",") : [];
|
|
156
156
|
return l in c ? c[l](this[t], d).catch((g) => {
|
|
157
157
|
const o = this.validation.messages.get(t);
|
|
158
158
|
return o && l in o && this.validation.errors.push(t, o[l]), Promise.reject(g);
|
|
159
|
-
}) : Promise.reject(new Error(`There is no validation rule called "${
|
|
159
|
+
}) : Promise.reject(new Error(`There is no validation rule called "${r}"`));
|
|
160
160
|
}
|
|
161
161
|
);
|
|
162
|
-
return Promise.all(
|
|
162
|
+
return Promise.all(i).then(() => {
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
165
|
return Promise.resolve();
|
|
@@ -170,14 +170,14 @@ class O {
|
|
|
170
170
|
);
|
|
171
171
|
return Promise.all(t).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
|
|
172
172
|
}
|
|
173
|
-
values() {
|
|
174
|
-
const
|
|
175
|
-
return Object.keys(this.originalValues).forEach((
|
|
176
|
-
t[
|
|
177
|
-
}),
|
|
173
|
+
values(t) {
|
|
174
|
+
const s = {};
|
|
175
|
+
return Object.keys(this.originalValues).forEach((i) => {
|
|
176
|
+
(!t || t.includes(i)) && (s[i] = this[i]);
|
|
177
|
+
}), s;
|
|
178
178
|
}
|
|
179
|
-
valuesAsFormData() {
|
|
180
|
-
return u(this.values());
|
|
179
|
+
valuesAsFormData(t) {
|
|
180
|
+
return u(this.values(t));
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
const A = (e) => new O(e);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.FormWrapper={}))})(this,function(r){"use strict";var P=Object.defineProperty;var V=(r,n,u)=>n in r?P(r,n,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[n]=u;var
|
|
1
|
+
(function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.FormWrapper={}))})(this,function(r){"use strict";var P=Object.defineProperty;var V=(r,n,u)=>n in r?P(r,n,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[n]=u;var l=(r,n,u)=>V(r,typeof n!="symbol"?n+"":n,u);const n=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?n(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class h{constructor(){l(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const i=Array.isArray(e)?e[0]:e;if(i)return String(i)}return null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class f extends h{}class p extends h{}class F extends h{push(t,e){const i=this.get(t)||[];return this.collection={...this.collection,[t]:[...i,e]},this}get(t){return super.get(t,[])}}class b{constructor(){l(this,"errors",new F);l(this,"messages",new f);l(this,"rules",new p)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},j=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),m=(s,t,e=null)=>{const i=t||new FormData;return Object.keys(s).forEach(a=>{const o=s[a];if(a=e?`${e}[${a}]`:a,!([void 0,null].indexOf(o)>-1)){if(j(o)&&!(o instanceof File)||Array.isArray(o)){m(o,i,a);return}i.append(a,o)}}),i};class v{constructor(t){l(this,"awaiting",!1);l(this,"originalValues",{});l(this,"validation",new b);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){const i=u(e);this[t]=i.value,this.originalValues[t]=i.value,this.validation.messages.push(t,i.validation.messages),this.validation.rules.push(t,i.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(i=>{let a=t[i];e&&(this.originalValues[i]=a),this[i]=a}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const i=e.map(a=>{const o=a.split(":"),c=o[0],w=o.length===2?o[1].split(","):[];return c in g?g[c](this[t],w).catch(O=>{const d=this.validation.messages.get(t);return d&&c in d&&this.validation.errors.push(t,d[c]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${a}"`))});return Promise.all(i).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(t){const e={};return Object.keys(this.originalValues).forEach(i=>{(!t||t.includes(i))&&(e[i]=this[i])}),e}valuesAsFormData(t){return m(this.values(t))}}const y=s=>new v(s);r.createForm=y,r.default=v,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|