diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index c8035bd578..bc48b54b51 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -22,7 +22,7 @@ import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import merge from "lodash/merge" import { quotas } from "@budibase/pro" -import { roles } from "@budibase/backend-core" +import { db, roles } from "@budibase/backend-core" describe.each([ ["internal", undefined], @@ -840,6 +840,52 @@ describe.each([ }) ) }) + + it("updating schema will only validate modified field", async () => { + let view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, + Price: { + visible: true, + }, + Category: { visible: true }, + }, + }) + + // Update the view to an invalid state + const tableToUpdate = await config.api.table.get(table._id!) + ;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible = false + await db.getDB(config.appId!).put(tableToUpdate) + + view = await config.api.viewV2.get(view.id) + await config.api.viewV2.update({ + ...view, + schema: { + ...view.schema, + Price: { + visible: false, + }, + }, + }) + + expect(await config.api.viewV2.get(view.id)).toEqual( + expect.objectContaining({ + schema: { + id: expect.objectContaining({ + visible: false, + }), + Price: expect.objectContaining({ + visible: false, + }), + Category: expect.objectContaining({ + visible: true, + }), + }, + }) + ) + }) }) describe("delete", () => { diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 2d34a3f5a5..b6ac7b6f6b 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -9,7 +9,7 @@ import { import { HTTPError, db as dbCore } from "@budibase/backend-core" import { features } from "@budibase/pro" import { helpers } from "@budibase/shared-core" -import { cloneDeep } from "lodash" +import { cloneDeep } from "lodash/fp" import * as utils from "../../../db/utils" import { isExternalTableID } from "../../../integrations/utils" @@ -68,12 +68,21 @@ async function guardViewSchema( } } + const existingView = + table?.views && (table.views[view.name] as ViewV2 | undefined) + for (const field of Object.values(table.schema)) { if (!helpers.schema.isRequired(field.constraints)) { continue } const viewSchemaField = viewSchema[field.name] + const existingViewSchema = + existingView?.schema && existingView.schema[field.name] + if (!viewSchemaField && !existingViewSchema?.visible) { + // Supporting existing configs with required columns but hidden in views + continue + } if (!viewSchemaField?.visible) { throw new HTTPError(