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

Parallel validation error thrown in post validate hook if re-validating #8597

Closed
m-weeks opened this issue Feb 14, 2020 · 1 comment
Closed
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 Feb 14, 2020

Do you want to request a feature or report a bug?
bug

What is the current behavior?
I just recently upgraded from 5.8.4 to 5.9, and the new Parellel validation error that was added in 5.8.5 is causing issues with some functionality I have.

When calling this.validate in a post validate hook, a Parallel validation error will be thrown. My guess is because this.$__.validating must still be set while the hook is running.

I'm not sure if this would be the desired behavior, what do you think? Perhaps the way I am writing the functionality would be better done another way, but I hadn't really thought of one.

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

Given a schema:

var EmployeeSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  employeeNumber: {
    type: String,
    validate: {
      validator: function() { return v.length > 5; },
      message: function(props) {
        return `${props.path} must be 5 characters long'`;
       }
    }
  }
});

For theoretical purposes similar to mine, let's say employeeNumber must be retrieved from an external API. This is how I have done it:

EmployeeSchema.post('validate', async function() {
  if (!this.employeeNumber && !this._employeeNumberRetrieved) {
    this.employeeNumber = await getEmployeeNumberFromApi(this.name);
    this._employeeNumberRetrieved = true;
    await this.validate();
  }
});

In my case, it isn't ideal to fetch this data if the other fields are invalid, because it's just going to get thrown out anyway, which is why I am doing it in a post validate hook. But I need to handle the case where if something about the returned employee number is invalid, which is why validation would need to be repeated.

What is the expected behavior?

A document should be able to be re-validated in a post('validate') hook

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.9.0
Mongodb 4.0.6

@vkarpov15 vkarpov15 added this to the 5.9.2 milestone Feb 19, 2020
@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 Feb 19, 2020
vkarpov15 added a commit that referenced this issue Feb 20, 2020
@vkarpov15
Copy link
Collaborator

Fix will be in v5.9.2. No reason that we can't allow calling validate() in post('validate') middleware since by the time post validate middleware runs, we've already copied over validation errors so you can use Document#invalidate() again.

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