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

Cloning schema with 'map' type attribute breaks validation when 'of' set to schema definition #8357

Closed
nhitchins opened this issue Nov 19, 2019 · 1 comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@nhitchins
Copy link

nhitchins commented Nov 19, 2019

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

What is the current behavior?
Cloning a schema Schema.prototype.clone() containing a 'map' type attribute with 'of' set to a new embedded schema breaks validation.
Note: a workaround is to create a new schema and use Schema.prototype.add() to first add the schema to be cloned and then add additional attributes - what is the difference between using baseSchema.clone() versus (new mongoose.Schema()).add(baseSchema)?

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

var mongoose = require('mongoose');

var mapSchema = new mongoose.Schema({
  text: String
});

var baseSchema = new mongoose.Schema({
  mapTest: {
    type: Map,
    of: mapSchema
  }
});

// Broken:
var extendedSchema = baseSchema.clone();
extendedSchema.add({ extendedValue: Number });

// Workaround:
// var extendedSchema = new mongoose.Schema();
// extendedSchema.add(baseSchema).add({ extendedValue: Number });

var BaseModel = mongoose.model('Base', baseSchema, "test");
var ExtendedModel = mongoose.model('Extended', extendedSchema, "test");

var data = {
  mapTest: {
    randomKey: {
      text: 'random-string'
    }
  },
  extendedValue: 2
};

// Save fails validation
var localModel = new ExtendedModel(data);
localModel.save(function (err) {
  if (err) return console.log(err);
});

What is the expected behavior?
Should correctly clone (or reference?) the embedded map type schema(s)

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

@nhitchins
Copy link
Author

nhitchins commented Nov 20, 2019

Update: (new mongoose.Schema()).add(baseSchema) is obviously not a viable workaround because it does not add path discriminators defined on the baseSchema.

Missed this part in Schema.prototype.add() docs:

Adds key path / schema type pairs to this schema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants