Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'wholeNumber' and 'notWholeNumber' validators #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion example/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@
</div>
</div>

<div class="form-group">
<label class="col-xs-3 control-label">notWholeNumber</label>

<div class="col-xs-9">
<input class="form-control" type="text" ngModel name="notWholeNumber" #notWholeNumber="ngModel" notWholeNumber="3"/>
<p class="alert alert-danger" *ngIf="notWholeNumber?.errors?.notWholeNumber">notWholeNumber error</p>
</div>
</div>

<div class="form-group">
<label class="col-xs-3 control-label">equalTo</label>

Expand Down Expand Up @@ -262,5 +271,14 @@
<p class="alert alert-danger" *ngIf="uuid?.errors?.uuid">uuid error</p>
</div>
</div>

<div class="form-group">
<label class="col-xs-3 control-label">wholeNumber</label>

<div class="col-xs-9">
<input class="form-control" type="text" ngModel name="wholeNumber" #wholeNumber="ngModel" wholeNumber="3"/>
<p class="alert alert-danger" *ngIf="wholeNumber?.errors?.wholeNumber">wholeNumber error</p>
</div>
</div>
</form>
</div>
</div>
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,12 +48,14 @@ export const CustomValidators: any = {
minDate,
notEqual,
notEqualTo,
notWholeNumber,
number,
phone,
range,
rangeLength,
url,
uuid
uuid,
wholeNumber
};

const CUSTOM_FORM_DIRECTIVES = [
Expand All @@ -74,12 +78,14 @@ const CUSTOM_FORM_DIRECTIVES = [
MinDateValidator,
NotEqualValidator,
NotEqualToValidator,
NotWholeNumberValidator,
NumberValidator,
PhoneValidator,
RangeValidator,
RangeLengthValidator,
UrlValidator,
UUIDValidator
UUIDValidator,
WholeNumberValidator
];

@NgModule({
Expand Down
1 change: 1 addition & 0 deletions src/not-whole-number/directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// todo
20 changes: 20 additions & 0 deletions src/not-whole-number/directive.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/not-whole-number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './directive';
export * from './validator';
27 changes: 27 additions & 0 deletions src/not-whole-number/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -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});
});
});
10 changes: 10 additions & 0 deletions src/not-whole-number/validator.ts
Original file line number Diff line number Diff line change
@@ -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};
};
1 change: 1 addition & 0 deletions src/whole-number/directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// todo
20 changes: 20 additions & 0 deletions src/whole-number/directive.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/whole-number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './directive';
export * from './validator';
27 changes: 27 additions & 0 deletions src/whole-number/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -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});
});
});
10 changes: 10 additions & 0 deletions src/whole-number/validator.ts
Original file line number Diff line number Diff line change
@@ -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};
};