Merge pull request #13841 from Budibase/BUDI-8282/validate-configuration-for-hidden-views

Validate hidden configuration for views
This commit is contained in:
Adria Navarro 2024-06-04 11:59:40 +02:00 committed by GitHub
commit eaa1af7da2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 3 deletions

View File

@ -117,6 +117,9 @@ describe.each([
const newView: CreateViewRequest = { const newView: CreateViewRequest = {
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: {
id: { visible: true },
},
} }
const res = await config.api.viewV2.create(newView) const res = await config.api.viewV2.create(newView)
@ -145,6 +148,7 @@ describe.each([
type: SortType.STRING, type: SortType.STRING,
}, },
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
}, },
@ -155,6 +159,7 @@ describe.each([
expect(res).toEqual({ expect(res).toEqual({
...newView, ...newView,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
}, },
@ -169,6 +174,11 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: {
name: "id",
type: FieldType.NUMBER,
visible: true,
},
Price: { Price: {
name: "Price", name: "Price",
type: FieldType.NUMBER, type: FieldType.NUMBER,
@ -190,6 +200,7 @@ describe.each([
expect(createdView).toEqual({ expect(createdView).toEqual({
...newView, ...newView,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
order: 1, order: 1,
@ -206,6 +217,12 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: {
name: "id",
type: FieldType.AUTO,
autocolumn: true,
visible: true,
},
Price: { Price: {
name: "Price", name: "Price",
type: FieldType.NUMBER, type: FieldType.NUMBER,
@ -229,6 +246,7 @@ describe.each([
tableId: table._id!, tableId: table._id!,
primaryDisplay: generator.word(), primaryDisplay: generator.word(),
schema: { schema: {
id: { visible: true },
Price: { visible: true }, Price: { visible: true },
Category: { visible: false }, Category: { visible: false },
}, },
@ -238,6 +256,7 @@ describe.each([
expect(res).toEqual({ expect(res).toEqual({
...newView, ...newView,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
}, },
@ -252,6 +271,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
nonExisting: { nonExisting: {
visible: true, visible: true,
}, },
@ -290,6 +310,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
name: { name: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -303,6 +324,7 @@ describe.each([
const res = await config.api.viewV2.create(newView) const res = await config.api.viewV2.create(newView)
expect(res.schema).toEqual({ expect(res.schema).toEqual({
id: { visible: true },
name: { name: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -335,6 +357,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
name: { name: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -372,6 +395,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
name: { name: {
visible: false, visible: false,
readonly: true, readonly: true,
@ -410,6 +434,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
name: { name: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -437,6 +462,9 @@ describe.each([
view = await config.api.viewV2.create({ view = await config.api.viewV2.create({
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: {
id: { visible: true },
},
}) })
}) })
@ -485,6 +513,7 @@ describe.each([
type: SortType.STRING, type: SortType.STRING,
}, },
schema: { schema: {
id: { visible: true },
Category: { Category: {
visible: false, visible: false,
}, },
@ -502,7 +531,7 @@ describe.each([
schema: { schema: {
...table.schema, ...table.schema,
id: expect.objectContaining({ id: expect.objectContaining({
visible: false, visible: true,
}), }),
Category: expect.objectContaining({ Category: expect.objectContaining({
visible: false, visible: false,
@ -599,6 +628,9 @@ describe.each([
const anotherView = await config.api.viewV2.create({ const anotherView = await config.api.viewV2.create({
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: {
id: { visible: true },
},
}) })
const result = await config const result = await config
.request!.put(`/api/v2/views/${anotherView.id}`) .request!.put(`/api/v2/views/${anotherView.id}`)
@ -617,6 +649,7 @@ describe.each([
const updatedView = await config.api.viewV2.update({ const updatedView = await config.api.viewV2.update({
...view, ...view,
schema: { schema: {
...view.schema,
Price: { Price: {
name: "Price", name: "Price",
type: FieldType.NUMBER, type: FieldType.NUMBER,
@ -636,6 +669,7 @@ describe.each([
expect(updatedView).toEqual({ expect(updatedView).toEqual({
...view, ...view,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
order: 1, order: 1,
@ -652,6 +686,7 @@ describe.each([
{ {
...view, ...view,
schema: { schema: {
...view.schema,
Price: { Price: {
name: "Price", name: "Price",
type: FieldType.NUMBER, type: FieldType.NUMBER,
@ -675,6 +710,7 @@ describe.each([
view = await config.api.viewV2.update({ view = await config.api.viewV2.update({
...view, ...view,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -697,6 +733,7 @@ describe.each([
view = await config.api.viewV2.update({ view = await config.api.viewV2.update({
...view, ...view,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
readonly: true, readonly: true,
@ -711,6 +748,7 @@ describe.each([
const res = await config.api.viewV2.update({ const res = await config.api.viewV2.update({
...view, ...view,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
readonly: false, readonly: false,
@ -721,6 +759,7 @@ describe.each([
expect.objectContaining({ expect.objectContaining({
...view, ...view,
schema: { schema: {
id: { visible: true },
Price: { Price: {
visible: true, visible: true,
readonly: false, readonly: false,
@ -738,6 +777,9 @@ describe.each([
view = await config.api.viewV2.create({ view = await config.api.viewV2.create({
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: {
id: { visible: true },
},
}) })
}) })
@ -760,6 +802,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
Price: { visible: false }, Price: { visible: false },
Category: { visible: true }, Category: { visible: true },
}, },
@ -782,6 +825,7 @@ describe.each([
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
id: { visible: true },
Price: { visible: true, readonly: true }, Price: { visible: true, readonly: true },
}, },
}) })
@ -817,6 +861,7 @@ describe.each([
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: { schema: {
id: { visible: true },
Country: { Country: {
visible: true, visible: true,
}, },
@ -851,6 +896,7 @@ describe.each([
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: { schema: {
id: { visible: true },
two: { visible: true }, two: { visible: true },
}, },
}) })
@ -876,6 +922,7 @@ describe.each([
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: { schema: {
id: { visible: true },
one: { visible: true, readonly: true }, one: { visible: true, readonly: true },
two: { visible: true }, two: { visible: true },
}, },
@ -917,6 +964,7 @@ describe.each([
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
schema: { schema: {
id: { visible: true },
one: { visible: true, readonly: true }, one: { visible: true, readonly: true },
two: { visible: true }, two: { visible: true },
}, },
@ -984,6 +1032,7 @@ describe.each([
rows.map(r => ({ rows.map(r => ({
_viewId: view.id, _viewId: view.id,
tableId: table._id, tableId: table._id,
id: r.id,
_id: r._id, _id: r._id,
_rev: r._rev, _rev: r._rev,
...(isInternal ...(isInternal
@ -1024,6 +1073,7 @@ describe.each([
}, },
], ],
schema: { schema: {
id: { visible: true },
two: { visible: true }, two: { visible: true },
}, },
}) })
@ -1035,6 +1085,7 @@ describe.each([
{ {
_viewId: view.id, _viewId: view.id,
tableId: table._id, tableId: table._id,
id: two.id,
two: two.two, two: two.two,
_id: two._id, _id: two._id,
_rev: two._rev, _rev: two._rev,
@ -1188,7 +1239,11 @@ describe.each([
describe("sorting", () => { describe("sorting", () => {
let table: Table let table: Table
const viewSchema = { age: { visible: true }, name: { visible: true } } const viewSchema = {
id: { visible: true },
age: { visible: true },
name: { visible: true },
}
beforeAll(async () => { beforeAll(async () => {
table = await config.api.table.save( table = await config.api.table.save(

View File

@ -74,7 +74,15 @@ async function guardViewSchema(
} }
const viewSchemaField = viewSchema[field.name] const viewSchemaField = viewSchema[field.name]
if (viewSchemaField?.readonly) {
if (!viewSchemaField?.visible) {
throw new HTTPError(
`You can't hide the required field "${field.name}"`,
400
)
}
if (viewSchemaField.readonly) {
throw new HTTPError( throw new HTTPError(
`You can't make field "${field.name}" readonly because it is a required field.`, `You can't make field "${field.name}" readonly because it is a required field.`,
400 400