diff --git a/example/src/app.html b/example/src/app.html index 4103332..bc98f92 100644 --- a/example/src/app.html +++ b/example/src/app.html @@ -126,6 +126,15 @@ +
+ + +
+ +

notWholeNumber error

+
+
+
@@ -262,5 +271,14 @@

uuid error

+ +
+ + +
+ +

wholeNumber error

+
+
- \ No newline at end of file + diff --git a/src/index.ts b/src/index.ts index d088187..56e73c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,12 +19,14 @@ import { min, MinValidator } from './min'; import { minDate, MinDateValidator } from './min-date'; import { notEqual, NotEqualValidator } from './not-equal'; import { notEqualTo, NotEqualToValidator } from './not-equal-to'; +import { notWholeNumber, NotWholeNumberValidator } from './not-whole-number'; import { number, NumberValidator } from './number'; import { phone, PhoneValidator } from './phone'; import { range, RangeValidator } from './range'; import { rangeLength, RangeLengthValidator } from './range-length'; import { url, UrlValidator } from './url'; import { uuid, UUIDValidator } from './uuid'; +import { wholeNumber, WholeNumberValidator } from './whole-number'; export const CustomValidators: any = { base64, @@ -46,12 +48,14 @@ export const CustomValidators: any = { minDate, notEqual, notEqualTo, + notWholeNumber, number, phone, range, rangeLength, url, - uuid + uuid, + wholeNumber }; const CUSTOM_FORM_DIRECTIVES = [ @@ -74,12 +78,14 @@ const CUSTOM_FORM_DIRECTIVES = [ MinDateValidator, NotEqualValidator, NotEqualToValidator, + NotWholeNumberValidator, NumberValidator, PhoneValidator, RangeValidator, RangeLengthValidator, UrlValidator, - UUIDValidator + UUIDValidator, + WholeNumberValidator ]; @NgModule({ diff --git a/src/not-whole-number/directive.spec.ts b/src/not-whole-number/directive.spec.ts new file mode 100644 index 0000000..8826b52 --- /dev/null +++ b/src/not-whole-number/directive.spec.ts @@ -0,0 +1 @@ +// todo \ No newline at end of file diff --git a/src/not-whole-number/directive.ts b/src/not-whole-number/directive.ts new file mode 100644 index 0000000..9467ad2 --- /dev/null +++ b/src/not-whole-number/directive.ts @@ -0,0 +1,20 @@ +import { Directive, forwardRef } from '@angular/core'; +import { NG_VALIDATORS, Validator, AbstractControl } from '@angular/forms'; + +import { notWholeNumber } from './'; + +const NOT_WHOLE_NUMBER_VALIDATOR: any = { + provide: NG_VALIDATORS, + useExisting: forwardRef(() => NotWholeNumberValidator), + multi: true +}; + +@Directive({ + selector: '[notWholeNumber][formControlName],[notWholeNumber][formControl],[notWholeNumber][ngModel]', + providers: [NOT_WHOLE_NUMBER_VALIDATOR] +}) +export class NotWholeNumberValidator implements Validator { + validate(c: AbstractControl): {[key: string]: any} { + return notWholeNumber(c); + } +} diff --git a/src/not-whole-number/index.ts b/src/not-whole-number/index.ts new file mode 100644 index 0000000..5c8cea0 --- /dev/null +++ b/src/not-whole-number/index.ts @@ -0,0 +1,2 @@ +export * from './directive'; +export * from './validator'; diff --git a/src/not-whole-number/validator.spec.ts b/src/not-whole-number/validator.spec.ts new file mode 100644 index 0000000..dd2181c --- /dev/null +++ b/src/not-whole-number/validator.spec.ts @@ -0,0 +1,27 @@ +import { FormControl } from '@angular/forms'; + +import { notWholeNumber } from './'; + +describe('NotWholeNumber', () => { + let control: FormControl; + + it('"23.3" should equal to "null"', () => { + control = new FormControl('23.3'); + expect(notWholeNumber(control)).toBeNull() + }); + + it('"23" should equal to "{notWholeNumber: true}"', () => { + control = new FormControl('23'); + expect(notWholeNumber(control)).toEqual({notWholeNumber: true}); + }); + + it('"23a" should equal to "{notWholeNumber: true}"', () => { + control = new FormControl('23a'); + expect(notWholeNumber(control)).toEqual({notWholeNumber: true}); + }); + + it('"23." should equal to "{notWholeNumber: true}"', () => { + control = new FormControl('23.'); + expect(notWholeNumber(control)).toEqual({notWholeNumber: true}); + }); +}); diff --git a/src/not-whole-number/validator.ts b/src/not-whole-number/validator.ts new file mode 100644 index 0000000..f3852e3 --- /dev/null +++ b/src/not-whole-number/validator.ts @@ -0,0 +1,10 @@ +import { AbstractControl, Validators, ValidatorFn } from '@angular/forms'; + +import { isPresent } from '../util/lang'; + +export const notWholeNumber: ValidatorFn = (control: AbstractControl): {[key: string]: boolean} => { + if (isPresent(Validators.required(control))) return null; + + let v: number = Number(control.value); + return v % 1 === 0 ? null : {'notWholeNumber': true}; +}; diff --git a/src/whole-number/directive.spec.ts b/src/whole-number/directive.spec.ts new file mode 100644 index 0000000..8826b52 --- /dev/null +++ b/src/whole-number/directive.spec.ts @@ -0,0 +1 @@ +// todo \ No newline at end of file diff --git a/src/whole-number/directive.ts b/src/whole-number/directive.ts new file mode 100644 index 0000000..0f2adb4 --- /dev/null +++ b/src/whole-number/directive.ts @@ -0,0 +1,20 @@ +import { Directive, forwardRef } from '@angular/core'; +import { NG_VALIDATORS, Validator, AbstractControl } from '@angular/forms'; + +import { wholeNumber } from './'; + +const WHOLE_NUMBER_VALIDATOR: any = { + provide: NG_VALIDATORS, + useExisting: forwardRef(() => WholeNumberValidator), + multi: true +}; + +@Directive({ + selector: '[wholeNumber][formControlName],[wholeNumber][formControl],[wholeNumber][ngModel]', + providers: [WHOLE_NUMBER_VALIDATOR] +}) +export class WholeNumberValidator implements Validator { + validate(c: AbstractControl): {[key: string]: any} { + return wholeNumber(c); + } +} diff --git a/src/whole-number/index.ts b/src/whole-number/index.ts new file mode 100644 index 0000000..5c8cea0 --- /dev/null +++ b/src/whole-number/index.ts @@ -0,0 +1,2 @@ +export * from './directive'; +export * from './validator'; diff --git a/src/whole-number/validator.spec.ts b/src/whole-number/validator.spec.ts new file mode 100644 index 0000000..64b164e --- /dev/null +++ b/src/whole-number/validator.spec.ts @@ -0,0 +1,27 @@ +import { FormControl } from '@angular/forms'; + +import { wholeNumber } from './'; + +describe('WholeNumber', () => { + let control: FormControl; + + it('"23" should equal to "null"', () => { + control = new FormControl('23'); + expect(wholeNumber(control)).toBeNull() + }); + + it('"23.3" should equal to "{wholeNumber: true}"', () => { + control = new FormControl('23.3'); + expect(wholeNumber(control)).toEqual({wholeNumber: true}); + }); + + it('"23a" should equal to "{wholeNumber: true}"', () => { + control = new FormControl('23a'); + expect(wholeNumber(control)).toEqual({wholeNumber: true}); + }); + + it('"23." should equal to "{wholeNumber: true}"', () => { + control = new FormControl('23.'); + expect(wholeNumber(control)).toEqual({wholeNumber: true}); + }); +}); diff --git a/src/whole-number/validator.ts b/src/whole-number/validator.ts new file mode 100644 index 0000000..d8c9367 --- /dev/null +++ b/src/whole-number/validator.ts @@ -0,0 +1,10 @@ +import { AbstractControl, Validators, ValidatorFn } from '@angular/forms'; + +import { isPresent } from '../util/lang'; + +export const wholeNumber: ValidatorFn = (control: AbstractControl): {[key: string]: boolean} => { + if (isPresent(Validators.required(control))) return null; + + let v: number = Number(control.value); + return v % 1 !== 0 ? null : {'wholeNumber': true}; +};