Skip to content

Commit

Permalink
fix(db-postgres): validateExistingBlockIsIdentical with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Jan 14, 2024
1 parent f9dda62 commit 3b88adc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
46 changes: 24 additions & 22 deletions packages/db-postgres/src/schema/validateExistingBlockIsIdentical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const getFlattenedFieldNames = (fields: Field[], prefix: string = ''): string[]
return fields.reduce((fieldsToUse, field) => {
let fieldPrefix = prefix

if (field.type === 'blocks') {
if (
field.type === 'blocks' ||
field.type === 'array' ||
('hasMany' in field && field.hasMany === true)
) {
return fieldsToUse
}

Expand Down Expand Up @@ -54,29 +58,27 @@ export const validateExistingBlockIsIdentical = ({
rootTableName,
table,
}: Args): void => {
if (table) {
const fieldNames = getFlattenedFieldNames(block.fields)
const fieldNames = getFlattenedFieldNames(block.fields)

const missingField =
// ensure every field from the config is in the matching table
fieldNames.find((name) => Object.keys(table).indexOf(name) === -1) ||
// ensure every table column is matched for every field from the config
Object.keys(table).find((fieldName) => {
if (!['_locale', '_order', '_parentID', '_path', '_uuid'].includes(fieldName)) {
return fieldNames.indexOf(fieldName) === -1
}
})
const missingField =
// ensure every field from the config is in the matching table
fieldNames.find((name) => Object.keys(table).indexOf(name) === -1) ||
// ensure every table column is matched for every field from the config
Object.keys(table).find((fieldName) => {
if (!['_locale', '_order', '_parentID', '_path', '_uuid'].includes(fieldName)) {
return fieldNames.indexOf(fieldName) === -1
}
})

if (missingField) {
throw new InvalidConfiguration(
`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One block includes the field ${missingField}, while the other block does not.`,
)
}
if (missingField) {
throw new InvalidConfiguration(
`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One block includes the field ${missingField}, while the other block does not.`,
)
}

if (Boolean(localized) !== Boolean(table._locale)) {
throw new InvalidConfiguration(
`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One is localized, but another is not. Block schemas of the same name must match exactly.`,
)
}
if (Boolean(localized) !== Boolean(table._locale)) {
throw new InvalidConfiguration(
`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One is localized, but another is not. Block schemas of the same name must match exactly.`,
)
}
}
16 changes: 16 additions & 0 deletions test/fields/collections/Blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ const BlockFields: CollectionConfig = {
},
],
},
{
slug: 'block-b',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title2',
type: 'text',
required: true,
},
],
},
],
},
],
},
{
Expand Down

0 comments on commit 3b88adc

Please sign in to comment.