diff --git a/app/client/src/widgets/SelectWidget/widget/index.test.tsx b/app/client/src/widgets/SelectWidget/widget/index.test.tsx index 9d697ce970..7c11839c64 100644 --- a/app/client/src/widgets/SelectWidget/widget/index.test.tsx +++ b/app/client/src/widgets/SelectWidget/widget/index.test.tsx @@ -13,6 +13,40 @@ describe("defaultOptionValueValidation - ", () => { messages: [""], }); }); + it("should get tested with number", () => { + const testValues = [ + [ + "{{1}}", + { + isValid: true, + parsed: 1, + messages: [""], + }, + ], + [ + "1", + { + isValid: true, + parsed: "1", + messages: [""], + }, + ], + [ + 1, + { + isValid: true, + parsed: 1, + messages: [""], + }, + ], + ]; + + testValues.forEach(([input, expected]) => { + expect( + defaultOptionValueValidation(input, {} as SelectWidgetProps, _), + ).toEqual(expected); + }); + }); it("should get tested with simple string", () => { const input = "green"; @@ -43,6 +77,40 @@ describe("defaultOptionValueValidation - ", () => { messages: [""], }); }); + it("should get tested with valid strings", () => { + const testValues = [ + [ + "undefined", + { + isValid: true, + parsed: "undefined", + messages: [""], + }, + ], + [ + "null", + { + isValid: true, + parsed: "null", + messages: [""], + }, + ], + [ + "true", + { + isValid: true, + parsed: "true", + messages: [""], + }, + ], + ]; + + testValues.forEach(([input, expected]) => { + expect( + defaultOptionValueValidation(input, {} as SelectWidgetProps, _), + ).toEqual(expected); + }); + }); it("should get tested with invalid values", () => { const testValues = [ diff --git a/app/client/src/widgets/SelectWidget/widget/index.tsx b/app/client/src/widgets/SelectWidget/widget/index.tsx index 1acb229f1a..581b08162a 100644 --- a/app/client/src/widgets/SelectWidget/widget/index.tsx +++ b/app/client/src/widgets/SelectWidget/widget/index.tsx @@ -11,12 +11,12 @@ import { import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory"; import { MinimumPopupRows, GRID_DENSITY_MIGRATION_V1 } from "widgets/constants"; import { AutocompleteDataType } from "utils/autocomplete/TernServer"; -import { findIndex, isArray, isNumber, isString } from "lodash"; +import { findIndex, isArray, isNumber, isString, LoDashStatic } from "lodash"; export function defaultOptionValueValidation( value: unknown, props: SelectWidgetProps, - _: any, + _: LoDashStatic, ): ValidationResponse { let isValid; let parsed; @@ -40,7 +40,10 @@ export function defaultOptionValueValidation( */ if (typeof value === "string") { try { - value = JSON.parse(value); + const parsedValue = JSON.parse(value); + if (_.isObject(parsedValue)) { + value = parsedValue; + } } catch (e) {} } @@ -53,7 +56,7 @@ export function defaultOptionValueValidation( } else { isValid = false; parsed = {}; - message = `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`; + message = `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`; } return {