-
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
Changes from 1 commit
f9daeb4
68194f0
ff70f79
fb235f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,6 @@ | |
|
||
var mongoose = require('mongoose'); | ||
|
||
// Bind custom message for mongoose if it doesn't already exist | ||
if (mongoose.Error.messages.general.unique === undefined) { | ||
mongoose.Error.messages.general.unique = | ||
'Path `{PATH}` ({VALUE}) is not unique.'; | ||
} | ||
|
||
var errorRegex = /index:\s*(?:.+?\.\$)?(.*?)\s*dup/; | ||
var indexesCache = {}; | ||
|
||
|
@@ -56,9 +50,10 @@ function getIndexes(collection) { | |
* @param {Collection} collection Mongoose collection. | ||
* @param {Object} values Hashmap containing data about duplicated values | ||
* @param {Object} messages Map fields to unique error messages | ||
* @param {String} message Default message formatter string | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename |
||
// Try to recover the list of duplicated fields | ||
var onSuberrors = global.Promise.resolve({}); | ||
|
||
|
@@ -86,7 +81,7 @@ function beautify(error, collection, values, messages) { | |
if (typeof messages[path] === 'string') { | ||
props.message = messages[path]; | ||
} else { | ||
props.message = mongoose.Error.messages.general.unique; | ||
props.message = message; | ||
} | ||
|
||
suberrors[path] = new mongoose.Error.ValidatorError(props); | ||
|
@@ -105,9 +100,14 @@ function beautify(error, collection, values, messages) { | |
}); | ||
} | ||
|
||
module.exports = function (schema) { | ||
module.exports = function (schema, options) { | ||
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 commentThe 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 |
||
} | ||
|
||
// Fetch error messages defined in the "unique" field, | ||
// store them for later use and replace them with true | ||
for (key in tree) { | ||
|
@@ -161,7 +161,7 @@ module.exports = function (schema) { | |
values = doc; | ||
} | ||
|
||
beautify(error, collection, values, messages) | ||
beautify(error, collection, values, messages, options.message) | ||
.then(next) | ||
.catch(function (beautifyError) { | ||
setTimeout(function () { | ||
|
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 theunique
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 renamingoptions.message
tooptions.defaultMessage
will make things clearer.