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!, tableId: table._id!,
schema: { schema: {
name: { name: {
visible: true,
readonly: true, readonly: true,
}, },
}, },
@ -344,7 +345,7 @@ describe.each([
await config.api.viewV2.create(newView, { await config.api.viewV2.create(newView, {
status: 400, status: 400,
body: { 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, status: 400,
}, },
}) })

View File

@ -39,9 +39,7 @@ async function guardViewSchema(
tableId: string, tableId: string,
viewSchema?: Record<string, ViewUIFieldMetadata> viewSchema?: Record<string, ViewUIFieldMetadata>
) { ) {
if (!viewSchema || !Object.keys(viewSchema).length) { viewSchema ??= {}
return
}
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
for (const field of Object.keys(viewSchema)) { for (const field of Object.keys(viewSchema)) {
@ -61,13 +59,6 @@ async function guardViewSchema(
throw new HTTPError(`Readonly fields are not enabled`, 400) 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) { if (!viewSchema[field].visible) {
throw new HTTPError( throw new HTTPError(
`Field "${field}" must be visible if you want to make it readonly`, `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( export async function create(