Skip to content

Commit

Permalink
feat(editor): add operatorConditionalType (inclusive or exclusive)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Apr 27, 2020
1 parent 1aadc86 commit e300b31
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/common/src/editorValidators/floatValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface FloatValidatorOptions {
errorMessage?: string;
minValue?: string | number;
maxValue?: string | number;
operatorConditionalType?: 'inclusive' | 'exclusive';
required?: boolean;
validator?: EditorValidator;
}
Expand All @@ -18,6 +19,7 @@ export function floatValidator(inputValue: any, options: FloatValidatorOptions):
const isRequired = options.required;
const minValue = options.minValue;
const maxValue = options.maxValue;
const operatorConditionalType = options.operatorConditionalType || 'inclusive';
const errorMsg = options.errorMessage;
const mapValidation = {
'{{minValue}}': minValue,
Expand All @@ -37,19 +39,19 @@ export function floatValidator(inputValue: any, options: FloatValidatorOptions):
// 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;
} else if (minValue !== undefined && maxValue !== undefined && floatNumber !== null && (floatNumber < minValue || floatNumber > maxValue)) {
} else if (minValue !== undefined && maxValue !== undefined && floatNumber !== null && ((operatorConditionalType === 'exclusive' && (floatNumber <= minValue || floatNumber >= maxValue)) || (operatorConditionalType === 'inclusive' && (floatNumber < minValue || floatNumber > maxValue)))) {
// MIN & MAX Values provided
// 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_BETWEEN.replace(/{{minValue}}|{{maxValue}}/gi, (matched) => mapValidation[matched]);
} else if (minValue !== undefined && floatNumber !== null && floatNumber < minValue) {
} else if (minValue !== undefined && floatNumber !== null && ((operatorConditionalType === 'exclusive' && floatNumber <= minValue)) || (operatorConditionalType === 'inclusive' && 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 && ((operatorConditionalType === 'exclusive' && floatNumber >= maxValue)) || (operatorConditionalType === 'inclusive' && 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
8 changes: 5 additions & 3 deletions packages/common/src/editorValidators/integerValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface IntegerValidatorOptions {
errorMessage?: string;
minValue?: string | number;
maxValue?: string | number;
operatorConditionalType?: 'inclusive' | 'exclusive';
required?: boolean;
validator?: EditorValidator;
}
Expand All @@ -20,6 +21,7 @@ export function integerValidator(inputValue: any, options: IntegerValidatorOptio
const isRequired = options.required;
const minValue = options.minValue;
const maxValue = options.maxValue;
const operatorConditionalType = options.operatorConditionalType || 'inclusive';
const mapValidation = {
'{{minValue}}': minValue,
'{{maxValue}}': maxValue
Expand All @@ -35,19 +37,19 @@ export function integerValidator(inputValue: any, options: IntegerValidatorOptio
} 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)) {
} else if (minValue !== undefined && maxValue !== undefined && intNumber !== null && ((operatorConditionalType === 'exclusive' && (intNumber <= minValue || intNumber >= maxValue)) || (operatorConditionalType === 'inclusive' && (intNumber < minValue || intNumber > maxValue)))) {
// MIN & MAX Values provided
// 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_BETWEEN.replace(/{{minValue}}|{{maxValue}}/gi, (matched) => mapValidation[matched]);
} else if (minValue !== undefined && intNumber !== null && intNumber < minValue) {
} else if (minValue !== undefined && intNumber !== null && ((operatorConditionalType === 'exclusive' && intNumber <= minValue)) || (operatorConditionalType === 'inclusive' && 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 && ((operatorConditionalType === 'exclusive' && intNumber >= maxValue)) || (operatorConditionalType === 'inclusive' && 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
49 changes: 49 additions & 0 deletions packages/common/src/editors/__tests__/floatEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,24 @@ describe('FloatEditor', () => {
expect(validation).toEqual({ valid: true, msg: '' });
});

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

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

it('should return False when field is equal to the maxValue defined but "operatorType" is set to "exclusive"', () => {
mockColumn.internalColumnEditor.maxValue = 10.2;
mockColumn.internalColumnEditor.operatorConditionalType = 'exclusive';
editor = new FloatEditor(editorArguments);
const validation = editor.validate(10.2);

expect(validation).toEqual({ valid: false, msg: 'Please enter a valid number that is lower than 10.2' });
});

it('should return False when field is not between minValue & maxValue defined', () => {
mockColumn.internalColumnEditor.minValue = 10.5;
mockColumn.internalColumnEditor.maxValue = 99.5;
Expand All @@ -499,6 +517,37 @@ describe('FloatEditor', () => {
expect(validation).toEqual({ valid: false, msg: 'Please enter a valid number between 10.5 and 99.5' });
});

it('should return True when field is is equal to maxValue defined when both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10.5;
mockColumn.internalColumnEditor.maxValue = 99.5;
editor = new FloatEditor(editorArguments);
const validation = editor.validate(99.5);

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

it('should return True when field is is equal to minValue defined when "operatorType" is set to "inclusive" and both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10.5;
mockColumn.internalColumnEditor.maxValue = 99.5;
mockColumn.internalColumnEditor.operatorConditionalType = 'inclusive';
editor = new FloatEditor(editorArguments);
const validation = editor.validate(10.5);

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

it('should return False when field is equal to maxValue but "operatorType" is set to "exclusive" when both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10.5;
mockColumn.internalColumnEditor.maxValue = 99.5;
mockColumn.internalColumnEditor.operatorConditionalType = 'exclusive';
editor = new FloatEditor(editorArguments);
const validation1 = editor.validate(99.5);
const validation2 = editor.validate(10.5);

expect(validation1).toEqual({ valid: false, msg: 'Please enter a valid number between 10.5 and 99.5' });
expect(validation2).toEqual({ valid: false, msg: 'Please enter a valid number between 10.5 and 99.5' });
});

it('should return False when field has more decimals than the "decimalPlaces" which is the maximum decimal allowed', () => {
mockColumn.internalColumnEditor.params = { decimalPlaces: 2 };

Expand Down
49 changes: 49 additions & 0 deletions packages/common/src/editors/__tests__/integerEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,24 @@ describe('IntegerEditor', () => {
expect(validation).toEqual({ valid: true, msg: '' });
});

it('should return True when field is equal to the maxValue defined and "operatorType" is set to "inclusive"', () => {
mockColumn.internalColumnEditor.maxValue = 9;
mockColumn.internalColumnEditor.operatorConditionalType = 'inclusive';
editor = new IntegerEditor(editorArguments);
const validation = editor.validate(9);

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

it('should return False when field is equal to the maxValue defined but "operatorType" is set to "exclusive"', () => {
mockColumn.internalColumnEditor.maxValue = 9;
mockColumn.internalColumnEditor.operatorConditionalType = 'exclusive';
editor = new IntegerEditor(editorArguments);
const validation = editor.validate(9);

expect(validation).toEqual({ valid: false, msg: 'Please enter a valid integer number that is lower than 9' });
});

it('should return False when field is not between minValue & maxValue defined', () => {
mockColumn.internalColumnEditor.minValue = 10;
mockColumn.internalColumnEditor.maxValue = 99;
Expand All @@ -461,6 +479,37 @@ describe('IntegerEditor', () => {
expect(validation).toEqual({ valid: false, msg: 'Please enter a valid integer number between 10 and 99' });
});

it('should return True when field is is equal to maxValue defined when both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10;
mockColumn.internalColumnEditor.maxValue = 89;
editor = new IntegerEditor(editorArguments);
const validation = editor.validate(89);

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

it('should return True when field is is equal to minValue defined when "operatorType" is set to "inclusive" and both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10;
mockColumn.internalColumnEditor.maxValue = 89;
mockColumn.internalColumnEditor.operatorConditionalType = 'inclusive';
editor = new IntegerEditor(editorArguments);
const validation = editor.validate(10);

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

it('should return False when field is equal to maxValue but "operatorType" is set to "exclusive" when both min/max values are defined', () => {
mockColumn.internalColumnEditor.minValue = 10;
mockColumn.internalColumnEditor.maxValue = 89;
mockColumn.internalColumnEditor.operatorConditionalType = 'exclusive';
editor = new IntegerEditor(editorArguments);
const validation1 = editor.validate(89);
const validation2 = editor.validate(10);

expect(validation1).toEqual({ valid: false, msg: 'Please enter a valid integer number between 10 and 89' });
expect(validation2).toEqual({ valid: false, msg: 'Please enter a valid integer number between 10 and 89' });
});

it('should return True when field is required and field is a valid input value', () => {
mockColumn.internalColumnEditor.required = true;
editor = new IntegerEditor(editorArguments);
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/editors/dualInputEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,14 @@ export class DualInputEditor implements Editor {
decimal: this.getDecimalPlaces(position),
minValue: positionEditorParams.minValue,
maxValue: positionEditorParams.maxValue,
operatorConditionalType: positionEditorParams.operatorConditionalType,
});
case 'integer':
return integerValidator(currentVal, {
...baseValidatorOptions,
minValue: positionEditorParams.minValue,
maxValue: positionEditorParams.maxValue,
operatorConditionalType: positionEditorParams.operatorConditionalType,
});
case 'text':
case 'password':
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/editors/floatEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export class FloatEditor implements Editor {
decimal: this.getDecimalPlaces(),
minValue: this.columnEditor.minValue,
maxValue: this.columnEditor.maxValue,
operatorConditionalType: this.columnEditor.operatorConditionalType,
required: this.columnEditor.required,
validator: this.validator,
});
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/editors/integerEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class IntegerEditor implements Editor {
errorMessage: this.columnEditor.errorMessage,
minValue: this.columnEditor.minValue,
maxValue: this.columnEditor.maxValue,
operatorConditionalType: this.columnEditor.operatorConditionalType,
required: this.columnEditor.required,
validator: this.validator,
});
Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/interfaces/columnEditor.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export interface ColumnEditor {
*/
placeholder?: string;

/** Defaults to "inclusive", operator should be (inclusive or exclusive) when executing validation condition check against the minValue/maxValue. */
operatorConditionalType?: 'inclusive' | 'exclusive';

/**
* Title attribute that can be used in some Editors as tooltip (usually the "input" editors).
*
Expand Down

0 comments on commit e300b31

Please sign in to comment.