Ensure schema is sanitised when updating primary display column

This commit is contained in:
Andrew Kingston 2024-09-24 14:47:53 +01:00
parent 06c8e5f815
commit f493025d94
No known key found for this signature in database
2 changed files with 15 additions and 6 deletions

View File

@ -284,7 +284,7 @@
// Ensure the field is not required if we have a default value
if (saveColumn.default) {
saveColumn.constraints.presence = false
saveColumn.constraints.presence = { allowEmpty: true }
}
// Delete default value for options fields if the option is no longer available
@ -298,7 +298,7 @@
// Ensure primary display columns are always required and don't have default values
if (primaryDisplay) {
saveColumn.constraints.presence = true
saveColumn.constraints.presence = { allowEmpty: false }
delete saveColumn.default
}

View File

@ -1,6 +1,7 @@
import { derived, get } from "svelte/store"
import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch"
import { memo } from "../../../utils"
import { cloneDeep } from "lodash"
export const createStores = () => {
const definition = memo(null)
@ -161,10 +162,18 @@ export const createActions = context => {
// Updates the datasources primary display column
const changePrimaryDisplay = async column => {
return await saveDefinition({
...get(definition),
primaryDisplay: column,
})
let newDefinition = cloneDeep(get(definition))
// Update primary display
newDefinition.primaryDisplay = column
// Sanitise schema to ensure field is required and has no default value
if (!newDefinition.schema[column].constraints) {
newDefinition.schema[column].constraints = {}
}
newDefinition.schema[column].constraints.presence = { allowEmpty: false }
delete newDefinition.schema[column].default
return await saveDefinition(newDefinition)
}
// Adds a schema mutation for a single field