@cocreate/validation 1.3.5 → 1.4.3
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 +28 -0
- package/docs/index.html +2 -10
- package/package.json +5 -5
- package/src/index.js +49 -21
- package/src/validations/matches.js +55 -0
- package/src/validations/required.js +30 -0
- package/src/validations/subdomain.js +35 -5
- package/src/validations/unique.js +33 -0
- package/src/validations/equalTo.js +0 -25
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [1.4.3](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.4.2...v1.4.3) (2021-11-20)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* updated script tags and link tags ([d2adf65](https://github.com/CoCreate-app/CoCreate-validation/commit/d2adf6588fa6ec6b78f4a8e13d6a3529e1c736ae))
|
|
7
|
+
|
|
8
|
+
## [1.4.2](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.4.1...v1.4.2) (2021-11-20)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* update dependencies ([785f913](https://github.com/CoCreate-app/CoCreate-validation/commit/785f9135d4939752ed70d47c848a45d4a9baad67))
|
|
14
|
+
|
|
15
|
+
## [1.4.1](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.4.0...v1.4.1) (2021-11-19)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* update dependencies ([621dde6](https://github.com/CoCreate-app/CoCreate-validation/commit/621dde6d78a5a00e34eecc195d66a3738e43a7b3))
|
|
21
|
+
|
|
22
|
+
# [1.4.0](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.3.5...v1.4.0) (2021-11-19)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* support for required inputs ([6c89b25](https://github.com/CoCreate-app/CoCreate-validation/commit/6c89b25eee80cac4ede435a0c69ebfb8f8ffa0a3))
|
|
28
|
+
|
|
1
29
|
## [1.3.5](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.3.4...v1.3.5) (2021-11-18)
|
|
2
30
|
|
|
3
31
|
|
package/docs/index.html
CHANGED
|
@@ -112,15 +112,7 @@
|
|
|
112
112
|
|
|
113
113
|
</div>
|
|
114
114
|
</div>
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
apiKey: 'c2b08663-06e3-440c-ef6f-13978b42883a',
|
|
118
|
-
securityKey: 'f26baf68-e3a9-45fc-effe-502e47116265',
|
|
119
|
-
organization_Id: '5de0387b12e200ea63204d6c'
|
|
120
|
-
}
|
|
121
|
-
</script>
|
|
122
|
-
|
|
123
|
-
<!--CoCreateJS-->
|
|
124
|
-
<script src="https://server.cocreate.app/js/CoCreate.min.js"></script>
|
|
115
|
+
<script src="/apikey.js"></script>
|
|
116
|
+
<script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script>
|
|
125
117
|
</body>
|
|
126
118
|
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/validation",
|
|
3
|
-
"version": "1.3
|
|
3
|
+
"version": "1.4.3",
|
|
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",
|
|
@@ -61,9 +61,9 @@
|
|
|
61
61
|
"webpack-log": "^3.0.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@cocreate/action": "^1.3.
|
|
65
|
-
"@cocreate/docs": "^1.2.
|
|
66
|
-
"@cocreate/hosting": "^1.2.
|
|
67
|
-
"@cocreate/render": "^1.3.
|
|
64
|
+
"@cocreate/action": "^1.3.15",
|
|
65
|
+
"@cocreate/docs": "^1.2.53",
|
|
66
|
+
"@cocreate/hosting": "^1.2.48",
|
|
67
|
+
"@cocreate/render": "^1.3.16"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/src/index.js
CHANGED
|
@@ -7,42 +7,69 @@
|
|
|
7
7
|
|
|
8
8
|
/*globals CustomEvent*/
|
|
9
9
|
import action from '@cocreate/action';
|
|
10
|
-
import render from '@cocreate/render';
|
|
11
|
-
import './validations/
|
|
12
|
-
import './validations/
|
|
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';
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
const validations = new Map();
|
|
17
|
+
validations.set('matches', matches);
|
|
18
|
+
validations.set('required', required);
|
|
19
|
+
validations.set('subdomain', subdomain);
|
|
20
|
+
validations.set('unique', unique);
|
|
21
|
+
|
|
22
|
+
const selectors = '[matches], [required], [unique], [subdomain]';
|
|
23
|
+
|
|
24
|
+
function validate(btn) {
|
|
15
25
|
let validateElements;
|
|
26
|
+
let validateSelectors = [];
|
|
27
|
+
let failedElements = [];
|
|
16
28
|
|
|
17
29
|
if (btn.actionParams.size != 0){
|
|
18
30
|
let selector = btn.actionParams.get('validate');
|
|
19
|
-
|
|
31
|
+
for (let [validation] of validations){
|
|
32
|
+
let vSelctor = `${selector}[${validation}]`;
|
|
33
|
+
validateSelectors.push(vSelctor);
|
|
34
|
+
}
|
|
35
|
+
let validSelectors = validateSelectors.join(', ');
|
|
36
|
+
validateElements = document.querySelectorAll(validSelectors);
|
|
20
37
|
if (!validateElements.length){
|
|
21
38
|
let element = document.querySelector(selector);
|
|
22
|
-
|
|
39
|
+
if (element)
|
|
40
|
+
validateElements = element.querySelectorAll(selectors);
|
|
23
41
|
}
|
|
24
42
|
} else {
|
|
25
43
|
let element = btn.closest('form');
|
|
26
|
-
validateElements = element.querySelectorAll(
|
|
44
|
+
validateElements = element.querySelectorAll(selectors);
|
|
27
45
|
}
|
|
28
46
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
|
|
48
|
+
for (let [validation, func] of validations){
|
|
49
|
+
let elements = [];
|
|
50
|
+
for (let element of validateElements){
|
|
51
|
+
// let validationSelectors = element.getAttribute('validate').replace(/ /g, '').split(',');
|
|
52
|
+
if (element.hasAttribute(validation)){
|
|
53
|
+
elements.push(element);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (elements.length){
|
|
57
|
+
let failedEls = func(elements);
|
|
58
|
+
if (failedEls)
|
|
59
|
+
failedElements.push(failedEls);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (let element of validateElements){
|
|
63
|
+
let isValid = element.getAttribute('validation');
|
|
64
|
+
if (isValid == 'false')
|
|
65
|
+
failedElements.push(element);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (failedElements.length == 0){
|
|
42
69
|
document.dispatchEvent(new CustomEvent('validate', {
|
|
43
70
|
detail: {}
|
|
44
71
|
}));
|
|
45
|
-
}
|
|
72
|
+
}
|
|
46
73
|
}
|
|
47
74
|
|
|
48
75
|
action.init({
|
|
@@ -53,3 +80,4 @@ action.init({
|
|
|
53
80
|
}
|
|
54
81
|
});
|
|
55
82
|
|
|
83
|
+
export default {validations};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import render from '@cocreate/render';
|
|
2
|
+
|
|
3
|
+
function initMatches(){
|
|
4
|
+
let elements = document.querySelectorAll('[matches]');
|
|
5
|
+
for (let element of elements){
|
|
6
|
+
let targetSelector = element.getAttribute('matches');
|
|
7
|
+
let target = document.querySelector(targetSelector);
|
|
8
|
+
element.validations = {matches: target};
|
|
9
|
+
element.addEventListener('input', validateMatchEvent);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function validateMatchEvent(e){
|
|
14
|
+
validateMatch(e.target);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validateMatch(element){
|
|
18
|
+
// let element = e.target;
|
|
19
|
+
let target = element.validations.matches;
|
|
20
|
+
if (target.value == element.value){
|
|
21
|
+
target.setAttribute('validation', 'true');
|
|
22
|
+
element.setAttribute('validation', 'true');
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
target.setAttribute('validation', 'false');
|
|
26
|
+
element.setAttribute('validation', 'false');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function matches(elements){
|
|
31
|
+
let failedElements = [];
|
|
32
|
+
for (let element of elements) {
|
|
33
|
+
let isValid = element.getAttribute('validation');
|
|
34
|
+
if (isValid == 'false'){
|
|
35
|
+
failedElements.push(element);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (failedElements.length){
|
|
39
|
+
render.data({
|
|
40
|
+
selector: "[template_id='validate']",
|
|
41
|
+
data: {
|
|
42
|
+
type: 'matches',
|
|
43
|
+
status: 'failed',
|
|
44
|
+
message: 'One or more values do not match',
|
|
45
|
+
selector: '[matches][validation="false"]',
|
|
46
|
+
elements: failedElements
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return failedElements;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
initMatches();
|
|
54
|
+
|
|
55
|
+
export {matches};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// import {validations} from '../index.js';
|
|
2
|
+
import render from '@cocreate/render';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export var required = (elements) => {
|
|
6
|
+
let failedElements = [];
|
|
7
|
+
for (let element of elements){
|
|
8
|
+
let value = element.getValue(element);
|
|
9
|
+
if (value){
|
|
10
|
+
element.setAttribute('validation', 'true');
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
element.setAttribute('validation', 'false');
|
|
14
|
+
failedElements.push(element);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (failedElements.length){
|
|
18
|
+
render.data({
|
|
19
|
+
selector: "[template_id='validate']",
|
|
20
|
+
data: {
|
|
21
|
+
type: 'unique',
|
|
22
|
+
status: 'failed',
|
|
23
|
+
message: 'One or more required fields are missing values',
|
|
24
|
+
selector: '[required][validation="false"]',
|
|
25
|
+
elements: failedElements
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return failedElements;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import render from '@cocreate/render';
|
|
2
|
+
|
|
3
|
+
function initSubdomain(){
|
|
2
4
|
let elements = document.querySelectorAll('[subdomain]');
|
|
3
5
|
for (let element of elements){
|
|
4
|
-
element.addEventListener('input',
|
|
6
|
+
element.addEventListener('input', validateSubdomainEvent);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
+
function validateSubdomainEvent(e){
|
|
11
|
+
validateSubdomain(e.target);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function validateSubdomain(element){
|
|
10
15
|
let array = element.getValue(element).split('.').length -1;
|
|
11
16
|
if (array == 2){
|
|
12
17
|
element.setAttribute('validation', 'true');
|
|
@@ -16,4 +21,29 @@ function validateSubdomain(e){
|
|
|
16
21
|
}
|
|
17
22
|
}
|
|
18
23
|
|
|
19
|
-
subdomain()
|
|
24
|
+
function subdomain(elements){
|
|
25
|
+
let failedElements = [];
|
|
26
|
+
for (let element of elements) {
|
|
27
|
+
let isValid = element.getAttribute('validation');
|
|
28
|
+
if (isValid == 'false'){
|
|
29
|
+
failedElements.push(element);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (failedElements.length){
|
|
33
|
+
render.data({
|
|
34
|
+
selector: "[template_id='validate']",
|
|
35
|
+
data: {
|
|
36
|
+
type: 'subdomain',
|
|
37
|
+
status: 'failed',
|
|
38
|
+
message: 'Enter a valid subdomain. subdomain can only be one level.',
|
|
39
|
+
selector: '[subdomain][validation="false"]',
|
|
40
|
+
elements: failedElements
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return failedElements;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
initSubdomain();
|
|
48
|
+
|
|
49
|
+
export {subdomain};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import render from '@cocreate/render';
|
|
2
|
+
|
|
3
|
+
export var unique = (elements) => {
|
|
4
|
+
let failedElements = [];
|
|
5
|
+
for (let element of elements){
|
|
6
|
+
let unique = element.getAttribute('unique');
|
|
7
|
+
if (unique == 'false'){
|
|
8
|
+
failedElements.push(element);
|
|
9
|
+
}
|
|
10
|
+
// if (unique != 'false'){
|
|
11
|
+
// element.setAttribute('validation', 'true');
|
|
12
|
+
// }
|
|
13
|
+
// else {
|
|
14
|
+
// element.setAttribute('validation', 'false');
|
|
15
|
+
// failedElements.push(element);
|
|
16
|
+
// }
|
|
17
|
+
}
|
|
18
|
+
if (failedElements.length){
|
|
19
|
+
render.data({
|
|
20
|
+
selector: "[template_id='validate']",
|
|
21
|
+
data: {
|
|
22
|
+
type: 'unique',
|
|
23
|
+
status: 'failed',
|
|
24
|
+
message: 'One or more values are not unique',
|
|
25
|
+
selector: '[unique="false"]',
|
|
26
|
+
elements: failedElements
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return failedElements;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// unique();
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export function equalTo(){
|
|
2
|
-
let elements = document.querySelectorAll('[equal-to]');
|
|
3
|
-
for (let element of elements){
|
|
4
|
-
let targetSelector = element.getAttribute('equal-to');
|
|
5
|
-
let target = document.querySelector(targetSelector);
|
|
6
|
-
element.validations = {isEqualTo: target};
|
|
7
|
-
element.addEventListener('input', validateEqual);
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function validateEqual(e){
|
|
13
|
-
let element = e.target;
|
|
14
|
-
let target = element.validations.isEqualTo;
|
|
15
|
-
if (target.value == element.value){
|
|
16
|
-
target.setAttribute('validation', 'true');
|
|
17
|
-
element.setAttribute('validation', 'true');
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
target.setAttribute('validation', 'false');
|
|
21
|
-
element.setAttribute('validation', 'false');
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
equalTo();
|