Skip to content

Commit 7c9aa45

Browse files
committed
feat(general validators): Add new general validators: minLength, maxLength, isInRange
1 parent c4abe15 commit 7c9aa45

File tree

7 files changed

+178
-9
lines changed

7 files changed

+178
-9
lines changed

src/email-validators.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import { AbstractControl } from '@angular/common';
2+
import {Util} from './util'
23

34
export class EmailValidators {
45

56
static simple(): any {
67
return function validate(control: AbstractControl): { [key: string]: any } {
8+
if (Util.isNotPresent(control)) return undefined;
79
let pattern = '^.+@.+\\..+$';
8-
if (control.value !== '' && !new RegExp(pattern).test(control.value)) {
9-
return { 'simpleEmailRule': true };
10+
if (new RegExp(pattern).test(control.value)) {
11+
return undefined;
1012
}
11-
return undefined;
13+
return { 'simpleEmailRule': true };
1214
};
1315
}
1416

1517
// https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
1618
static normal(): any {
1719
return function validate(control: AbstractControl): { [key: string]: any } {
20+
if (Util.isNotPresent(control)) return undefined;
1821
let pattern = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
19-
if (control.value !== '' && !pattern.test(control.value)) {
20-
return { 'normalEmailRule': true };
22+
if (pattern.test(control.value)) {
23+
return undefined;
2124
}
22-
return undefined;
25+
return { 'normalEmailRule': true };
2326
};
2427
}
2528
}

src/password-validators.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { AbstractControl } from '@angular/common';
2+
import {Util} from './util'
23

34
export class PasswordValidators {
45

56
static repeatCharacterRegexRule(repeatCount: number): any {
67
return function validate(control: AbstractControl): { [key: string]: any } {
8+
if (Util.isNotPresent(control)) return undefined;
79
let repeatDec = repeatCount - 1;
810
let pattern = '([^\\x00-\\x1F])\\1{' + repeatDec + '}';
911
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
@@ -15,6 +17,7 @@ export class PasswordValidators {
1517

1618
static allowedCharacterRule(allowedChars: string[]): any {
1719
return function validate(control: AbstractControl): { [key: string]: any } {
20+
if (Util.isNotPresent(control)) return undefined;
1821
let value: string = control.value;
1922
let valid = true;
2023
let invalidChars: string[] = [];
@@ -36,6 +39,7 @@ export class PasswordValidators {
3639

3740
static alphabeticalCharacterRule(amount: number): any {
3841
return function validate(control: AbstractControl): { [key: string]: any } {
42+
if (Util.isNotPresent(control)) return undefined;
3943
let value: string = control.value;
4044
if (value.length === 0) {
4145
return undefined;
@@ -51,6 +55,7 @@ export class PasswordValidators {
5155

5256
static digitCharacterRule(amount: number): any {
5357
return function validate(control: AbstractControl): { [key: string]: any } {
58+
if (Util.isNotPresent(control)) return undefined;
5459
let value: string = control.value;
5560
if (value.length === 0) {
5661
return undefined;
@@ -66,6 +71,7 @@ export class PasswordValidators {
6671

6772
static lowercaseCharacterRule(amount: number): any {
6873
return function validate(control: AbstractControl): { [key: string]: any } {
74+
if (Util.isNotPresent(control)) return undefined;
6975
let value: string = control.value;
7076
if (value.length === 0) {
7177
return undefined;
@@ -81,6 +87,7 @@ export class PasswordValidators {
8187

8288
static uppercaseCharacterRule(amount: number): any {
8389
return function validate(control: AbstractControl): { [key: string]: any } {
90+
if (Util.isNotPresent(control)) return undefined;
8491
let value: string = control.value;
8592
if (value.length === 0) {
8693
return undefined;

src/universal-validators.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { AbstractControl } from '@angular/common';
22
import { NumberWrapper} from '@angular/core/src/facade/lang';
3+
import {Util} from './util'
4+
35
export class UniversalValidators {
46

57
static noWhitespace(): any {
68
return function validate(control: AbstractControl): { [key: string]: any } {
9+
if (Util.isNotPresent(control)) return undefined;
710
let pattern = '\\s';
8-
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
11+
if (new RegExp(pattern).test(control.value)) {
912
return { 'noWhitespaceRequired': true };
1013
}
1114
return undefined;
@@ -14,12 +17,52 @@ export class UniversalValidators {
1417

1518
static isNumber(): any {
1619
return function validate(control: AbstractControl): { [key: string]: any } {
17-
if (control.value !== '' && NumberWrapper.isNaN(control.value)) {
20+
if (Util.isNotPresent(control)) return undefined;
21+
if (NumberWrapper.isNaN(control.value)) {
1822
return { 'numberRequired': true };
1923
}
2024
return undefined;
2125
};
2226
}
2327

28+
static isInRange(minValue: number, maxValue: number): any {
29+
return function validate(control: AbstractControl): { [key: string]: any } {
30+
if (Util.isNotPresent(control)) return undefined;
31+
if (NumberWrapper.isNaN(control.value)) {
32+
return { 'numberRequired': true };
33+
}
34+
if (+control.value < minValue) {
35+
return { 'rangeValueToSmall': true };
36+
}
37+
38+
if (+control.value > maxValue) {
39+
return { 'rangeValueToBig': true };
40+
} else {
41+
return undefined;
42+
}
43+
};
44+
}
45+
46+
static minLength(minLength: number): any {
47+
return function validate(control: AbstractControl): { [key: string]: any } {
48+
if (Util.isNotPresent(control)) return undefined;
49+
let value: string = control.value;
50+
if (value.length > minLength) {
51+
return undefined;
52+
}
53+
return { 'minLength': true };
54+
};
55+
}
56+
57+
static maxLength(maxLength: number): any {
58+
return function validate(control: AbstractControl): { [key: string]: any } {
59+
if (Util.isNotPresent(control)) return undefined;
60+
let value: string = control.value;
61+
if (maxLength > value.length) {
62+
return undefined;
63+
}
64+
return { 'maxLength': true };
65+
};
66+
}
2467

2568
}

src/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AbstractControl } from '@angular/common';
2+
3+
export class Util {
4+
static isNotPresent(control: AbstractControl): boolean {
5+
return control.value !== '' ? false : true;
6+
};
7+
}
8+

tests/universal-validators.spec.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,86 @@ export function main() {
5353
});
5454

5555
});
56+
57+
describe('isInRangeRule', () => {
58+
59+
it('should work for empty control', () => {
60+
let control: Control = new Control('');
61+
let validated = UniversalValidators.isInRange(5, 10)(control);
62+
expect(validated).toBeUndefined();
63+
});
64+
65+
it('should work for valid number', () => {
66+
let control: Control = new Control('7');
67+
let validated = UniversalValidators.isInRange(5, 10)(control);
68+
expect(validated).toBeUndefined();
69+
});
70+
71+
it('should work for invalid number small', () => {
72+
let control: Control = new Control('4');
73+
let validated = UniversalValidators.isInRange(5, 10)(control);
74+
expect(validated).toEqual({ 'rangeValueToSmall': true });
75+
});
76+
77+
it('should work for invalid number big', () => {
78+
let control: Control = new Control('11');
79+
let validated = UniversalValidators.isInRange(5, 10)(control);
80+
expect(validated).toEqual({ 'rangeValueToBig': true });
81+
});
82+
83+
it('should work for invalid input', () => {
84+
let control: Control = new Control('sdsd');
85+
let validated = UniversalValidators.isInRange(5, 10)(control);
86+
expect(validated).toEqual({ 'numberRequired': true });
87+
});
88+
89+
});
90+
91+
describe('minLength', () => {
92+
93+
it('should work for empty control', () => {
94+
let control: Control = new Control('');
95+
let validated = UniversalValidators.minLength(2)(control);
96+
expect(validated).toBeUndefined();
97+
});
98+
99+
it('should work for valid length', () => {
100+
let control: Control = new Control('453');
101+
let validated = UniversalValidators.minLength(2)(control);
102+
expect(validated).toBeUndefined();
103+
});
104+
105+
it('should work for invalid length', () => {
106+
let control: Control = new Control('abbccc');
107+
let validated = UniversalValidators.minLength(6)(control);
108+
expect(validated).toEqual({ 'minLength': true });
109+
});
110+
111+
});
112+
113+
describe('maxLength', () => {
114+
115+
it('should work for empty control', () => {
116+
let control: Control = new Control('');
117+
let validated = UniversalValidators.maxLength(2)(control);
118+
expect(validated).toBeUndefined();
119+
});
120+
121+
it('should work for valid length', () => {
122+
let control: Control = new Control('453');
123+
let validated = UniversalValidators.maxLength(4)(control);
124+
expect(validated).toBeUndefined();
125+
});
126+
127+
it('should work for invalid length', () => {
128+
let control: Control = new Control('abbccc');
129+
let validated = UniversalValidators.maxLength(2)(control);
130+
expect(validated).toEqual({ 'maxLength': true });
131+
});
132+
133+
});
134+
135+
136+
56137
});
57138
}

tests/util.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {Util} from '../src/util';
2+
import {Control} from '@angular/common';
3+
4+
export function main() {
5+
6+
describe('Util service', () => {
7+
8+
describe('isNotPresent', () => {
9+
10+
it('should work for empty control', () => {
11+
let control: Control = new Control('');
12+
let validated = Util.isNotPresent(control);
13+
expect(validated).toBeTruthy();
14+
});
15+
16+
it('should work for control with text', () => {
17+
let control: Control = new Control('aaabbbccc');
18+
let validated = Util.isNotPresent(control);
19+
expect(validated).toBeFalsy();
20+
});
21+
22+
});
23+
24+
});
25+
}

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"./src/email-validators.ts",
1919
"./tests/email-validators.spec.ts",
2020
"./src/universal-validators.ts",
21-
"./tests/universal-validators.spec.ts"
21+
"./tests/universal-validators.spec.ts",
22+
"./src/util.ts",
23+
"./tests/util.spec.ts"
2224
]
2325
}

0 commit comments

Comments
 (0)