fix: Number validation passthrough behaviour on 0 (#18001)
This commit is contained in:
parent
d5d2bf92bb
commit
d15bbe0a5e
|
|
@ -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.
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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"],
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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}`],
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user