fix: Number validation passthrough behaviour on 0 (#18001)

This commit is contained in:
Aswath K 2022-11-01 18:17:07 +05:30 committed by GitHub
parent d5d2bf92bb
commit d15bbe0a5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 50 additions and 6 deletions

View File

@ -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.
};

View File

@ -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,
},
},
},
],

View File

@ -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,
},
},
},
{

View File

@ -256,7 +256,7 @@ class InputWidget extends BaseInputWidget<InputWidgetProps, WidgetState> {
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;

View File

@ -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;

View File

@ -75,7 +75,13 @@ class ProgressWidget extends BaseWidget<ProgressWidgetProps, WidgetState> {
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"],

View File

@ -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,

View File

@ -572,7 +572,12 @@ export const VALIDATORS: Record<ValidationTypes, Validator> = {
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}`],
};
}