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: Fails casting to String arrays since the last (5.6.9) release #8081

Closed
birdofpreyru opened this issue Aug 19, 2019 · 3 comments
Closed

Comments

@birdofpreyru
Copy link
Contributor

The latest Mongoose release (5.6.9) breaks my code, which worked fine with previous versions. I did not other changes beside the Mongoose update from 5.6.8 to 5.6.9.

In a Schema I have a string array field, defined this way:

roles: {
    type: [String],
    enum: VALID_ROLE_VALUES,
    get: function rolesGetter(roles) {
      // some logic that either returns "roles", or [...roles, "beta_user"]
    },
  },

This worked fine before the update, after the update I am getting this error upon the Schema creation:

`enum` can only be set on an array of strings, not undefined

      45 | });
      46 | 
    > 47 | const schema = new Schema({
         |                ^
      48 |   createdAt: {
      49 |     type: Date,
      50 |     default: () => Date.now(),

      at SchemaArray.Object.<anonymous>.SchemaArray.enum (node_modules/mongoose/lib/schema/array.js:206:13)
      at SchemaArray.SchemaType (node_modules/mongoose/lib/schematype.js:85:18)
      at new SchemaArray (node_modules/mongoose/lib/schema/array.js:87:14)
      at Schema.Object.<anonymous>.Schema.interpretAsType (node_modules/mongoose/lib/schema.js:865:12)
      at Schema.Object.<anonymous>.Schema.path (node_modules/mongoose/lib/schema.js:606:27)
      at Schema.add (node_modules/mongoose/lib/schema.js:444:12)
      at new Schema (node_modules/mongoose/lib/schema.js:121:10)
      at Object.<anonymous> (src/models/User.js:47:16)
      at Object.<anonymous> (src/auth/methods/jwt.js:15:1)

If I try to comment out enum and get instructions, I get another error, when attempting to create corresponding documents:

ValidationError: User validation failed: roles: Cast to Array failed for value "[ 'shared_user' ]" at path "roles"

      34 |   await AnalysisOutput.deleteMany();
      35 |   await User.deleteMany();
    > 36 |   USERS.push(await User.create({
         |                         ^
      37 |     _id: getMockMongoObjectId(),
      38 |     ethAddress: makeEthAddress(),
      39 |     roles: [ROLES.SHARED_USER],

      at new ValidationError (node_modules/mongoose/lib/error/validation.js:30:11)
      at model.Object.<anonymous>.Document.invalidate (node_modules/mongoose/lib/document.js:2317:32)
      at model.$set (node_modules/mongoose/lib/document.js:1112:10)
      at model._handleIndex (node_modules/mongoose/lib/document.js:877:14)
      at model.$set (node_modules/mongoose/lib/document.js:821:22)
      at model.Document (node_modules/mongoose/lib/document.js:124:12)
      at model.Model (node_modules/mongoose/lib/model.js:92:12)
      at new model (node_modules/mongoose/lib/model.js:4395:15)
      at toExecute.push.callback (node_modules/mongoose/lib/model.js:3035:22)
      at node_modules/async/internal/parallel.js:31:39
      at eachOfArrayLike (node_modules/async/eachOf.js:65:9)
      at Object.<anonymous>.exports.default (node_modules/async/eachOf.js:9:5)
      at _parallel (node_modules/async/internal/parallel.js:30:5)
      at parallelLimit (node_modules/async/parallel.js:88:26)
      at utils.promiseOrCallback.cb (node_modules/mongoose/lib/model.js:3045:5)
      at Promise (node_modules/mongoose/lib/utils.js:271:5)
      at Object.promiseOrCallback (node_modules/mongoose/lib/utils.js:270:10)
      at Function.create (node_modules/mongoose/lib/model.js:3006:16)
      at Object.create (__tests__/functional/routes/analyses/issues.js:36:25)
@vkarpov15
Copy link
Collaborator

This is a bug in Jest, see #8053, this issue does not happen in Node.js. The issue is fixed in v5.6.10.

But I strongly recommend you read this comment about why testing with Jest is an anti-pattern.

@birdofpreyru
Copy link
Contributor Author

birdofpreyru commented Aug 21, 2019

Thanks @vkarpov15!

Regarding Jest and this issue, actually looking at your fix, and Node docs, I believe that Jest's require in this case behaves exactly according to the Node's module resolution algorithm, as described here: https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_all_together
If you do require('./') from a module /some/path/module.js, it will try load /some/path.js first and only if that fails it will eventually try /some/path/index.js.

So, I believe it was a legit bug in Mongoose.

@vkarpov15
Copy link
Collaborator

You're looking at docs for node 0.10.x. I haven't tried this in every node version, but that is not how node 8.x works. If you don't believe me, try it yourself, maybe I'm missing something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants