Skip to content

Commit

Permalink
fix(editor): float validator min/max values should be inclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Apr 26, 2020
1 parent 98f2b5e commit 3e193aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/editorValidators/floatValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function floatValidator(inputValue: any, options: FloatValidatorOptions):
// for example if we set decimalPlaces to 2, we will only accept numbers between 0 and 2 decimals
isValid = false;
outputMsg = errorMsg || Constants.VALIDATION_EDITOR_NUMBER_BETWEEN.replace(/{{minValue}}|{{maxValue}}/gi, (matched) => mapValidation[matched]);
} else if (minValue !== undefined && floatNumber !== null && floatNumber <= minValue) {
} else if (minValue !== undefined && floatNumber !== null && floatNumber < minValue) {
// MIN VALUE ONLY
// when decimal value is bigger than 0, we only accept the decimal values as that value set
// for example if we set decimalPlaces to 2, we will only accept numbers between 0 and 2 decimals
isValid = false;
outputMsg = errorMsg || Constants.VALIDATION_EDITOR_NUMBER_MIN.replace(/{{minValue}}/gi, (matched) => mapValidation[matched]);
} else if (maxValue !== undefined && floatNumber !== null && floatNumber >= maxValue) {
} else if (maxValue !== undefined && floatNumber !== null && floatNumber > maxValue) {
// MAX VALUE ONLY
// when decimal value is bigger than 0, we only accept the decimal values as that value set
// for example if we set decimalPlaces to 2, we will only accept numbers between 0 and 2 decimals
Expand Down
16 changes: 16 additions & 0 deletions packages/common/src/editors/__tests__/floatEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ describe('FloatEditor', () => {
expect(validation).toEqual({ valid: false, msg: 'Please enter a valid number that is greater than 10.2' });
});

it('should return True when field is equal to the minValue defined', () => {
mockColumn.internalColumnEditor.minValue = 10.2;
editor = new FloatEditor(editorArguments);
const validation = editor.validate(10.2);

expect(validation).toEqual({ valid: true, msg: '' });
});

it('should return False when field is greater than a maxValue defined', () => {
mockColumn.internalColumnEditor.maxValue = 10.2;
editor = new FloatEditor(editorArguments);
Expand All @@ -474,6 +482,14 @@ describe('FloatEditor', () => {
expect(validation).toEqual({ valid: false, msg: 'Please enter a valid number that is lower than 10.2' });
});

it('should return True when field is equal to the maxValue defined', () => {
mockColumn.internalColumnEditor.maxValue = 10.2;
editor = new FloatEditor(editorArguments);
const validation = editor.validate(10.2);

expect(validation).toEqual({ valid: true, msg: '' });
});

it('should return False when field is not between minValue & maxValue defined', () => {
mockColumn.internalColumnEditor.minValue = 10.5;
mockColumn.internalColumnEditor.maxValue = 99.5;
Expand Down

0 comments on commit 3e193aa

Please sign in to comment.