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

Bug - Cloning a schematype loose the requiredValidator reference #8111

Closed
StphnDamon opened this issue Aug 29, 2019 · 1 comment
Closed

Bug - Cloning a schematype loose the requiredValidator reference #8111

StphnDamon opened this issue Aug 29, 2019 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@StphnDamon
Copy link

StphnDamon commented Aug 29, 2019

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

What is the current behavior?
By cloning a schema the SchemaType loose the reference of the default requiredValidator method.

The error is related to this commit a0f6ac1

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

const mongoose = require('mongoose');

class Distance extends mongoose.SchemaType {
  //...
}

mongoose.Schema.Types.Distance = Distance;

(async () => {

  const schema = new mongoose.Schema({
    field: {type: Distance, required: true}
  });

  // Test KO

  const TestKo = mongoose.model('TestKo', schema.clone()); //>> cloned

  const testKo = new TestKo();

  await testKo.validate(); //>> KO, not throwing the ValidationError "Path `field` is required

  console.error('should not exec bottom');

  // Test Ok

  const TestOk = mongoose.model('TestOk', schema); //>> not cloned

  const testOk = new TestOk();

  await testOk.validate();

})();

What is the expected behavior?
I expect the required validator to be executed after being cloned

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

@vkarpov15 vkarpov15 added this to the 5.6.12 milestone Aug 31, 2019
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 31, 2019
vkarpov15 added a commit that referenced this issue Sep 1, 2019
vkarpov15 added a commit that referenced this issue Sep 1, 2019
@StphnDamon
Copy link
Author

Hi @vkarpov15 ,

Thanks for the fix!

As I worked on creating plugins with setters and getters I saw that when a SchemaType is cloned, we also loose its getters and setters.

Can you add these two lines in the clone method:

schematype.getters = this.getters.slice();
schematype.setters = this.setters.slice();

Kind regards

StphnDamon pushed a commit to StphnDamon/mongoose that referenced this issue Sep 3, 2019
vkarpov15 added a commit that referenced this issue Sep 3, 2019
Add missing getters and setters copy when cloning (see issue #8111)
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