question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidationError when using array in Schema

See original GitHub issue

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

What is the current behavior? After defining my schema similar to the following one:

const UserSchema = new mongoose.Schema({
  roles: {
    type: [String],
    index: true,
  },
});

mongoose.model('User', UserSchema)

If I try to instantiate a new User like this:

const user = new User({ roles: ['super-admin'] });

It raises the following exception ValidationError: User validation failed: roles: Cast to Array failed for value "[ 'super-admin' ]" at path "roles"

Instantiating the user with an empty roles array does not raise any exception. This is happening on mongoose@5.6.9, downgrading to 5.6.8 fixes it.

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

The schema shown above should be enough to reproduce the issue.

What is the expected behavior? No ValidationError exception raised when instantiating a model with an array in the schema.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. node 10.16.0 mongoose 5.6.9 mongo 4.0.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
BrunoGodefroycommented, Aug 10, 2019

@vkarpov15 I have put together a small reproduction repository: https://github.com/BrunoGodefroy/repro-mongoose-issue-8053

I tried to simplify it at a maximum to showcase the error, I could not remove jest while keeping the bug so it might be caused by babel transpiling the code for jest.

Hope that helps

3reactions
vkarpov15commented, Aug 14, 2019

This appears to be yet another bug with Jest. Not to harp about this too much, but Jest is horribly broken and you should really use Mocha or some other framework that doesn’t invent its own JavaScript runtime that isn’t quite Node.js or a browser. Because Jest makes up its own runtime by monkeypatching everything, code that works fine in Node.js may not work in Jest and vice versa, so Jest is not a reliable way to test Node.js projects.

It looks like this issue comes down to Jest stubbing out require(), so if you require('./) in a directory where the parent dir happens to have a file with the same name as the current directory, Jest’s require() gives you the parent dir’s file. For example, suppose you have the below file structure:

- test.js
- dir.js
- dir
  - index.js
  - fn.js

test.js

const fn = require('./dir/fn');

it('test', function() {
  console.log(fn());
});

dir.js

module.exports = 'top-level';

dir/index.js

module.exports = 'nested';

dir/fn.js

module.exports = function() {
  return require('./');
};

Running test.js will output:

top-level

As opposed to nested like in Node.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

json schema validation error with LocalDateTime-array found ...
I am validating date of type java.time.LocalDateTime using json-schema. But I am getting below error: message= array found, string expected.
Read more >
17.7.0 API Reference - joi.dev
First, a schema is constructed using the provided types and constraints: ... This method is designed to override the joi validation error and...
Read more >
parse jason validation error - Power Platform Community
Hello. when I run my flow it returns me a parse jason validation error, the schema valdation has failed. I don't know how...
Read more >
Mongoose v6.8.1: Error
DivergentArrayError : You attempted to save() an array that was modified after you loaded it with a $elemMatch or similar projection; MissingSchemaError :...
Read more >
https://hackage.haskell.org/package/openapi3-3.0.0...
This can be used with QuickCheck to ensure those instances are coherent: ... Definitions Schema -> Schema -> Value -> [ValidationError] validateJSON ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found