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

demonstrate how to forbid certain slugs #59

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
25 changes: 25 additions & 0 deletions modules/@apostrophecms/doc-type/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Placing this file in modules/@apostrophecms/doc-type/index.js causes Apostrophe to load it as additional configuration for @apostrophecms/doc-type, the base class of all modules.

options: {
forbiddenSlugs: [
'/evil-page',
Copy link
Member Author

Choose a reason for hiding this comment

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

Change this list to suit your needs.

'evil-piece'
]
},
handlers(self) {
return {
beforeSave: {
checkForbiddenSlugs(req, doc) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Each beforeSave handler must have a descriptive, unique name.

if (self.options.forbiddenSlugs.includes(doc.slug)) {
const e = self.apos.error('invalid', 'That slug is reserved.');
e.path = 'slug';
Copy link
Member Author

Choose a reason for hiding this comment

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

Tag the error with its path in the schema (the name of the field concerned).

throw self.apos.error('invalid', {
errors: [
e
Copy link
Member Author

Choose a reason for hiding this comment

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

Including it in an errors array in the final error object causes the Apostrophe UI to loop through and find it and display it in the right place, along with any errors you wish to report on other fields.

]
});
}
}
}
}
}
};