Skip to content

Commit

Permalink
Merge pull request #14733 from Budibase/view-calculation-validation-3
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho authored Oct 9, 2024
2 parents d29de7c + 6da0c29 commit 710c092
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
29 changes: 29 additions & 0 deletions packages/server/src/api/routes/tests/viewV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
NumericCalculationFieldMetadata,
ViewV2Schema,
ViewV2Type,
JsonTypes,
} from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests"
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
Expand Down Expand Up @@ -736,6 +737,34 @@ describe.each([
},
})
})

// We don't allow the creation of tables with most JsonTypes when using
// external datasources.
isInternal &&
it("cannot use complex types as group-by fields", async () => {
for (const type of JsonTypes) {
const field = { name: "field", type } as FieldSchema
const table = await config.api.table.save(
saveTableRequest({ schema: { field } })
)
await config.api.viewV2.create(
{
tableId: table._id!,
name: generator.guid(),
type: ViewV2Type.CALCULATION,
schema: {
field: { visible: true },
},
},
{
status: 400,
body: {
message: `Grouping by fields of type "${type}" is not supported`,
},
}
)
}
})
})

describe("update", () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/server/src/sdk/app/views/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
CalculationType,
canGroupBy,
FieldType,
isNumeric,
PermissionLevel,
RelationSchemaField,
RenameColumn,
Expand Down Expand Up @@ -103,7 +105,7 @@ async function guardCalculationViewSchema(
)
}

if (!isCount && !helpers.schema.isNumeric(targetSchema)) {
if (!isCount && !isNumeric(targetSchema.type)) {
throw new HTTPError(
`Calculation field "${name}" references field "${schema.field}" which is not a numeric field`,
400
Expand All @@ -120,6 +122,13 @@ async function guardCalculationViewSchema(
400
)
}

if (!canGroupBy(targetSchema.type)) {
throw new HTTPError(
`Grouping by fields of type "${targetSchema.type}" is not supported`,
400
)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/types/src/documents/app/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ export const JsonTypes = [
FieldType.ARRAY,
]

export const NumericTypes = [FieldType.NUMBER, FieldType.BIGINT]

export function isNumeric(type: FieldType) {
return NumericTypes.includes(type)
}

export const GroupByTypes = [
FieldType.STRING,
FieldType.LONGFORM,
FieldType.OPTIONS,
FieldType.NUMBER,
FieldType.BOOLEAN,
FieldType.DATETIME,
FieldType.BIGINT,
]

export function canGroupBy(type: FieldType) {
return GroupByTypes.includes(type)
}

export interface RowAttachment {
size: number
name: string
Expand Down

0 comments on commit 710c092

Please sign in to comment.