Merge pull request #14733 from Budibase/view-calculation-validation-3

This commit is contained in:
Sam Rose 2024-10-09 14:00:01 +01:00 committed by GitHub
commit 710c092ae3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import {
NumericCalculationFieldMetadata, NumericCalculationFieldMetadata,
ViewV2Schema, ViewV2Schema,
ViewV2Type, ViewV2Type,
JsonTypes,
} from "@budibase/types" } from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests" import { generator, mocks } from "@budibase/backend-core/tests"
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
@ -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", () => { describe("update", () => {

View File

@ -1,6 +1,8 @@
import { import {
CalculationType, CalculationType,
canGroupBy,
FieldType, FieldType,
isNumeric,
PermissionLevel, PermissionLevel,
RelationSchemaField, RelationSchemaField,
RenameColumn, RenameColumn,
@ -103,7 +105,7 @@ async function guardCalculationViewSchema(
) )
} }
if (!isCount && !helpers.schema.isNumeric(targetSchema)) { if (!isCount && !isNumeric(targetSchema.type)) {
throw new HTTPError( throw new HTTPError(
`Calculation field "${name}" references field "${schema.field}" which is not a numeric field`, `Calculation field "${name}" references field "${schema.field}" which is not a numeric field`,
400 400
@ -120,6 +122,13 @@ async function guardCalculationViewSchema(
400 400
) )
} }
if (!canGroupBy(targetSchema.type)) {
throw new HTTPError(
`Grouping by fields of type "${targetSchema.type}" is not supported`,
400
)
}
} }
} }

View File

@ -127,6 +127,26 @@ export const JsonTypes = [
FieldType.ARRAY, 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 { export interface RowAttachment {
size: number size: number
name: string name: string