diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 5b335542c0..7b3619351a 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -386,7 +386,8 @@ describe.each([ await config.api.viewV2.create(newView, { status: 400, body: { - message: 'Field "name" cannot be readonly and not visible', + message: + 'Field "name" must be visible if you want to make it readonly', status: 400, }, }) @@ -686,7 +687,7 @@ describe.each([ }) mocks.licenses.useCloudFree() - await config.api.viewV2.create(view, { + await config.api.viewV2.update(view, { status: 400, body: { message: "Readonly fields are not enabled for your tenant", diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 2d0c046b08..da66e8ac18 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -43,37 +43,36 @@ async function guardViewSchema( return } const table = await sdk.tables.getTable(tableId) - if (viewSchema) { - for (const field of Object.keys(viewSchema)) { - const tableSchemaField = table.schema[field] - if (!tableSchemaField) { + + for (const field of Object.keys(viewSchema)) { + const tableSchemaField = table.schema[field] + if (!tableSchemaField) { + throw new HTTPError( + `Field "${field}" is not valid for the requested table`, + 400 + ) + } + + if (viewSchema[field].readonly) { + if (!(await features.isViewReadonlyColumnsEnabled())) { throw new HTTPError( - `Field "${field}" is not valid for the requested table`, + `Readonly fields are not enabled for your tenant`, 400 ) } - if (viewSchema[field].readonly) { - if (!(await features.isViewReadonlyColumnsEnabled())) { - throw new HTTPError( - `Readonly fields are not enabled for your tenant`, - 400 - ) - } + if (isRequired(tableSchemaField.constraints)) { + throw new HTTPError( + `Field "${field}" cannot be readonly as it is a required field`, + 400 + ) + } - if (isRequired(tableSchemaField.constraints)) { - throw new HTTPError( - `Field "${field}" cannot be readonly as it is a required field`, - 400 - ) - } - - if (!viewSchema[field].visible) { - throw new HTTPError( - `Field "${field}" cannot be readonly and not visible`, - 400 - ) - } + if (!viewSchema[field].visible) { + throw new HTTPError( + `Field "${field}" must be visible if you want to make it readonly`, + 400 + ) } } }