Fix default values when using multi-option column and supplying empty array.

This commit is contained in:
Sam Rose 2024-10-29 10:23:32 +00:00
parent 199c447bc4
commit b9b4f88e4a
No known key found for this signature in database
2 changed files with 20 additions and 1 deletions

View File

@ -763,12 +763,25 @@ describe.each([
expect(row.food).toEqual(["apple", "orange"]) expect(row.food).toEqual(["apple", "orange"])
}) })
it("creates a new row with a default value when given an empty list", async () => {
const row = await config.api.row.save(table._id!, { food: [] })
expect(row.food).toEqual(["apple", "orange"])
})
it("does not use default value if value specified", async () => { it("does not use default value if value specified", async () => {
const row = await config.api.row.save(table._id!, { const row = await config.api.row.save(table._id!, {
food: ["orange"], food: ["orange"],
}) })
expect(row.food).toEqual(["orange"]) expect(row.food).toEqual(["orange"])
}) })
it("resets back to its default value when empty", async () => {
let row = await config.api.row.save(table._id!, {
food: ["orange"],
})
row = await config.api.row.save(table._id!, { ...row, food: [] })
expect(row.food).toEqual(["apple", "orange"])
})
}) })
describe("user column", () => { describe("user column", () => {

View File

@ -20,6 +20,7 @@ import {
User, User,
ViewV2, ViewV2,
FeatureFlag, FeatureFlag,
Document,
} from "@budibase/types" } from "@budibase/types"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { import {
@ -134,7 +135,12 @@ async function processDefaultValues(table: Table, row: Row) {
} }
for (const [key, schema] of Object.entries(table.schema)) { for (const [key, schema] of Object.entries(table.schema)) {
if ("default" in schema && schema.default != null && row[key] == null) { const isEmpty =
row[key] == null ||
row[key] === "" ||
(Array.isArray(row[key]) && row[key].length === 0)
if ("default" in schema && schema.default != null && isEmpty) {
let processed: string | string[] let processed: string | string[]
if (Array.isArray(schema.default)) { if (Array.isArray(schema.default)) {
processed = schema.default.map(val => processStringSync(val, ctx)) processed = schema.default.map(val => processStringSync(val, ctx))