@cocreate/validation 1.14.0 → 1.15.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/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.14.1...v1.15.0) (2023-12-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * initialize length and email validations ([7cace75](https://github.com/CoCreate-app/CoCreate-validation/commit/7cace75c62a054ce23a7f8f03dc68736387ef2bb))
7
+ * render property ([93e3040](https://github.com/CoCreate-app/CoCreate-validation/commit/93e3040d12407ea95693f2fbd6544dd6bf70b4fb))
8
+
9
+
10
+ ### Features
11
+
12
+ * email validation ([c950a32](https://github.com/CoCreate-app/CoCreate-validation/commit/c950a3215536eef27f1110d3fe10466c8d9c346e))
13
+ * length validation ([591b2b8](https://github.com/CoCreate-app/CoCreate-validation/commit/591b2b811d597a909c6d0eb0020ca6857224d66d))
14
+
15
+ ## [1.14.1](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.14.0...v1.14.1) (2023-12-15)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * render handling ([d538633](https://github.com/CoCreate-app/CoCreate-validation/commit/d538633867b7b8ab4d7b8d07b4a0ba866908afa0))
21
+
1
22
  # [1.14.0](https://github.com/CoCreate-app/CoCreate-validation/compare/v1.13.0...v1.14.0) (2023-11-25)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/validation",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
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",
package/src/index.js CHANGED
@@ -12,14 +12,18 @@ import { matches } from './validations/matches';
12
12
  import { required } from './validations/required';
13
13
  import { unique } from './validations/unique';
14
14
  import { subdomain } from './validations/subdomain';
15
+ import { length } from './validations/length';
16
+ import { email } from './validations/email';
15
17
 
16
18
  const validations = new Map();
17
19
  validations.set('matches', matches);
18
20
  validations.set('required', required);
19
21
  validations.set('subdomain', subdomain);
20
22
  validations.set('unique', unique);
23
+ validations.set('length', length);
24
+ validations.set('email', email);
21
25
 
22
- const selectors = '[matches], [required], [unique], [subdomain]';
26
+ const selectors = '[matches], [required], [unique], [subdomain], [length], [email]';
23
27
 
24
28
  async function validate(btn, params) {
25
29
  let validateElements;
@@ -0,0 +1,31 @@
1
+ import { render } from '@cocreate/render';
2
+
3
+ export var email = async (elements) => {
4
+ let failedElements = [];
5
+ for (let element of elements) {
6
+ let length = element.getAttribute('length') || 0
7
+ length = parseFloat(length)
8
+ let value = await element.getValue();
9
+ let isValidEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(value);
10
+
11
+ if (isValidEmail) {
12
+ element.setAttribute('validation', 'true');
13
+ } else {
14
+ element.setAttribute('validation', 'false');
15
+ failedElements.push(element);
16
+ }
17
+ }
18
+ if (failedElements.length) {
19
+ render({
20
+ selector: "[template='validate']",
21
+ data: [{
22
+ type: 'email',
23
+ status: 'failed',
24
+ message: 'Please enter a valid email address',
25
+ selector: '[required][validation="false"]',
26
+ elements: failedElements
27
+ }]
28
+ });
29
+ return failedElements;
30
+ }
31
+ };
@@ -0,0 +1,31 @@
1
+ import { render } from '@cocreate/render';
2
+
3
+ export var length = async (elements) => {
4
+ let failedElements = [];
5
+ for (let element of elements) {
6
+ let length = element.getAttribute('length') || 0
7
+ length = parseFloat(length)
8
+ let value = await element.getValue();
9
+ if (element.type === 'password')
10
+ value = atob(value)
11
+ if (value.length >= length) {
12
+ element.setAttribute('validation', 'true');
13
+ } else {
14
+ element.setAttribute('validation', 'false');
15
+ failedElements.push(element);
16
+ }
17
+ }
18
+ if (failedElements.length) {
19
+ render({
20
+ selector: "[template='validate']",
21
+ data: [{
22
+ type: 'length',
23
+ status: 'failed',
24
+ message: 'Does not meet the minimum character length requirement',
25
+ selector: '[required][validation="false"]',
26
+ elements: failedElements
27
+ }]
28
+ });
29
+ return failedElements;
30
+ }
31
+ };
@@ -38,13 +38,13 @@ function matches(elements) {
38
38
  if (failedElements.length) {
39
39
  render({
40
40
  selector: "[template='validate']",
41
- data: {
41
+ data: [{
42
42
  type: 'matches',
43
43
  status: 'failed',
44
44
  message: 'One or more values do not match',
45
45
  selector: '[matches][validation="false"]',
46
46
  elements: failedElements
47
- }
47
+ }]
48
48
  });
49
49
  return failedElements;
50
50
  }
@@ -17,13 +17,13 @@ export var required = async (elements) => {
17
17
  if (failedElements.length) {
18
18
  render({
19
19
  selector: "[template='validate']",
20
- data: {
20
+ data: [{
21
21
  type: 'unique',
22
22
  status: 'failed',
23
23
  message: 'One or more required fields are missing values',
24
24
  selector: '[required][validation="false"]',
25
25
  elements: failedElements
26
- }
26
+ }]
27
27
  });
28
28
  return failedElements;
29
29
  }
@@ -48,13 +48,13 @@ function subdomain(elements) {
48
48
  if (failedElements.length) {
49
49
  render({
50
50
  selector: "[template='validate']",
51
- data: {
51
+ data: [{
52
52
  type: 'subdomain',
53
53
  status: 'failed',
54
54
  message: 'Enter a valid subdomain. subdomain can only be one level.',
55
55
  selector: '[subdomain][validation="false"]',
56
56
  elements: failedElements
57
- }
57
+ }]
58
58
  });
59
59
  return failedElements;
60
60
  }
@@ -11,13 +11,13 @@ export const unique = (elements) => {
11
11
  if (failedElements.length) {
12
12
  render({
13
13
  selector: "[template='validate']",
14
- data: {
14
+ data: [{
15
15
  type: 'unique',
16
16
  status: 'failed',
17
17
  message: 'One or more values are not unique',
18
18
  selector: '[unique="false"]',
19
19
  elements: failedElements
20
- }
20
+ }]
21
21
  });
22
22
  return failedElements;
23
23
  }