Skip to content

Commit

Permalink
warn for page schema conflicts (#4798)
Browse files Browse the repository at this point in the history
* warn for page schema conflicts

* update warning message

* update changelog

---------

Co-authored-by: Harouna Traoré <[email protected]>
  • Loading branch information
haroun and haroun authored Nov 14, 2024
1 parent 7a7f8a4 commit ca1e5df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ did not actually use any noncompliant cookie names or values, so there was no vu
* It's possible now to target the HMR build when registering via `template.append` and `template.prepend`. Use `when: 'hmr:public'` or `when: 'hmr:apos'` that will be evaluated against the current asset `options.hmr` configuration.
* Adds asset module option `options.modulePreloadPolyfill` (default `true`) to allow disabling the polyfill preload for e.g. external front-ends.
* Adds `bundleMarkup` to the data sent to the external front-end, containing all markup for injecting Apostrophe UI in the front-end.
* Warns users when two page types have the same field name, but a different field type. This may cause errors or other problems when an editor switches page types.

### Changes

Expand Down
31 changes: 31 additions & 0 deletions modules/@apostrophecms/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,37 @@ module.exports = {
}
}
},
detectSchemaConflicts() {
for (const left of self.typeChoices) {
for (const right of self.typeChoices) {
const diff = compareSchema(left, right);
if (diff.size) {
self.apos.util.warnDev(`The page type "${left.name}" has a conflict with "${right.name}" (${formatDiff(diff)}). This may cause errors or other problems when an editor switches page types.`);
}
}
}
function compareSchema(left, right) {
const conflicts = new Map();
if (left.name === right.name) {
return conflicts;
}

const leftSchema = self.apos.modules[left.name].schema;
const rightSchema = self.apos.modules[right.name].schema;
for (const leftField of leftSchema) {
const rightField = rightSchema.find(field => field.name === leftField.name);
if (rightField && leftField.type !== rightField.type) {
conflicts.set(leftField.name, [ leftField.type, rightField.type ]);
}
}

return conflicts;
}
function formatDiff(diff) {
return Array.from(diff.entries())
.map(([ entry, [ left, right ] ]) => `${entry}:${left} vs ${entry}:${right}`);
}
},
async manageOrphans() {
const managed = self.apos.doc.getManaged();

Expand Down

0 comments on commit ca1e5df

Please sign in to comment.