Why does Mongoose throws ValidationError when running test in CI although the type declaration is OK?

I’m getting this error: ValidationError: <MySchema> validation failed: name: Cast to String failed for value "some-string" (type string) at path "name" because of "TypeError" while running my unit tests in GitHub Actions. Obviously, that’s weird since it gets a string for casting so I really can’t see why it doesn’t work…
Schema is along:

var MySchema = {
  ...
  name: { type: String, get: encryption.decrypt, set: encryption.encrypt },
  ...
}

This does not reproduce locally nor there’s this problem in our testing/production environments.

I’ve tried many things, like adding the { typeKey: '$type' } but it’s not what I really want, since my key should be automatically used for type.
I’ve worked around this by using:

var validateBeforeSave = process.env.NODEJS_ENV !== "test-ci";
MySchema.set("validateBeforeSave", validateBeforeSave);

But then I miss this functionality in our tests.

Mongoose version: 6.10.1
Mongo version: 5.0.6

Thanks for any help!

Hi Moshe,

As your problem is with mongoose I believe that the better place to you ask this question is directly on his issues. Here you will find a lot of people using mongoDB but probably using mongo driver.

So I recommend you to take a look at mongoose GitHub issues.