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

Update validators #12655

Closed
2 tasks done
HunSpeedi opened this issue Nov 4, 2022 · 0 comments · Fixed by #12738
Closed
2 tasks done

Update validators #12655

HunSpeedi opened this issue Nov 4, 2022 · 0 comments · Fixed by #12738
Assignees
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@HunSpeedi
Copy link

HunSpeedi commented Nov 4, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

In the validation docs you mention that:

However, when running update validators, the document being updated may not be in the server's memory, so by default the value of this is not defined.

If I understand correctly now Mongoose always uses context = 'query'.
So wouldn't be the following statement more accurate:

However, when running update validators, this refers to the query object instead of the document.

Also then an additional example could be added:

 /**
   * Since Mongoose always uses `context = 'query'` when running update validators `this` refers to the query object!
   */

   it('Update validation with query object', function(done) {
    // acquit:ignore:start
    const toySchema = new Schema({
      color: String,
      name: String
    });
    // acquit:ignore:end
    toySchema.path('color').validate(function(value) {
      // When running update validators `this` refers to the query object.
      if (this.getUpdate().$set.name.toLowerCase().indexOf('red') !== -1) {
        return value === 'red';
      }
      return true;
    });

    const Toy = db.model('ActionFigure', toySchema);

    const update = { color: 'blue', name: 'Red Power Ranger' };
    const opts = { runValidators: true };

    Toy.updateOne({}, update, opts, function(error) {
      assert.ok(error.errors['color']);
      // acquit:ignore:start
      done();
      // acquit:ignore:end
    });
  });

If my conclusion is right I can make a PR with the mentioned changes.
Thanks in advance!!

@hasezoey hasezoey added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Nov 8, 2022
@vkarpov15 vkarpov15 added this to the 6.7.3 milestone Nov 9, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.3, 6.7.4, 6.7.5 Nov 22, 2022
@IslandRhythms IslandRhythms self-assigned this Nov 29, 2022
@IslandRhythms IslandRhythms linked a pull request Nov 29, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants