@cocreate/validation 1.15.6 → 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 CHANGED
@@ -1,3 +1,22 @@
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
+
8
+ # [1.16.0](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.15.6...v1.16.0) (2024-11-04)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * pretier.config.js and file formating ([4638484](https://github.com/CoCreate-app/CoCreate-validation/commit/463848483a70362248f3ed63195c6a81f78e205a))
14
+
15
+
16
+ ### Features
17
+
18
+ * add prettier.config.js and format files ([421caf7](https://github.com/CoCreate-app/CoCreate-validation/commit/421caf7b0b58af8efe9f764e49601096e6ce8a1d))
19
+
1
20
  ## [1.15.6](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.15.5...v1.15.6) (2024-06-12)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/validation",
3
- "version": "1.15.6",
3
+ "version": "1.16.1",
4
4
  "description": "A simple validation component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "validation",
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ tabWidth: 4,
3
+ semi: true,
4
+ trailingComma: "none",
5
+ bracketSameLine: true,
6
+ useTabs: true,
7
+ overrides: [
8
+ {
9
+ files: ["*.json", "*.yml", "*.yaml"],
10
+ options: {
11
+ tabWidth: 2,
12
+ useTabs: false
13
+ },
14
+ }
15
+ ],
16
+ };
package/src/index.js CHANGED
@@ -6,81 +6,82 @@
6
6
  ********************************************************************************/
7
7
 
8
8
  /*globals CustomEvent*/
9
- import actions from '@cocreate/actions';
9
+ import actions from "@cocreate/actions";
10
10
  // import render from '@cocreate/render';
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';
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('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);
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 = '[matches], [required], [unique], [subdomain], [length], [email]';
26
+ const selectors =
27
+ "[matches], [required], [unique], [subdomain], [length], [email]";
27
28
 
28
- async function validate(btn, params) {
29
- let validateElements;
30
- let validateSelectors = [];
31
- let failedElements = [];
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
- if (params) {
34
- let selector = params
35
- for (let [validation] of validations) {
36
- let vSelctor = `${selector}[${validation}]`;
37
- validateSelectors.push(vSelctor);
38
- }
39
- let validSelectors = validateSelectors.join(', ');
40
- validateElements = document.querySelectorAll(validSelectors);
41
- if (!validateElements.length) {
42
- let element = document.querySelector(selector);
43
- if (element)
44
- validateElements = element.querySelectorAll(selectors);
45
- }
46
- } else {
47
- let element = btn.closest('form');
48
- validateElements = element.querySelectorAll(selectors);
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
- for (let [validation, func] of validations) {
53
- let elements = [];
54
- for (let element of validateElements) {
55
- // let validationSelectors = element.getAttribute('validate').replace(/ /g, '').split(',');
56
- if (element.hasAttribute(validation)) {
57
- elements.push(element);
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
- name: "validate",
81
- callback: (action) => {
82
- validate(action.element, action.params);
83
- }
81
+ name: "validate",
82
+ callback: (action) => {
83
+ validate(action);
84
+ }
84
85
  });
85
86
 
86
- export default { validations };
87
+ export default { validations };