From 1aadc86787d88de8e18a193853e40ee88e795f93 Mon Sep 17 00:00:00 2001 From: Ghislain Beaulac Date: Mon, 27 Apr 2020 16:25:58 -0400 Subject: [PATCH] fix(editor): number validators should be ok with null value on init --- .../src/editorValidators/floatValidator.ts | 2 +- .../src/editorValidators/integerValidator.ts | 6 +++--- .../src/editors/__tests__/floatEditor.spec.ts | 4 ++-- .../src/editors/__tests__/integerEditor.spec.ts | 16 ++++++++++++++++ .../src/examples/example04.ts | 8 ++++---- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/common/src/editorValidators/floatValidator.ts b/packages/common/src/editorValidators/floatValidator.ts index 0fbe7f37f..e11c89fc7 100644 --- a/packages/common/src/editorValidators/floatValidator.ts +++ b/packages/common/src/editorValidators/floatValidator.ts @@ -33,7 +33,7 @@ export function floatValidator(inputValue: any, options: FloatValidatorOptions): } else if (isRequired && inputValue === '') { isValid = false; outputMsg = errorMsg || Constants.VALIDATION_REQUIRED_FIELD; - } else if (isNaN(inputValue as number) || (decPlaces === 0 && !/^[-+]?(\d+(\.)?(\d)*)$/.test(inputValue))) { + } else if (inputValue !== '' && (isNaN(inputValue as number) || (decPlaces === 0 && !/^[-+]?(\d+(\.)?(\d)*)$/.test(inputValue)))) { // when decimal value is 0 (which is the default), we accept 0 or more decimal values isValid = false; outputMsg = errorMsg || Constants.VALIDATION_EDITOR_VALID_NUMBER; diff --git a/packages/common/src/editorValidators/integerValidator.ts b/packages/common/src/editorValidators/integerValidator.ts index becf3cc60..55161afc5 100644 --- a/packages/common/src/editorValidators/integerValidator.ts +++ b/packages/common/src/editorValidators/integerValidator.ts @@ -32,7 +32,7 @@ export function integerValidator(inputValue: any, options: IntegerValidatorOptio } else if (isRequired && inputValue === '') { isValid = false; outputMsg = errorMsg || Constants.VALIDATION_REQUIRED_FIELD; - } else if (inputValue && (isNaN(inputValue as number) || !/^[+-]?\d+$/.test(inputValue))) { + } else if (inputValue !== '' && ((isNaN(inputValue as number) || !/^[+-]?\d+$/.test(inputValue)))) { isValid = false; outputMsg = errorMsg || Constants.VALIDATION_EDITOR_VALID_INTEGER; } else if (minValue !== undefined && maxValue !== undefined && intNumber !== null && (intNumber < minValue || intNumber > maxValue)) { @@ -41,13 +41,13 @@ export function integerValidator(inputValue: any, options: IntegerValidatorOptio // 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_INTEGER_BETWEEN.replace(/{{minValue}}|{{maxValue}}/gi, (matched) => mapValidation[matched]); - } else if (minValue !== undefined && intNumber !== null && intNumber <= minValue) { + } else if (minValue !== undefined && intNumber !== null && intNumber < 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_INTEGER_MIN.replace(/{{minValue}}/gi, (matched) => mapValidation[matched]); - } else if (maxValue !== undefined && intNumber !== null && intNumber >= maxValue) { + } else if (maxValue !== undefined && intNumber !== null && intNumber > 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 45a618df9..56df5907a 100644 --- a/packages/common/src/editors/__tests__/floatEditor.spec.ts +++ b/packages/common/src/editors/__tests__/floatEditor.spec.ts @@ -406,13 +406,13 @@ describe('FloatEditor', () => { }); it('should not call anything when the input value is not a valid float number', () => { - mockItemData = { id: 1, price: '.', isActive: true }; + mockItemData = { id: 1, price: null, isActive: true }; gridOptionMock.autoCommitEdit = true; const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit'); editor = new FloatEditor(editorArguments); editor.loadValue(mockItemData); - editor.setValue('.'); + editor.setValue('-.'); editor.save(); expect(spy).not.toHaveBeenCalled(); diff --git a/packages/common/src/editors/__tests__/integerEditor.spec.ts b/packages/common/src/editors/__tests__/integerEditor.spec.ts index 0d0254a76..b3f799f6c 100644 --- a/packages/common/src/editors/__tests__/integerEditor.spec.ts +++ b/packages/common/src/editors/__tests__/integerEditor.spec.ts @@ -428,6 +428,14 @@ describe('IntegerEditor', () => { expect(validation).toEqual({ valid: false, msg: 'Please enter a valid integer number that is greater than 10' }); }); + it('should return True when field is equal to the minValue defined', () => { + mockColumn.internalColumnEditor.minValue = 9; + editor = new IntegerEditor(editorArguments); + const validation = editor.validate(9); + + expect(validation).toEqual({ valid: true, msg: '' }); + }); + it('should return False when field is greater than a maxValue defined', () => { mockColumn.internalColumnEditor.maxValue = 10; editor = new IntegerEditor(editorArguments); @@ -436,6 +444,14 @@ describe('IntegerEditor', () => { expect(validation).toEqual({ valid: false, msg: 'Please enter a valid integer number that is lower than 10' }); }); + it('should return True when field is equal to the maxValue defined', () => { + mockColumn.internalColumnEditor.maxValue = 99; + editor = new IntegerEditor(editorArguments); + const validation = editor.validate(99); + + expect(validation).toEqual({ valid: true, msg: '' }); + }); + it('should return False when field is not between minValue & maxValue defined', () => { mockColumn.internalColumnEditor.minValue = 10; mockColumn.internalColumnEditor.maxValue = 99; diff --git a/packages/web-demo-vanilla-bundle/src/examples/example04.ts b/packages/web-demo-vanilla-bundle/src/examples/example04.ts index 3c583d7e7..5531be16d 100644 --- a/packages/web-demo-vanilla-bundle/src/examples/example04.ts +++ b/packages/web-demo-vanilla-bundle/src/examples/example04.ts @@ -328,7 +328,8 @@ export class Example4 { this.dataset[i] = { id: i, title: 'Task ' + i, - duration: Math.round(Math.random() * 100) + '', + // duration: Math.round(Math.random() * 100) + '', + duration: null, percentComplete: Math.round(Math.random() * 100), start: new Date(randomYear, randomMonth, randomDay), finish: new Date(randomYear, (randomMonth + 1), randomDay), @@ -343,9 +344,8 @@ export class Example4 { costDurationFormatter(row, cell, value, columnDef, dataContext) { const costText = (dataContext.cost === null) ? 'n/a' : Slicker.Utilities.formatNumber(dataContext.cost, 0, 2, false, '$', '', '.', ','); let durationText = ''; - if (dataContext.duration >= 0) { - const durationValue = 0; - durationText = `${dataContext.duration} ${durationValue > 0 ? 'day' : 'days'}`; + if (dataContext.duration !== null && dataContext.duration !== undefined && dataContext.duration >= 0) { + durationText = `${dataContext.duration} ${dataContext.duration > 1 ? 'days' : 'day'}`; } return `${costText} | ${durationText}`; }