-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Add disallow decimals validator (#135)
* (feat) Add disallow decimals validator * Restore ESLint ignores * Remove console.log
- Loading branch information
1 parent
68dc735
commit cd73355
Showing
8 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ Thumbs.db | |
./package-lock.json | ||
|
||
.turbo | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
projects/ngx-formentry/src/form-entry/question-models/disallow-decimals-validation.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ValidationModel } from './validation.model'; | ||
|
||
export class DisallowDecimalsValidationModel extends ValidationModel { | ||
disallowDecimals: boolean; | ||
failsWhenExpression: string; | ||
message: string; | ||
|
||
constructor(validations: any) { | ||
super(validations); | ||
this.disallowDecimals = validations.disallowDecimals; | ||
} | ||
|
||
setMessage(): void { | ||
this.message = 'Decimal values are not allowed'; | ||
} | ||
|
||
setFailsWhenExpression(): void { | ||
this.failsWhenExpression = `!isEmpty(myValue) && (myValue).toString().includes('.')`; | ||
} | ||
|
||
setValuesAndExpressions() { | ||
this.setMessage(); | ||
this.setFailsWhenExpression(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AfeFormControl } from '../../abstract-controls-extension/afe-form-control'; | ||
import { DisallowDecimalsValidator } from './disallow-decimals.validator'; | ||
|
||
describe('DisallowDecimalsValidator', () => { | ||
it('should return null when a non-decimal value is provided', () => { | ||
const validator: DisallowDecimalsValidator = new DisallowDecimalsValidator(); | ||
const number = '123'; | ||
|
||
const formControl = new AfeFormControl(number, [validator.validate]); | ||
|
||
expect(formControl.errors).toBe(null); | ||
}); | ||
|
||
it('should return an error when a decimal value is provided', () => { | ||
const validator: DisallowDecimalsValidator = new DisallowDecimalsValidator(); | ||
const number = '123.456'; | ||
|
||
const formControl = new AfeFormControl(number, [validator.validate()]); | ||
|
||
expect(formControl.errors['disallowDecimals']).toBe(true); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { AfeFormControl } from '../../abstract-controls-extension/afe-form-control'; | ||
|
||
export class DisallowDecimalsValidator { | ||
validate() { | ||
return (control: AfeFormControl): { [key: string]: any } => { | ||
if (control.hidden) { | ||
return null; | ||
} | ||
|
||
if (control.value && control.value.length !== 0) { | ||
const test: boolean = !/^\d+$/.test(control.value); | ||
return test ? { disallowDecimals: true } : null; | ||
} | ||
|
||
return null; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters