From 3e193aabd8bdf515d53da938c19bc931b29c8438 Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Sun, 26 Apr 2020 18:04:19 -0400 Subject: [PATCH] fix(editor): float validator min/max values should be inclusive --- .../src/editorValidators/floatValidator.ts | 4 ++-- .../src/editors/__tests__/floatEditor.spec.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/common/src/editorValidators/floatValidator.ts b/packages/common/src/editorValidators/floatValidator.ts index e6c8bbb5e..0fbe7f37f 100644 --- a/packages/common/src/editorValidators/floatValidator.ts +++ b/packages/common/src/editorValidators/floatValidator.ts @@ -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 diff --git a/packages/common/src/editors/__tests__/floatEditor.spec.ts b/packages/common/src/editors/__tests__/floatEditor.spec.ts index f1ffd179d..45a618df9 100644 --- a/packages/common/src/editors/__tests__/floatEditor.spec.ts +++ b/packages/common/src/editors/__tests__/floatEditor.spec.ts @@ -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); @@ -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;