Skip to content

Commit

Permalink
(feat) Add disallow decimals validator (#135)
Browse files Browse the repository at this point in the history
* (feat) Add disallow decimals validator

* Restore ESLint ignores

* Remove console.log
  • Loading branch information
denniskigen authored Apr 19, 2024
1 parent 68dc735 commit cd73355
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Thumbs.db
./package-lock.json

.turbo
.eslintcache
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { MaxLengthValidationModel } from '../question-models/max-length-validati
import { MinLengthValidationModel } from '../question-models/min-length-validation.model';
import { WorkspaceLauncherQuestion } from '../question-models';
import { DecimalValidationModel } from '../question-models/decimal-validation.model';

import { DisallowDecimalsValidationModel } from '../question-models/disallow-decimals-validation.model';
@Injectable()
export class QuestionFactory {
dataSources: any = {};
Expand Down Expand Up @@ -1055,6 +1055,14 @@ export class QuestionFactory {
})
);
}
if (questionOptions.disallowDecimals) {
validators.push(
new DisallowDecimalsValidationModel({
type: 'disallowDecimals',
disallowDecimals: questionOptions.disallowDecimals
})
);
}

break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { MaxLengthValidator } from '../validators/max-length.validator';
import { MaxLengthValidationModel } from '../question-models/max-length-validation.model';
import { MinLengthValidationModel } from '../question-models/min-length-validation.model';
import { MinLengthValidator } from '../validators/min-length.validator';
import { DisallowDecimalsValidator } from '../validators/disallow-decimals.validator';

@Injectable()
export class ValidationFactory {
Expand Down Expand Up @@ -57,6 +58,9 @@ export class ValidationFactory {
this.getMaxValueValidator((<MaxValidationModel>validator).max)
);
break;
case 'disallowDecimals':
list.push(this.getDisallowedDecimalsValidator());
break;
case 'maxlength':
list.push(
this.maxLengthValidator(
Expand Down Expand Up @@ -115,6 +119,10 @@ export class ValidationFactory {
return new ConditionalAnsweredValidator();
}

get disallowDecimalsValidator(): DisallowDecimalsValidator {
return new DisallowDecimalsValidator();
}

get requiredValidator(): any {
return new RequiredValidator().validate;
}
Expand Down Expand Up @@ -151,6 +159,10 @@ export class ValidationFactory {
return new MaxValidator().validate(max);
}

public getDisallowedDecimalsValidator() {
return new DisallowDecimalsValidator().validate();
}

get jsExpressionValidator() {
return new JsExpressionValidator();
}
Expand All @@ -170,6 +182,9 @@ export class ValidationFactory {
case 'futureDateRestriction':
messages.push(this.translate.instant('futureDateRestriction'));
break;
case 'disallowDecimals':
messages.push(this.translate.instant('disallowDecimals'));
break;
case 'minlength':
messages.push(
this.translate
Expand Down
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();
}
}
2 changes: 2 additions & 0 deletions projects/ngx-formentry/src/form-entry/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ export class Messages {

public static readonly min =
'Please enter a value greater than or equal to {min}';

public static readonly disallowDecimals = 'Decimals values are not allowed';
}
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);
});
});
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;
};
}
}
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
"changeToMonthView": "Change to month view",
"chooseMonthAndYear": "Choose month and year",
"clearEntry": "Are you sure you want to clear this entry?",
"componentLoadingFailed": "Component Loading failed...",
"componentLoadingFailed": "Component loading failed...",
"daysAgo": " days ago",
"deleteEntry": "Are you sure you want to delete this item?",
"disallowDecimals": "Decimal values are not allowed",
"enterMoreCharacters": "Please enter 2 or more characters",
"fix": "Fix",
"from": "From",
Expand Down

0 comments on commit cd73355

Please sign in to comment.