From bc96e61baf170601a304e9a64c585c04ab605ce7 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 2 Oct 2024 12:57:10 +0100 Subject: [PATCH 1/3] Avoid mutating path array in-place --- packages/client/src/components/app/forms/Form.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/components/app/forms/Form.svelte b/packages/client/src/components/app/forms/Form.svelte index 5522bd4b46..fe02dc665d 100644 --- a/packages/client/src/components/app/forms/Form.svelte +++ b/packages/client/src/components/app/forms/Form.svelte @@ -63,7 +63,7 @@ // Look up the component tree and find something that is provided by an // ancestor that matches our datasource. This is for backwards compatibility // as previously we could use the "closest" context. - for (let id of path.reverse().slice(1)) { + for (let id of path.toReversed().slice(1)) { // Check for matching view datasource if ( dataSource.type === "viewV2" && From c4a98832cbc6ee8da82ade1ad64eee85e85ffd4b Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 2 Oct 2024 15:24:04 +0100 Subject: [PATCH 2/3] Enable default values for options and array types --- packages/server/src/utilities/rowProcessor/index.ts | 6 ++++-- packages/shared-core/src/table.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 7332f8b244..e63750bff9 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -134,8 +134,10 @@ async function processDefaultValues(table: Table, row: Row) { for (const [key, schema] of Object.entries(table.schema)) { if ("default" in schema && schema.default != null && row[key] == null) { - const processed = await processString(schema.default, ctx) - + const processed = + typeof schema.default === "string" + ? await processString(schema.default, ctx) + : schema.default try { row[key] = coerce(processed, schema.type) } catch (err: any) { diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index 8a8069ce4d..57f6854604 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -53,8 +53,9 @@ const allowDefaultColumnByType: Record = { [FieldType.DATETIME]: true, [FieldType.LONGFORM]: true, [FieldType.STRING]: true, + [FieldType.OPTIONS]: true, + [FieldType.ARRAY]: true, - [FieldType.OPTIONS]: false, [FieldType.AUTO]: false, [FieldType.INTERNAL]: false, [FieldType.BARCODEQR]: false, @@ -64,7 +65,6 @@ const allowDefaultColumnByType: Record = { [FieldType.ATTACHMENTS]: false, [FieldType.ATTACHMENT_SINGLE]: false, [FieldType.SIGNATURE_SINGLE]: false, - [FieldType.ARRAY]: false, [FieldType.LINK]: false, [FieldType.BB_REFERENCE]: false, [FieldType.BB_REFERENCE_SINGLE]: false, From 3e3fd20207cb0139f403e25c3b4a5c977c2b19f4 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 2 Oct 2024 15:35:17 +0100 Subject: [PATCH 3/3] Add tests for new default value types --- .../server/src/api/routes/tests/row.spec.ts | 63 +++++++++++++++++++ .../types/src/documents/app/table/schema.ts | 2 + 2 files changed, 65 insertions(+) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 9ccf919ff8..f751942df9 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -695,6 +695,69 @@ describe.each([ }) }) + describe("options column", () => { + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + status: { + name: "status", + type: FieldType.OPTIONS, + default: "requested", + constraints: { + inclusion: ["requested", "approved"], + }, + }, + }, + }) + ) + }) + + it("creates a new row with a default value successfully", async () => { + const row = await config.api.row.save(table._id!, {}) + expect(row.status).toEqual("requested") + }) + + it("does not use default value if value specified", async () => { + const row = await config.api.row.save(table._id!, { + status: "approved", + }) + expect(row.status).toEqual("approved") + }) + }) + + describe("array column", () => { + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + food: { + name: "food", + type: FieldType.ARRAY, + default: ["apple", "orange"], + constraints: { + type: JsonFieldSubType.ARRAY, + inclusion: ["apple", "orange", "banana"], + }, + }, + }, + }) + ) + }) + + it("creates a new row with a default value successfully", async () => { + const row = await config.api.row.save(table._id!, {}) + expect(row.food).toEqual(["apple", "orange"]) + }) + + it("does not use default value if value specified", async () => { + const row = await config.api.row.save(table._id!, { + food: ["orange"], + }) + expect(row.food).toEqual(["orange"]) + }) + }) + describe("bindings", () => { describe("string column", () => { beforeAll(async () => { diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 6078f73d1d..f9d1a4c012 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -161,6 +161,7 @@ export interface OptionsFieldMetadata extends BaseFieldSchema { constraints: FieldConstraints & { inclusion: string[] } + default?: string } export interface ArrayFieldMetadata extends BaseFieldSchema { @@ -169,6 +170,7 @@ export interface ArrayFieldMetadata extends BaseFieldSchema { type: JsonFieldSubType.ARRAY inclusion: string[] } + default?: string[] } interface BaseFieldSchema extends UIFieldMetadata {