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

Validation failed: Cannot read property 'caster' of undefined #10669

Closed
lcrosetto opened this issue Sep 3, 2021 · 0 comments
Closed

Validation failed: Cannot read property 'caster' of undefined #10669

lcrosetto opened this issue Sep 3, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lcrosetto
Copy link

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

Bug

What is the current behavior?

Calling model.validate() on a document field with an array of strings throws an error.

Error: Validation failed: docs.0: Cannot read property 'caster' of undefined
    at ValidationError.inspect (node_modules/mongoose/lib/error/validation.js:48:26)
    at internal/per_context/primordials.js:23:32
    at formatValue (internal/util/inspect.js:774:19)
    at inspect (internal/util/inspect.js:336:10)
    at formatWithOptionsInternal (internal/util/inspect.js:2006:40)
    at formatWithOptions (internal/util/inspect.js:1888:10)
    at console.value (internal/console/constructor.js:320:14)
    at console.warn (internal/console/constructor.js:353:61)
    at validate (bugtest.js:15:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  errors: {
    'docs.0': TypeError: Cannot read property 'caster' of undefined
        at Proxy._cast (node_modules/mongoose/lib/types/array/methods/index.js:245:36)
        at Object.set (node_modules/mongoose/lib/types/array/index.js:72:50)
        at node_modules/mongoose/lib/model.js:4261:42
        at node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
        at new Promise (<anonymous>)
        at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
        at Mongoose._promiseOrCallback (node_modules/mongoose/lib/index.js:1151:10)
        at Function.validate (node_modules/mongoose/lib/model.js:4208:23)
        at validate (bugtest.js:13:28)
        at Object.<anonymous> (bugtest.js:19:1)
  },
  _message: 'Validation failed'
}

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

const { Schema, model } = require('mongoose');

const testSchema = new Schema({
  docs: [String],
});

const Test = model('Test', testSchema);

const test = { docs: [ '6132655f2cdb9d94eaebc09b' ] };

async function validate() {
  try {
    const res = await Test.validate(test);
  } catch (e) {
    console.error(e);
  }
}

validate();

What is the expected behavior?

Validate should succeed. It functions without error in v5.13.8.

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

Node: 14.15.1
Mongoose: 6.0.4
MongoDB: 4.4.8

@vkarpov15 vkarpov15 added this to the 6.0.5 milestone Sep 5, 2021
@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 Sep 5, 2021
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