@cocreate/validation 1.16.0 → 1.16.1
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +66 -65
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.16.1](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.16.0...v1.16.1) (2025-04-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* dispatch end event on action element rather than document ([e415c5f](https://github.com/CoCreate-app/CoCreate-validation/commit/e415c5fb8da8e1892cdb530d606c8453e2f1d158))
|
|
7
|
+
|
|
1
8
|
# [1.16.0](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.15.6...v1.16.0) (2024-11-04)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -6,81 +6,82 @@
|
|
|
6
6
|
********************************************************************************/
|
|
7
7
|
|
|
8
8
|
/*globals CustomEvent*/
|
|
9
|
-
import actions from
|
|
9
|
+
import actions from "@cocreate/actions";
|
|
10
10
|
// import render from '@cocreate/render';
|
|
11
|
-
import { matches } from
|
|
12
|
-
import { required } from
|
|
13
|
-
import { unique } from
|
|
14
|
-
import { subdomain } from
|
|
15
|
-
import { length } from
|
|
16
|
-
import { email } from
|
|
11
|
+
import { matches } from "./validations/matches";
|
|
12
|
+
import { required } from "./validations/required";
|
|
13
|
+
import { unique } from "./validations/unique";
|
|
14
|
+
import { subdomain } from "./validations/subdomain";
|
|
15
|
+
import { length } from "./validations/length";
|
|
16
|
+
import { email } from "./validations/email";
|
|
17
17
|
|
|
18
18
|
const validations = new Map();
|
|
19
|
-
validations.set(
|
|
20
|
-
validations.set(
|
|
21
|
-
validations.set(
|
|
22
|
-
validations.set(
|
|
23
|
-
validations.set(
|
|
24
|
-
validations.set(
|
|
19
|
+
validations.set("matches", matches);
|
|
20
|
+
validations.set("required", required);
|
|
21
|
+
validations.set("subdomain", subdomain);
|
|
22
|
+
validations.set("unique", unique);
|
|
23
|
+
validations.set("length", length);
|
|
24
|
+
validations.set("email", email);
|
|
25
25
|
|
|
26
|
-
const selectors =
|
|
26
|
+
const selectors =
|
|
27
|
+
"[matches], [required], [unique], [subdomain], [length], [email]";
|
|
27
28
|
|
|
28
|
-
async function validate(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
async function validate(action) {
|
|
30
|
+
let btn = action.element;
|
|
31
|
+
let params = action.params;
|
|
32
|
+
let validateElements;
|
|
33
|
+
let validateSelectors = [];
|
|
34
|
+
let failedElements = [];
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
36
|
+
if (params) {
|
|
37
|
+
let selector = params;
|
|
38
|
+
for (let [validation] of validations) {
|
|
39
|
+
let vSelctor = `${selector}[${validation}]`;
|
|
40
|
+
validateSelectors.push(vSelctor);
|
|
41
|
+
}
|
|
42
|
+
let validSelectors = validateSelectors.join(", ");
|
|
43
|
+
validateElements = document.querySelectorAll(validSelectors);
|
|
44
|
+
if (!validateElements.length) {
|
|
45
|
+
let element = document.querySelector(selector);
|
|
46
|
+
if (element) validateElements = element.querySelectorAll(selectors);
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
let element = btn.closest("form");
|
|
50
|
+
validateElements = element.querySelectorAll(selectors);
|
|
51
|
+
}
|
|
50
52
|
|
|
53
|
+
for (let [validation, func] of validations) {
|
|
54
|
+
let elements = [];
|
|
55
|
+
for (let element of validateElements) {
|
|
56
|
+
// let validationSelectors = element.getAttribute('validate').replace(/ /g, '').split(',');
|
|
57
|
+
if (element.hasAttribute(validation)) {
|
|
58
|
+
elements.push(element);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (elements.length) {
|
|
62
|
+
let failedEls = await func(elements);
|
|
63
|
+
if (failedEls) failedElements.push(failedEls);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (let element of validateElements) {
|
|
67
|
+
let isValid = element.getAttribute("validation");
|
|
68
|
+
if (isValid == "false") failedElements.push(element);
|
|
69
|
+
}
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
if (elements.length) {
|
|
61
|
-
let failedEls = await func(elements);
|
|
62
|
-
if (failedEls)
|
|
63
|
-
failedElements.push(failedEls);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
for (let element of validateElements) {
|
|
67
|
-
let isValid = element.getAttribute('validation');
|
|
68
|
-
if (isValid == 'false')
|
|
69
|
-
failedElements.push(element);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (failedElements.length == 0) {
|
|
73
|
-
document.dispatchEvent(new CustomEvent('validate', {
|
|
74
|
-
detail: {}
|
|
75
|
-
}));
|
|
76
|
-
}
|
|
71
|
+
if (failedElements.length == 0) {
|
|
72
|
+
btn.dispatchEvent(
|
|
73
|
+
new CustomEvent("validate", {
|
|
74
|
+
detail: {}
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
}
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
actions.init({
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
name: "validate",
|
|
82
|
+
callback: (action) => {
|
|
83
|
+
validate(action);
|
|
84
|
+
}
|
|
84
85
|
});
|
|
85
86
|
|
|
86
|
-
export default { validations };
|
|
87
|
+
export default { validations };
|