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

SchemaType.validate does not allow a function to be used for a message if a custom type is not defined #8360

Closed
m-weeks opened this issue Nov 19, 2019 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@m-weeks
Copy link

m-weeks commented Nov 19, 2019

Do you want to request a feature or report a bug?
bug
What is the current behavior?
SchemaType.validate does not allow a function to be sent as a message if a custom type is not defined

If the current behavior is a bug, please provide the steps to reproduce.

So if I have the following for a custom schema type:

function Email (path, options) {
  mongoose.SchemaTypes.String.call(this, path, options)
  this.validate(val => validateEmail(val), (error) => `${error.path} is invalid`)
}

An error message won't be returned when validation fails.
Now If I define a custom type:

function Email (path, options) {
  mongoose.SchemaTypes.String.call(this, path, options)
  this.validate(val => validateEmail(val), (error) => `${error.path} is invalid`, 'email')

it works as expected.

I believe the error is caused at https://github.com/Automattic/mongoose/blob/master/lib/schematype.js#L679 (if (message instanceof Object && !type) ) because apparently <function> instanceof Object is true. I would imagine typeof message === 'object' would do in its place

What is the expected behavior?
The function should be called either way, not just if there is a custom type

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node: 10.16.0
Mongoose: 5.7.11
MongoDB: 4.0.6

@vkarpov15 vkarpov15 added this to the 5.7.14 milestone Dec 1, 2019
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Dec 1, 2019
vkarpov15 added a commit that referenced this issue Dec 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants