Skip to content

Commit 820b724

Browse files
committed
Merge pull request #1 from Nightapes/feat-general-validators
General validators
2 parents 549712f + f04b04c commit 820b724

13 files changed

+404
-60
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,28 @@ The rules are from https://github.com/vt-middleware/passay
1414
The password validators are:
1515

1616
* repeatCharacterRegexRule
17-
* whitespaceRule
17+
* whitespaceRule (moved to UniversalValidators)
1818
* allowedCharacterRule
1919
* alphabeticalCharacterRule
2020
* digitCharacterRule
2121
* lowercaseCharacterRule
2222
* uppercaseCharacterRule
2323
* more will come
2424

25-
## Email
25+
## Email validators
2626

2727
* simple (only checks if it looks like a mail)
2828
* normal (follows the [HTML5](https://www.w3.org/TR/html5/forms.html#valid-e-mail-address) rules)
2929

30+
## Universal validators
31+
32+
* noWhitespace
33+
* isNumber
34+
* isInRange
35+
* minLength
36+
* maxLength
37+
38+
3039
## Install
3140

3241
```
@@ -47,13 +56,18 @@ password: Control = new Control('', Validators.compose([
4756
email: Control = new Control('', EmailValidators.normal());
4857
```
4958

59+
```
60+
text: Control = new Control('', UniversalValidators.minLength(2));
61+
```
62+
63+
5064
##Todo
5165

5266
* Implement https://github.com/mailcheck/mailcheck
5367
* Add more password rules
54-
* Add general validators
68+
* Add credit card validators
69+
* Add phone validators
5570
* Add address validator
56-
* Add releases
5771

5872
Get the complete changelog here: https://github.com/Nightapes/ng2-validators/releases
5973

bundles/ng2-validators.js

Lines changed: 122 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
System.registerDynamic("src/password-validators", [], true, function($__require, exports, module) {
1+
System.registerDynamic("src/password-validators", ["./util"], true, function($__require, exports, module) {
22
"use strict";
33
;
44
var define,
55
global = this,
66
GLOBAL = this;
7+
var util_1 = $__require('./util');
78
var PasswordValidators = (function() {
89
function PasswordValidators() {}
910
PasswordValidators.repeatCharacterRegexRule = function(repeatCount) {
1011
return function validate(control) {
12+
if (util_1.Util.isNotPresent(control))
13+
return undefined;
1114
var repeatDec = repeatCount - 1;
1215
var pattern = '([^\\x00-\\x1F])\\1{' + repeatDec + '}';
1316
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
@@ -16,17 +19,10 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
1619
return undefined;
1720
};
1821
};
19-
PasswordValidators.whitespaceRule = function() {
20-
return function validate(control) {
21-
var pattern = '\\s';
22-
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
23-
return {'whitespaceRule': true};
24-
}
25-
return undefined;
26-
};
27-
};
2822
PasswordValidators.allowedCharacterRule = function(allowedChars) {
2923
return function validate(control) {
24+
if (util_1.Util.isNotPresent(control))
25+
return undefined;
3026
var value = control.value;
3127
var valid = true;
3228
var invalidChars = [];
@@ -48,6 +44,8 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
4844
};
4945
PasswordValidators.alphabeticalCharacterRule = function(amount) {
5046
return function validate(control) {
47+
if (util_1.Util.isNotPresent(control))
48+
return undefined;
5149
var value = control.value;
5250
if (value.length === 0) {
5351
return undefined;
@@ -62,6 +60,8 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
6260
};
6361
PasswordValidators.digitCharacterRule = function(amount) {
6462
return function validate(control) {
63+
if (util_1.Util.isNotPresent(control))
64+
return undefined;
6565
var value = control.value;
6666
if (value.length === 0) {
6767
return undefined;
@@ -76,6 +76,8 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
7676
};
7777
PasswordValidators.lowercaseCharacterRule = function(amount) {
7878
return function validate(control) {
79+
if (util_1.Util.isNotPresent(control))
80+
return undefined;
7981
var value = control.value;
8082
if (value.length === 0) {
8183
return undefined;
@@ -90,6 +92,8 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
9092
};
9193
PasswordValidators.uppercaseCharacterRule = function(amount) {
9294
return function validate(control) {
95+
if (util_1.Util.isNotPresent(control))
96+
return undefined;
9397
var value = control.value;
9498
if (value.length === 0) {
9599
return undefined;
@@ -108,30 +112,35 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
108112
return module.exports;
109113
});
110114

111-
System.registerDynamic("src/email-validators", [], true, function($__require, exports, module) {
115+
System.registerDynamic("src/email-validators", ["./util"], true, function($__require, exports, module) {
112116
"use strict";
113117
;
114118
var define,
115119
global = this,
116120
GLOBAL = this;
121+
var util_1 = $__require('./util');
117122
var EmailValidators = (function() {
118123
function EmailValidators() {}
119124
EmailValidators.simple = function() {
120125
return function validate(control) {
126+
if (util_1.Util.isNotPresent(control))
127+
return undefined;
121128
var pattern = '^.+@.+\\..+$';
122-
if (control.value !== '' && !new RegExp(pattern).test(control.value)) {
123-
return {'simpleEmailRule': true};
129+
if (new RegExp(pattern).test(control.value)) {
130+
return undefined;
124131
}
125-
return undefined;
132+
return {'simpleEmailRule': true};
126133
};
127134
};
128135
EmailValidators.normal = function() {
129136
return function validate(control) {
137+
if (util_1.Util.isNotPresent(control))
138+
return undefined;
130139
var 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])?)*$/;
131-
if (control.value !== '' && !pattern.test(control.value)) {
132-
return {'normalEmailRule': true};
140+
if (pattern.test(control.value)) {
141+
return undefined;
133142
}
134-
return undefined;
143+
return {'normalEmailRule': true};
135144
};
136145
};
137146
return EmailValidators;
@@ -140,7 +149,101 @@ System.registerDynamic("src/email-validators", [], true, function($__require, ex
140149
return module.exports;
141150
});
142151

143-
System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/email-validators"], true, function($__require, exports, module) {
152+
System.registerDynamic("src/util", [], true, function($__require, exports, module) {
153+
"use strict";
154+
;
155+
var define,
156+
global = this,
157+
GLOBAL = this;
158+
var Util = (function() {
159+
function Util() {}
160+
Util.isNotPresent = function(control) {
161+
return control.value !== '' ? false : true;
162+
};
163+
;
164+
return Util;
165+
}());
166+
exports.Util = Util;
167+
return module.exports;
168+
});
169+
170+
System.registerDynamic("src/universal-validators", ["@angular/core/src/facade/lang", "./util"], true, function($__require, exports, module) {
171+
"use strict";
172+
;
173+
var define,
174+
global = this,
175+
GLOBAL = this;
176+
var lang_1 = $__require('@angular/core/src/facade/lang');
177+
var util_1 = $__require('./util');
178+
var UniversalValidators = (function() {
179+
function UniversalValidators() {}
180+
UniversalValidators.noWhitespace = function() {
181+
return function validate(control) {
182+
if (util_1.Util.isNotPresent(control))
183+
return undefined;
184+
var pattern = '\\s';
185+
if (new RegExp(pattern).test(control.value)) {
186+
return {'noWhitespaceRequired': true};
187+
}
188+
return undefined;
189+
};
190+
};
191+
UniversalValidators.isNumber = function() {
192+
return function validate(control) {
193+
if (util_1.Util.isNotPresent(control))
194+
return undefined;
195+
if (lang_1.NumberWrapper.isNaN(control.value)) {
196+
return {'numberRequired': true};
197+
}
198+
return undefined;
199+
};
200+
};
201+
UniversalValidators.isInRange = function(minValue, maxValue) {
202+
return function validate(control) {
203+
if (util_1.Util.isNotPresent(control))
204+
return undefined;
205+
if (lang_1.NumberWrapper.isNaN(control.value)) {
206+
return {'numberRequired': true};
207+
}
208+
if (+control.value < minValue) {
209+
return {'rangeValueToSmall': true};
210+
}
211+
if (+control.value > maxValue) {
212+
return {'rangeValueToBig': true};
213+
} else {
214+
return undefined;
215+
}
216+
};
217+
};
218+
UniversalValidators.minLength = function(minLength) {
219+
return function validate(control) {
220+
if (util_1.Util.isNotPresent(control))
221+
return undefined;
222+
var value = control.value;
223+
if (value.length > minLength) {
224+
return undefined;
225+
}
226+
return {'minLength': true};
227+
};
228+
};
229+
UniversalValidators.maxLength = function(maxLength) {
230+
return function validate(control) {
231+
if (util_1.Util.isNotPresent(control))
232+
return undefined;
233+
var value = control.value;
234+
if (maxLength > value.length) {
235+
return undefined;
236+
}
237+
return {'maxLength': true};
238+
};
239+
};
240+
return UniversalValidators;
241+
}());
242+
exports.UniversalValidators = UniversalValidators;
243+
return module.exports;
244+
});
245+
246+
System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/email-validators", "./src/universal-validators"], true, function($__require, exports, module) {
144247
"use strict";
145248
;
146249
var define,
@@ -153,5 +256,6 @@ System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/em
153256
}
154257
__export($__require('./src/password-validators'));
155258
__export($__require('./src/email-validators'));
259+
__export($__require('./src/universal-validators'));
156260
return module.exports;
157261
});

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = function(config) {
5050
// test results reporter to use
5151
// possible values: 'dots', 'progress'
5252
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
53-
reporters: ['progress'],
53+
reporters: ['mocha'],
5454

5555

5656
// web server port

ng2-validators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './src/password-validators';
22
export * from './src/email-validators';
3-
3+
export * from './src/universal-validators';
44

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
},
3232
"devDependencies": {
3333
"@angular/common": "2.0.0-rc.1",
34+
"@angular/compiler": "2.0.0-rc.1",
3435
"@angular/core": "2.0.0-rc.1",
3536
"@angular/platform-browser": "2.0.0-rc.1",
36-
"@angular/compiler": "2.0.0-rc.1",
3737
"commitizen": "~2.8.1",
3838
"cz-conventional-changelog": "~1.1.6",
3939
"es6-promise": "^3.0.2",
@@ -45,6 +45,7 @@
4545
"karma-chrome-launcher": "~0.2.2",
4646
"karma-firefox-launcher": "~0.1.7",
4747
"karma-jasmine": "~0.3.8",
48+
"karma-mocha-reporter": "^2.0.3",
4849
"karma-typescript-preprocessor": "0.0.21",
4950
"reflect-metadata": "0.1.2",
5051
"rxjs": "5.0.0-beta.6",

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
}

0 commit comments

Comments
 (0)