jails.stdlib 1.2.0 → 2.0.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/form-validation/index.js +97 -89
- package/form-validation/index.ts +9 -11
- package/form-validation/index.umd.js +1 -1
- package/form-validation/readme.md +52 -27
- package/package.json +1 -1
package/form-validation/index.js
CHANGED
|
@@ -1,96 +1,104 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var A = (
|
|
1
|
+
var C = Object.defineProperty, G = Object.defineProperties;
|
|
2
|
+
var H = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var h = Object.getOwnPropertySymbols;
|
|
4
|
+
var L = Object.prototype.hasOwnProperty, M = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var A = (t, r, e) => r in t ? C(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, v = (t, r) => {
|
|
6
6
|
for (var e in r || (r = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var e of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
L.call(r, e) && A(t, e, r[e]);
|
|
8
|
+
if (h)
|
|
9
|
+
for (var e of h(r))
|
|
10
|
+
M.call(r, e) && A(t, e, r[e]);
|
|
11
|
+
return t;
|
|
12
|
+
}, w = (t, r) => G(t, H(r));
|
|
13
|
+
var y = (t, r) => {
|
|
14
|
+
var e = {};
|
|
15
|
+
for (var a in t)
|
|
16
|
+
L.call(t, a) && r.indexOf(a) < 0 && (e[a] = t[a]);
|
|
17
|
+
if (t != null && h)
|
|
18
|
+
for (var a of h(t))
|
|
19
|
+
r.indexOf(a) < 0 && M.call(t, a) && (e[a] = t[a]);
|
|
20
|
+
return e;
|
|
21
|
+
};
|
|
22
|
+
const S = "form-validation", p = "[data-validation]", J = "[data-mask]";
|
|
23
|
+
function W({
|
|
24
|
+
main: t,
|
|
16
25
|
elm: r,
|
|
17
26
|
state: e,
|
|
18
|
-
on:
|
|
19
|
-
emit:
|
|
20
|
-
dependencies:
|
|
21
|
-
trigger:
|
|
27
|
+
on: a,
|
|
28
|
+
emit: g,
|
|
29
|
+
dependencies: N,
|
|
30
|
+
trigger: O
|
|
22
31
|
}) {
|
|
23
|
-
var
|
|
24
|
-
const
|
|
25
|
-
let
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}), r.setValues = (
|
|
29
|
-
e.set((
|
|
32
|
+
var V;
|
|
33
|
+
const u = y(N, []), f = (V = r.querySelector("input,select,textarea")) == null ? void 0 : V.form;
|
|
34
|
+
let m = I(f);
|
|
35
|
+
t((s) => {
|
|
36
|
+
a("input", "input, textarea, select", j), a("input", J, K), a("input", p, b("input")), a("change", p, b("change")), a("blur", p, b("blur")), a("focus", "input, textarea, select", _), a("blur", "input, textarea, select", $), f.addEventListener("reset", T), f.addEventListener("submit", q), z();
|
|
37
|
+
}), r.setValues = (s) => {
|
|
38
|
+
e.set((o) => o.form.values = v(v({}, o.form.values), s));
|
|
30
39
|
};
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
40
|
+
const z = () => {
|
|
41
|
+
if (!u)
|
|
33
42
|
throw new Error(
|
|
34
|
-
"<form-validation> - No
|
|
43
|
+
"<form-validation> - No entities provided in dependencies"
|
|
35
44
|
);
|
|
36
|
-
const
|
|
37
|
-
e.set((
|
|
38
|
-
},
|
|
39
|
-
const
|
|
40
|
-
return
|
|
41
|
-
},
|
|
42
|
-
const
|
|
43
|
-
e.set((
|
|
44
|
-
|
|
45
|
+
const s = E();
|
|
46
|
+
e.set((o) => o.form.values = s);
|
|
47
|
+
}, E = () => {
|
|
48
|
+
const s = {};
|
|
49
|
+
return m.forEach((o) => s[o] = ""), s;
|
|
50
|
+
}, _ = (s) => {
|
|
51
|
+
const o = s.target.name;
|
|
52
|
+
e.set((n) => {
|
|
53
|
+
n.form.touched[o] = !0, n.form.focused[o] = !0;
|
|
45
54
|
});
|
|
46
|
-
},
|
|
47
|
-
const
|
|
48
|
-
e.set((
|
|
49
|
-
|
|
55
|
+
}, $ = (s) => {
|
|
56
|
+
const o = s.target.name;
|
|
57
|
+
e.set((n) => {
|
|
58
|
+
n.form.focused[o] = !1;
|
|
50
59
|
});
|
|
51
|
-
},
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
q || d.push(K);
|
|
60
|
+
}, b = (s) => (o) => {
|
|
61
|
+
const n = o.target, c = n.name, l = F(n, f), k = n.dataset.validation.split(/\s/), d = [], x = e.get();
|
|
62
|
+
k.forEach((i) => {
|
|
63
|
+
if (i in u) {
|
|
64
|
+
const D = u[i];
|
|
65
|
+
if (!D.validate(l, n, f)) {
|
|
66
|
+
const B = D.message(l, n, f);
|
|
67
|
+
d.push(B);
|
|
68
|
+
}
|
|
61
69
|
}
|
|
62
|
-
}), d.length ?
|
|
63
|
-
|
|
64
|
-
})) : (
|
|
65
|
-
|
|
66
|
-
})) : (
|
|
67
|
-
delete
|
|
70
|
+
}), d.length ? s === "input" ? (m.add(n.name), e.set((i) => {
|
|
71
|
+
i.form.isValid = !1, x.form.errors[c] && d[0] != x.form.errors[c] && (i.form.errors[c] = d[0]);
|
|
72
|
+
})) : (s === "blur" || s === "change") && (m.add(n.name), e.set((i) => {
|
|
73
|
+
i.form.errors[c] = d[0], i.form.isValid = !1;
|
|
74
|
+
})) : (m.delete(n.name), e.set((i) => {
|
|
75
|
+
delete i.form.errors[c], m.size || (i.form.isValid = !0);
|
|
68
76
|
}));
|
|
69
|
-
},
|
|
70
|
-
const { name:
|
|
71
|
-
e.set((
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
if (Object.keys(
|
|
76
|
-
|
|
77
|
+
}, j = (s) => {
|
|
78
|
+
const { name: o } = s.target, n = F(s.target, f);
|
|
79
|
+
e.set((c) => c.form.values[o] = n);
|
|
80
|
+
}, q = (s) => {
|
|
81
|
+
s.preventDefault(), O("blur", p);
|
|
82
|
+
const n = e.get().form.errors;
|
|
83
|
+
if (Object.keys(n).length)
|
|
84
|
+
g(`${S}:error`, { errors: n });
|
|
77
85
|
else {
|
|
78
|
-
const
|
|
79
|
-
|
|
86
|
+
const l = Q(s.target);
|
|
87
|
+
g(`${S}:submit`, v({}, l));
|
|
80
88
|
}
|
|
81
|
-
},
|
|
82
|
-
let
|
|
83
|
-
const { mask:
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
const
|
|
87
|
-
|
|
89
|
+
}, K = (s) => {
|
|
90
|
+
let o = s.target.value;
|
|
91
|
+
const { mask: n } = s.target.dataset;
|
|
92
|
+
n.split(/s/).forEach((l) => {
|
|
93
|
+
if (u[l] && u[l].mask) {
|
|
94
|
+
const k = u[l].mask;
|
|
95
|
+
o = k(o, s.target, s.target.form);
|
|
88
96
|
}
|
|
89
|
-
}), e.set((
|
|
90
|
-
},
|
|
91
|
-
|
|
92
|
-
form:
|
|
93
|
-
values:
|
|
97
|
+
}), e.set((l) => l.form.values[s.target.name] = o || "");
|
|
98
|
+
}, T = () => {
|
|
99
|
+
m = I(f), e.set({
|
|
100
|
+
form: w(v({}, P.form), {
|
|
101
|
+
values: E()
|
|
94
102
|
})
|
|
95
103
|
});
|
|
96
104
|
};
|
|
@@ -103,19 +111,19 @@ const P = {
|
|
|
103
111
|
isValid: !1,
|
|
104
112
|
focused: {}
|
|
105
113
|
}
|
|
106
|
-
}, Q = (
|
|
107
|
-
const r = new FormData(
|
|
108
|
-
for (let [
|
|
109
|
-
e[
|
|
114
|
+
}, Q = (t) => {
|
|
115
|
+
const r = new FormData(t), e = {};
|
|
116
|
+
for (let [a, g] of r)
|
|
117
|
+
e[a] = g;
|
|
110
118
|
return { formData: r, data: e };
|
|
111
|
-
},
|
|
112
|
-
const { name: e, type:
|
|
113
|
-
return
|
|
114
|
-
},
|
|
119
|
+
}, F = (t, r) => {
|
|
120
|
+
const { name: e, type: a } = t;
|
|
121
|
+
return a == "checkbox" ? t.checked ? t.value : "" : r[e].value;
|
|
122
|
+
}, I = (t) => {
|
|
115
123
|
const r = /* @__PURE__ */ new Set();
|
|
116
|
-
return Array.from(
|
|
124
|
+
return Array.from(t.elements).filter((e) => e.name && e.dataset.validation).forEach((e) => r.add(e.name)), r;
|
|
117
125
|
};
|
|
118
126
|
export {
|
|
119
|
-
|
|
127
|
+
W as default,
|
|
120
128
|
P as model
|
|
121
129
|
};
|
package/form-validation/index.ts
CHANGED
|
@@ -12,7 +12,7 @@ export default function formValidation({
|
|
|
12
12
|
trigger,
|
|
13
13
|
}) {
|
|
14
14
|
//
|
|
15
|
-
const {
|
|
15
|
+
const { ...entities } = dependencies
|
|
16
16
|
const form = elm.querySelector('input,select,textarea')?.form
|
|
17
17
|
let fields = getFields(form)
|
|
18
18
|
|
|
@@ -36,9 +36,9 @@ export default function formValidation({
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const initialValues = () => {
|
|
39
|
-
if (!
|
|
39
|
+
if (!entities) {
|
|
40
40
|
throw new Error(
|
|
41
|
-
'<form-validation> - No
|
|
41
|
+
'<form-validation> - No entities provided in dependencies'
|
|
42
42
|
)
|
|
43
43
|
}
|
|
44
44
|
const fields = getInitialValues()
|
|
@@ -75,13 +75,11 @@ export default function formValidation({
|
|
|
75
75
|
const currentState = state.get()
|
|
76
76
|
|
|
77
77
|
validationList.forEach((validation) => {
|
|
78
|
-
if (validation in
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
input,
|
|
82
|
-
form
|
|
83
|
-
)
|
|
78
|
+
if (validation in entities) {
|
|
79
|
+
const entity = entities[validation]
|
|
80
|
+
const ok = entity.validate( value, input, form )
|
|
84
81
|
if (!ok) {
|
|
82
|
+
const message = entity.message( value, input, form)
|
|
85
83
|
errorsList.push(message)
|
|
86
84
|
}
|
|
87
85
|
}
|
|
@@ -145,8 +143,8 @@ export default function formValidation({
|
|
|
145
143
|
const allMasks = mask.split(/s/)
|
|
146
144
|
|
|
147
145
|
allMasks.forEach((mask) => {
|
|
148
|
-
if (mask && mask
|
|
149
|
-
const fn =
|
|
146
|
+
if (entities[mask] && entities[mask].mask) {
|
|
147
|
+
const fn = entities[mask].mask
|
|
150
148
|
value = fn(value, e.target, e.target.form)
|
|
151
149
|
}
|
|
152
150
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,s){typeof exports=="object"&&typeof module!="undefined"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(t=typeof globalThis!="undefined"?globalThis:t||self,s(t["form-validation"]={}))})(this,(function(t){"use strict";var Q=Object.defineProperty,R=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var O=(t,s,n)=>s in t?Q(t,s,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[s]=n,b=(t,s)=>{for(var n in s||(s={}))T.call(s,n)&&O(t,n,s[n]);if(E)for(var n of E(s))_.call(s,n)&&O(t,n,s[n]);return t},j=(t,s)=>R(t,U(s));var F=(t,s)=>{var n={};for(var i in t)T.call(t,i)&&s.indexOf(i)<0&&(n[i]=t[i]);if(t!=null&&E)for(var i of E(t))s.indexOf(i)<0&&_.call(t,i)&&(n[i]=t[i]);return n};const s="form-validation",n="[data-validation]",i="[data-mask]";function I({main:d,elm:f,state:o,on:l,emit:k,dependencies:z,trigger:$}){var A;const g=F(z,[]),v=(A=f.querySelector("input,select,textarea"))==null?void 0:A.form;let p=D(v);d(e=>{l("input","input, textarea, select",B),l("input",i,G),l("input",n,V("input")),l("change",n,V("change")),l("blur",n,V("blur")),l("focus","input, textarea, select",K),l("blur","input, textarea, select",P),v.addEventListener("reset",H),v.addEventListener("submit",C),q()}),f.setValues=e=>{o.set(r=>r.form.values=b(b({},r.form.values),e))};const q=()=>{if(!g)throw new Error("<form-validation> - No entities provided in dependencies");const e=S();o.set(r=>r.form.values=e)},S=()=>{const e={};return p.forEach(r=>e[r]=""),e},K=e=>{const r=e.target.name;o.set(a=>{a.form.touched[r]=!0,a.form.focused[r]=!0})},P=e=>{const r=e.target.name;o.set(a=>{a.form.focused[r]=!1})},V=e=>r=>{const a=r.target,m=a.name,u=x(a,v),y=a.dataset.validation.split(/\s/),h=[],L=o.get();y.forEach(c=>{if(c in g){const w=g[c];if(!w.validate(u,a,v)){const J=w.message(u,a,v);h.push(J)}}}),h.length?e==="input"?(p.add(a.name),o.set(c=>{c.form.isValid=!1,L.form.errors[m]&&h[0]!=L.form.errors[m]&&(c.form.errors[m]=h[0])})):(e==="blur"||e==="change")&&(p.add(a.name),o.set(c=>{c.form.errors[m]=h[0],c.form.isValid=!1})):(p.delete(a.name),o.set(c=>{delete c.form.errors[m],p.size||(c.form.isValid=!0)}))},B=e=>{const{name:r}=e.target,a=x(e.target,v);o.set(m=>m.form.values[r]=a)},C=e=>{e.preventDefault(),$("blur",n);const a=o.get().form.errors;if(Object.keys(a).length)k(`${s}:error`,{errors:a});else{const u=N(e.target);k(`${s}:submit`,b({},u))}},G=e=>{let r=e.target.value;const{mask:a}=e.target.dataset;a.split(/s/).forEach(u=>{if(g[u]&&g[u].mask){const y=g[u].mask;r=y(r,e.target,e.target.form)}}),o.set(u=>u.form.values[e.target.name]=r||"")},H=()=>{p=D(v),o.set({form:j(b({},M.form),{values:S()})})}}const M={form:{errors:{},values:{},touched:{},isValid:!1,focused:{}}},N=d=>{const f=new FormData(d),o={};for(let[l,k]of f)o[l]=k;return{formData:f,data:o}},x=(d,f)=>{const{name:o,type:l}=d;return l=="checkbox"?d.checked?d.value:"":f[o].value},D=d=>{const f=new Set;return Array.from(d.elements).filter(o=>o.name&&o.dataset.validation).forEach(o=>f.add(o.name)),f};t.default=I,t.model=M,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|
|
@@ -44,9 +44,9 @@ on('form-validation:error', ({ errors }) => {})
|
|
|
44
44
|
##### main.ts
|
|
45
45
|
```ts
|
|
46
46
|
import * as formValidation from 'jails.stdlib/form-validation'
|
|
47
|
-
import
|
|
47
|
+
import * as entities from './my-entities'
|
|
48
48
|
|
|
49
|
-
jails.register('form-validation', formValidation, { ...
|
|
49
|
+
jails.register('form-validation', formValidation, { ...entities })
|
|
50
50
|
jails.start()
|
|
51
51
|
```
|
|
52
52
|
|
|
@@ -84,37 +84,62 @@ jails.start()
|
|
|
84
84
|
```
|
|
85
85
|
|
|
86
86
|
##### my-custom-rules/index.ts
|
|
87
|
+
You need to provide a map of entities that will be used to validate the kind of your form fields.
|
|
88
|
+
They all need to implement: `validate()`, `message()`. Optional: `mask()`.
|
|
89
|
+
|
|
90
|
+
##### Example
|
|
91
|
+
|
|
87
92
|
```ts
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
93
|
+
import * as formValidation from 'jails.stdlib/form-validation'
|
|
94
|
+
import { email, number, required } from './my-entities'
|
|
95
|
+
|
|
96
|
+
jails.register('form-validation', formValidation, { email, number, required })
|
|
97
|
+
jails.start()
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**./my-entities.ts**
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
export const email = {
|
|
104
|
+
validate( value, input, form ) {
|
|
105
|
+
if (!value) return true
|
|
106
|
+
if (!value.match(/(.*)@(.*)\.\w{2,}/)) return false
|
|
107
|
+
return true
|
|
108
|
+
},
|
|
109
|
+
mask() {
|
|
110
|
+
// I don't have a mask
|
|
107
111
|
},
|
|
112
|
+
message( value, input, form ) {
|
|
113
|
+
return `Invalid Email`
|
|
114
|
+
}
|
|
115
|
+
}
|
|
108
116
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
export const required = {
|
|
118
|
+
validate(value, input, form) {
|
|
119
|
+
if (!value) return false
|
|
120
|
+
return true
|
|
121
|
+
},
|
|
122
|
+
mask(value, input, form) {
|
|
123
|
+
// I don't have a mask
|
|
124
|
+
},
|
|
125
|
+
message(value, input, form) {
|
|
126
|
+
return `This field is required`
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export const number = {
|
|
131
|
+
validate(value, input, form) {
|
|
132
|
+
if (value.match(/\D/g)) return false
|
|
133
|
+
return true
|
|
134
|
+
},
|
|
135
|
+
mask(value, input, form) {
|
|
136
|
+
return value.replace(/\D/, '')
|
|
137
|
+
},
|
|
138
|
+
message() {
|
|
139
|
+
return `This field accepts only number`
|
|
113
140
|
}
|
|
114
141
|
}
|
|
115
142
|
```
|
|
116
|
-
|
|
117
|
-
To see how to inject this dependency, go back to the [usage](#usage) section.
|
|
118
143
|
|
|
119
144
|
|
|
120
145
|
##### element.setValues(...data)
|