Validate readonly

This commit is contained in:
Adria Navarro 2024-06-03 10:29:26 +02:00
parent d73d7113ae
commit 91c20213dc
2 changed files with 17 additions and 11 deletions

View File

@ -336,6 +336,7 @@ describe.each([
tableId: table._id!,
schema: {
name: {
visible: true,
readonly: true,
},
},
@ -344,7 +345,7 @@ describe.each([
await config.api.viewV2.create(newView, {
status: 400,
body: {
message: 'You can\'t make the required field "name" read only',
message: 'You can\'t make read only the required field "name"',
status: 400,
},
})

View File

@ -39,9 +39,7 @@ async function guardViewSchema(
tableId: string,
viewSchema?: Record<string, ViewUIFieldMetadata>
) {
if (!viewSchema || !Object.keys(viewSchema).length) {
return
}
viewSchema ??= {}
const table = await sdk.tables.getTable(tableId)
for (const field of Object.keys(viewSchema)) {
@ -61,13 +59,6 @@ async function guardViewSchema(
throw new HTTPError(`Readonly fields are not enabled`, 400)
}
if (isRequired(tableSchemaField.constraints)) {
throw new HTTPError(
`You can't make the required field "${field}" read only`,
400
)
}
if (!viewSchema[field].visible) {
throw new HTTPError(
`Field "${field}" must be visible if you want to make it readonly`,
@ -76,6 +67,20 @@ async function guardViewSchema(
}
}
}
for (const field of Object.values(table.schema)) {
if (!isRequired(field.constraints)) {
continue
}
const viewSchemaField = viewSchema[field.name]
if (viewSchemaField?.readonly) {
throw new HTTPError(
`You can't make read only the required field "${field.name}"`,
400
)
}
}
}
export async function create(