Skip to content

Commit c4abe15

Browse files
committed
feat(general-validators): Add new general validators
Add isNumber and noWhitespace Moved whitespace from password to general
1 parent 06e95e1 commit c4abe15

File tree

6 files changed

+117
-39
lines changed

6 files changed

+117
-39
lines changed

bundles/ng2-validators.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ System.registerDynamic("src/password-validators", [], true, function($__require,
1616
return undefined;
1717
};
1818
};
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-
};
2819
PasswordValidators.allowedCharacterRule = function(allowedChars) {
2920
return function validate(control) {
3021
var value = control.value;
@@ -140,7 +131,39 @@ System.registerDynamic("src/email-validators", [], true, function($__require, ex
140131
return module.exports;
141132
});
142133

143-
System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/email-validators"], true, function($__require, exports, module) {
134+
System.registerDynamic("src/universal-validators", ["@angular/core/src/facade/lang"], true, function($__require, exports, module) {
135+
"use strict";
136+
;
137+
var define,
138+
global = this,
139+
GLOBAL = this;
140+
var lang_1 = $__require('@angular/core/src/facade/lang');
141+
var UniversalValidators = (function() {
142+
function UniversalValidators() {}
143+
UniversalValidators.noWhitespace = function() {
144+
return function validate(control) {
145+
var pattern = '\\s';
146+
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
147+
return {'noWhitespaceRequired': true};
148+
}
149+
return undefined;
150+
};
151+
};
152+
UniversalValidators.isNumber = function() {
153+
return function validate(control) {
154+
if (control.value !== '' && lang_1.NumberWrapper.isNaN(control.value)) {
155+
return {'numberRequired': true};
156+
}
157+
return undefined;
158+
};
159+
};
160+
return UniversalValidators;
161+
}());
162+
exports.UniversalValidators = UniversalValidators;
163+
return module.exports;
164+
});
165+
166+
System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/email-validators", "./src/universal-validators"], true, function($__require, exports, module) {
144167
"use strict";
145168
;
146169
var define,
@@ -153,5 +176,6 @@ System.registerDynamic("ng2-validators", ["./src/password-validators", "./src/em
153176
}
154177
__export($__require('./src/password-validators'));
155178
__export($__require('./src/email-validators'));
179+
__export($__require('./src/universal-validators'));
156180
return module.exports;
157181
});

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

src/password-validators.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ export class PasswordValidators {
1313
};
1414
}
1515

16-
static whitespaceRule(): any {
17-
return function validate(control: AbstractControl): { [key: string]: any } {
18-
let pattern = '\\s';
19-
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
20-
return { 'whitespaceRule': true };
21-
}
22-
return undefined;
23-
};
24-
}
25-
2616
static allowedCharacterRule(allowedChars: string[]): any {
2717
return function validate(control: AbstractControl): { [key: string]: any } {
2818
let value: string = control.value;

src/universal-validators.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { AbstractControl } from '@angular/common';
2+
import { NumberWrapper} from '@angular/core/src/facade/lang';
3+
export class UniversalValidators {
4+
5+
static noWhitespace(): any {
6+
return function validate(control: AbstractControl): { [key: string]: any } {
7+
let pattern = '\\s';
8+
if (control.value !== '' && new RegExp(pattern).test(control.value)) {
9+
return { 'noWhitespaceRequired': true };
10+
}
11+
return undefined;
12+
};
13+
}
14+
15+
static isNumber(): any {
16+
return function validate(control: AbstractControl): { [key: string]: any } {
17+
if (control.value !== '' && NumberWrapper.isNaN(control.value)) {
18+
return { 'numberRequired': true };
19+
}
20+
return undefined;
21+
};
22+
}
23+
24+
25+
}

tests/password-validators.spec.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,6 @@ export function main() {
3131
expect(validated).toBeUndefined();
3232
});
3333
});
34-
describe('whitespaceRule', () => {
35-
36-
it('should work for empty control', () => {
37-
let control: Control = new Control('');
38-
let validated = PasswordValidators.whitespaceRule()(control);
39-
expect(validated).toBeUndefined();
40-
});
41-
it('should work for valid password', () => {
42-
let control: Control = new Control('aaabbbccc');
43-
let validated = PasswordValidators.whitespaceRule()(control);
44-
expect(validated).toBeUndefined();
45-
});
46-
it('should work for invalid password', () => {
47-
let control: Control = new Control('aaab bbccc');
48-
let validated = PasswordValidators.whitespaceRule()(control);
49-
expect(validated).toEqual({ 'whitespaceRule': true });
50-
});
51-
});
5234

5335
describe('allowedCharacterRule', () => {
5436

tests/universal-validators.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {UniversalValidators} from '../src/universal-validators';
2+
import {Control} from '@angular/common';
3+
4+
export function main() {
5+
6+
describe('Universal validators service', () => {
7+
8+
describe('whitespaceRule', () => {
9+
10+
it('should work for empty control', () => {
11+
let control: Control = new Control('');
12+
let validated = UniversalValidators.noWhitespace()(control);
13+
expect(validated).toBeUndefined();
14+
});
15+
16+
it('should work for valid password', () => {
17+
let control: Control = new Control('aaabbbccc');
18+
let validated = UniversalValidators.noWhitespace()(control);
19+
expect(validated).toBeUndefined();
20+
});
21+
22+
it('should work for invalid password', () => {
23+
let control: Control = new Control('aaab bbccc');
24+
let validated = UniversalValidators.noWhitespace()(control);
25+
expect(validated).toEqual({ 'noWhitespaceRequired': true });
26+
});
27+
});
28+
29+
describe('numberRule', () => {
30+
31+
it('should work for empty control', () => {
32+
let control: Control = new Control('');
33+
let validated = UniversalValidators.isNumber()(control);
34+
expect(validated).toBeUndefined();
35+
});
36+
37+
it('should work for valid number', () => {
38+
let control: Control = new Control('453');
39+
let validated = UniversalValidators.isNumber()(control);
40+
expect(validated).toBeUndefined();
41+
});
42+
43+
it('should work for valid number 2', () => {
44+
let control: Control = new Control('453.5');
45+
let validated = UniversalValidators.isNumber()(control);
46+
expect(validated).toBeUndefined();
47+
});
48+
49+
it('should work for invalid number', () => {
50+
let control: Control = new Control('abbccc');
51+
let validated = UniversalValidators.isNumber()(control);
52+
expect(validated).toEqual({ 'numberRequired': true });
53+
});
54+
55+
});
56+
});
57+
}

0 commit comments

Comments
 (0)