-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,26 @@ | |||
module.exports = { |
There was a problem hiding this comment.
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.
module.exports = { | ||
options: { | ||
forbiddenSlugs: [ | ||
'/evil-page', |
There was a problem hiding this comment.
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.
handlers(self) { | ||
return { | ||
beforeSave: { | ||
checkForbiddenSlugs(req, doc) { |
There was a problem hiding this comment.
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.
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.
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).
e.path = 'slug'; | ||
throw self.apos.error('invalid', { | ||
errors: [ | ||
e |
There was a problem hiding this comment.
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.
This PR on one of our basic starter kits demonstrates how to forbid certain slugs by generating an error on the server side. A
beforeSave
handler on@apostrophecms/doc-type
, the base class of all doc type modules, is used to achieve the effect.