From d15bbe0a5e769e94ea7613135d7e3afccc547278 Mon Sep 17 00:00:00 2001 From: Aswath K Date: Tue, 1 Nov 2022 18:17:07 +0530 Subject: [PATCH] fix: Number validation passthrough behaviour on 0 (#18001) --- .../src/constants/PropertyControlConstants.tsx | 1 + .../widgets/FilePickerWidgetV2/widget/index.tsx | 7 ++++++- .../widgets/FilepickerWidget/widget/index.tsx | 7 ++++++- .../src/widgets/InputWidgetV2/widget/index.tsx | 2 +- .../widgets/ProgressBarWidget/widget/index.tsx | 8 +++++++- .../src/widgets/ProgressWidget/widget/index.tsx | 8 +++++++- app/client/src/workers/validations.test.ts | 16 ++++++++++++++++ app/client/src/workers/validations.ts | 7 ++++++- 8 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/client/src/constants/PropertyControlConstants.tsx b/app/client/src/constants/PropertyControlConstants.tsx index f717224285..cdef533f5a 100644 --- a/app/client/src/constants/PropertyControlConstants.tsx +++ b/app/client/src/constants/PropertyControlConstants.tsx @@ -121,6 +121,7 @@ type ValidationConfigParams = { ignoreCase?: boolean; //to ignore the case of key type?: ValidationTypes; // Used for ValidationType.TABLE_PROPERTY to define sub type params?: ValidationConfigParams; // Used for ValidationType.TABLE_PROPERTY to define sub type params + passThroughOnZero?: boolean; // Used for ValidationType.NUMBER to allow 0 to be passed through. Deafults value is true limitLineBreaks?: boolean; // Used for ValidationType.TEXT to limit line breaks in a large json object. }; diff --git a/app/client/src/widgets/FilePickerWidgetV2/widget/index.tsx b/app/client/src/widgets/FilePickerWidgetV2/widget/index.tsx index 4eeb919f02..89d544304e 100644 --- a/app/client/src/widgets/FilePickerWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/FilePickerWidgetV2/widget/index.tsx @@ -377,7 +377,12 @@ class FilePickerWidget extends BaseWidget< isTriggerProperty: false, validation: { type: ValidationTypes.NUMBER, - params: { min: 1, max: 100, default: 5 }, + params: { + min: 1, + max: 100, + default: 5, + passThroughOnZero: false, + }, }, }, ], diff --git a/app/client/src/widgets/FilepickerWidget/widget/index.tsx b/app/client/src/widgets/FilepickerWidget/widget/index.tsx index df7b1e7387..08cca1d957 100644 --- a/app/client/src/widgets/FilepickerWidget/widget/index.tsx +++ b/app/client/src/widgets/FilepickerWidget/widget/index.tsx @@ -68,7 +68,12 @@ class FilePickerWidget extends BaseWidget< isTriggerProperty: false, validation: { type: ValidationTypes.NUMBER, - params: { min: 1, max: 100, default: 5 }, + params: { + min: 1, + max: 100, + default: 5, + passThroughOnZero: false, + }, }, }, { diff --git a/app/client/src/widgets/InputWidgetV2/widget/index.tsx b/app/client/src/widgets/InputWidgetV2/widget/index.tsx index 1109321db7..c707188032 100644 --- a/app/client/src/widgets/InputWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/InputWidgetV2/widget/index.tsx @@ -256,7 +256,7 @@ class InputWidget extends BaseInputWidget { isTriggerProperty: false, validation: { type: ValidationTypes.NUMBER, - params: { min: 1, natural: true }, + params: { min: 1, natural: true, passThroughOnZero: false }, }, hidden: (props: InputWidgetProps) => { return props.inputType !== InputTypes.TEXT; diff --git a/app/client/src/widgets/ProgressBarWidget/widget/index.tsx b/app/client/src/widgets/ProgressBarWidget/widget/index.tsx index a18154b296..31ef4fc130 100644 --- a/app/client/src/widgets/ProgressBarWidget/widget/index.tsx +++ b/app/client/src/widgets/ProgressBarWidget/widget/index.tsx @@ -61,7 +61,13 @@ class ProgressBarWidget extends BaseWidget< isTriggerProperty: false, validation: { type: ValidationTypes.NUMBER, - params: { min: 1, max: 100, default: 1, natural: true }, + params: { + min: 1, + max: 100, + default: 1, + natural: true, + passThroughOnZero: false, + }, }, hidden: (props: ProgressBarWidgetProps) => { return props.barType !== BarType.DETERMINATE; diff --git a/app/client/src/widgets/ProgressWidget/widget/index.tsx b/app/client/src/widgets/ProgressWidget/widget/index.tsx index 724b689a17..1fce15a841 100644 --- a/app/client/src/widgets/ProgressWidget/widget/index.tsx +++ b/app/client/src/widgets/ProgressWidget/widget/index.tsx @@ -75,7 +75,13 @@ class ProgressWidget extends BaseWidget { isTriggerProperty: false, validation: { type: ValidationTypes.NUMBER, - params: { min: 1, max: 100, default: 1, natural: true }, + params: { + min: 1, + max: 100, + default: 1, + natural: true, + passThroughOnZero: false, + }, }, hidden: (props: ProgressWidgetProps) => props.isIndeterminate, dependencies: ["isIndeterminate"], diff --git a/app/client/src/workers/validations.test.ts b/app/client/src/workers/validations.test.ts index 0534b35502..894561697b 100644 --- a/app/client/src/workers/validations.test.ts +++ b/app/client/src/workers/validations.test.ts @@ -284,6 +284,22 @@ describe("Validate Validators", () => { }); }); + it("Validates number with passThroughOnZero", () => { + const config: any = { + type: ValidationTypes.NUMBER, + params: { + min: 1, + max: 4, + }, + }; + + expect(validate(config, -1, DUMMY_WIDGET).parsed).toStrictEqual(-1); + expect(validate(config, 0, DUMMY_WIDGET).parsed).toStrictEqual(0); + + config.params.passThroughOnZero = false; + expect(validate(config, 0, DUMMY_WIDGET).parsed).toStrictEqual(1); + }); + it("correctly validates number when required is true", () => { const config = { type: ValidationTypes.NUMBER, diff --git a/app/client/src/workers/validations.ts b/app/client/src/workers/validations.ts index b035c891b4..ec14fcf8a3 100644 --- a/app/client/src/workers/validations.ts +++ b/app/client/src/workers/validations.ts @@ -572,7 +572,12 @@ export const VALIDATORS: Record = { if (parsed < Number(config.params.min)) { return { isValid: false, - parsed: parsed || config.params.min || 0, + parsed: + // passThroughOnZero is introduced to resolve a bug and to not break existing apps + // Refer: https://github.com/appsmithorg/appsmith/issues/17472#issuecomment-1281818238 + config.params.passThroughOnZero === false + ? parsed || config.params.min || 0 + : parsed ?? config.params.min ?? 0, messages: [`Minimum allowed value: ${config.params.min}`], }; }