-
Notifications
You must be signed in to change notification settings - Fork 39
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
Fixed default message formatter (closes #41) #42
Conversation
Thanks for your help! As I understand this, it enables users to override the default message, something impossible when you use |
Correct. I don't specify a message in my schema but want to modify a
global default so that's why. Also we should still support a custom
message per schema prop because people may customize as they need.
…On Aug 8, 2017 6:57 AM, "Mattéo Delabre ✏️" ***@***.***> wrote:
Thanks for your help! As I understand this, it enables users to override
the default message, something impossible when you use
mongoose.Error.messages.general.unique? However, I can’t see the use case
for having two ways of overriding the default (in the Schema and in the
options). Do you have an example where this is useful?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#42 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAf7hQEthXEChtL5X0GJS1E5rtkpt8lwks5sWD8dgaJpZM4OwS7A>
.
|
@matteodelabre can you take a look at this? ty |
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.
Sorry for the delay! I think a few minor problems should be addressed first. Tell me what you think.
index.js
Outdated
var tree = schema.tree, key, messages = {}; | ||
|
||
options = options || {}; | ||
if (!options.message) { | ||
options.message = 'Path `{PATH}` ({VALUE}) is not unique.'; |
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.
To avoid confusion between the different ways to provide a custom error message, I suggest using options.defaultMessage
instead of options.message
.
index.js
Outdated
* @return {Promise.<ValidationError>} Beautified error message | ||
*/ | ||
function beautify(error, collection, values, messages) { | ||
function beautify(error, collection, values, messages, message) { |
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.
I think we should rename message
to defaultMessage
to avoid confusion between the singular message
and the plural messages
variable.
README.md
Outdated
|
||
The default value of `mongoose.Error.messages.general.unique` is ``"Path `{PATH}` ({VALUE}) is not unique."`` which adheres to [the Mongoose error message defaults](https://github.com/Automattic/mongoose/blob/master/lib/error/messages.js). | ||
The default value of `options.message` is ``"Path `{PATH}` ({VALUE}) is not unique."`` which adheres to [the Mongoose error message defaults](https://github.com/Automattic/mongoose/blob/master/lib/error/messages.js). |
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.
If we are going to introduce more API surface, I think we should be extra-clear on when one mechanism for overriding the message should be used instead of the other one. Currently, I have the feeling that options.message
and the custom message in the unique
field might feel redundant to the users.
In my opinion, the main use for options.message
is for when the plugin is used globally. If you want to, I can try to improve the docs after merging this PR. I think renaming options.message
to options.defaultMessage
will make things clearer.
@matteodelabre if I change |
We can always wrap that line if it becomes too long. I don’t think it’s clear enough for users that these are default options. No worries, I will make those changes by myself! :) |
What do you think? |
Looks good to me |
No description provided.