Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn for page schema conflicts #4798

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 shares the same field name, but has a different type. This may cause errors or other problems when an editor switches page types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about: "Warns users when two page types have the same field name, but a different field type"

To clarify what the issue is


### 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
Loading