Conversation
| @@ -0,0 +1,26 @@ | |||
| module.exports = { | |||
There was a problem hiding this comment.
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.
| module.exports = { | ||
| options: { | ||
| forbiddenSlugs: [ | ||
| '/evil-page', |
There was a problem hiding this comment.
Change this list to suit your needs.
| handlers(self) { | ||
| return { | ||
| beforeSave: { | ||
| checkForbiddenSlugs(req, doc) { |
There was a problem hiding this comment.
Each beforeSave handler must have a descriptive, unique name.
| checkForbiddenSlugs(req, doc) { | ||
| if (self.options.forbiddenSlugs.includes(doc.slug)) { | ||
| const e = self.apos.error('invalid', 'That slug is reserved.'); | ||
| e.path = 'slug'; |
There was a problem hiding this comment.
Tag the error with its path in the schema (the name of the field concerned).
| e.path = 'slug'; | ||
| throw self.apos.error('invalid', { | ||
| errors: [ | ||
| e |
There was a problem hiding this comment.
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.
This PR on one of our basic starter kits demonstrates how to forbid certain slugs by generating an error on the server side. A
beforeSavehandler on@apostrophecms/doc-type, the base class of all doc type modules, is used to achieve the effect.