Skip to content

Commit

Permalink
fix(editor): number validators should be ok with null value on init
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Apr 27, 2020
1 parent aba4182 commit 1aadc86
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/editorValidators/floatValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/editorValidators/integerValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/editors/__tests__/floatEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions packages/common/src/editors/__tests__/integerEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/web-demo-vanilla-bundle/src/examples/example04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 `<b>${costText}</b> | ${durationText}`;
}
Expand Down

0 comments on commit 1aadc86

Please sign in to comment.