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

"is not a function" validation error with nested Schema #9438

Closed
Sharlaan opened this issue Sep 22, 2020 · 1 comment
Closed

"is not a function" validation error with nested Schema #9438

Sharlaan opened this issue Sep 22, 2020 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Sharlaan
Copy link

Sharlaan commented Sep 22, 2020

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

Possibly related to issue #8190

What is the current behavior?

const CategorySchema = new Schema(
  {
    name: String,
    // categoryUrl: String, // WORKS
    categoryUrl: { type: String, required: true }, // FAILS
    isSelected: Boolean,
  },
  { _id: false },
);

const SiteSchema = new Schema(
  {
    categoryUrls: [CategorySchema],
  },
  {
    toJSON: {
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
        delete ret.__v;
      },
    },
  },
);

try {
  const newSite = await Site.create(request.body);
  return new HttpResponseCreated({ id: newSite.id });
} catch (error) {
  return new HttpResponseInternalServerError(
    `[SiteController] createSite failed with error ${error.message}`,
  );
}

returns validation error while trying to save a new doc (properly formatted) in DB :
[SiteController] createSite failed with error Site validation failed: categoryUrls.0.categoryUrl: this.isSelected is not a function

What is the expected behavior?
should save new docs properly.

It took me quite some time to figure it werenot the 'isSelected' property making mongoose fail, but the property just above and its required check ...
Not sure if this could be qualified as a "bug", but the error message is really misleading ...
How should i make this nested propertty categoryUrl as required without it making the whole Schema validation fail ?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJS: 14.11.0
Mongoose: 5.10.6
MongoDB Server: 4.4.1

@vkarpov15 vkarpov15 added this to the 5.10.7 milestone Sep 22, 2020
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Sep 22, 2020
@AbdelrahmanHafez
Copy link
Collaborator

9438.js

'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');

const categorySchema = new Schema(
  {
    name: String,
    // categoryUrl: String, // Makes tests pass
    categoryUrl: { type: String, required: true }, // Makes test fail
    isSelected: Boolean
  },
  { _id: false }
);

const siteSchema = new Schema({ categoryUrls: [categorySchema] });

const Site = mongoose.model('Site', siteSchema);
const site = new Site({
  categoryUrls: [
    { name: 'A', categoryUrl: 'B', isSelected: false }
  ]
});
const err = site.validateSync();
console.log(err);

assert.ok(err == null);
console.log('All assertions passed.');

Output

    'categoryUrls.0.categoryUrl': ValidatorError: Path `categoryUrl` is required.
        at validate (\mongoose\lib\schematype.js:1358:13)
        at \mongoose\lib\schematype.js:1347:5
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidateSync (\mongoose\lib\schematype.js:1301:14)
        at \mongoose\lib\document.js:2534:19
        at Array.forEach (<anonymous>)
        at EmbeddedDocument.Document.validateSync (\mongoose\lib\document.js:2518:9)
        at DocumentArrayPath.doValidateSync (\mongoose\lib\schema\documentarray.js:302:37)
        at \mongoose\lib\document.js:2534:19
        at Array.forEach (<anonymous>)
        at model.Document.validateSync (\mongoose\lib\document.js:2518:9)
        at Object.<anonymous> (test.js:24:18)
        at Module._compile (internal/modules/cjs/loader.js:1076:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
        at Module.load (internal/modules/cjs/loader.js:941:32)
        at Function.Module._load (internal/modules/cjs/loader.js:782:14) {
      properties: [Object],
      kind: 'required',
      path: 'categoryUrl',
      value: 'B',
      reason: TypeError: this.isSelected is not a function
          at EmbeddedDocument.requiredValidator (\mongoose\lib\schematype.js:918:41)
          at \mongoose\lib\schematype.js:1335:24
          at Array.forEach (<anonymous>)
          at SchemaString.SchemaType.doValidateSync (\mongoose\lib\schematype.js:1301:14)
          at \mongoose\lib\document.js:2534:19
          at Array.forEach (<anonymous>)
          at EmbeddedDocument.Document.validateSync (\mongoose\lib\document.js:2518:9)
          at DocumentArrayPath.doValidateSync (\mongoose\lib\schema\documentarray.js:302:37)
          at \mongoose\lib\document.js:2534:19
          at Array.forEach (<anonymous>)
          at model.Document.validateSync (\mongoose\lib\document.js:2518:9)
          at Object.<anonymous> (test.js:24:18)
          at Module._compile (internal/modules/cjs/loader.js:1076:30)
          at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
          at Module.load (internal/modules/cjs/loader.js:941:32)
          at Function.Module._load (internal/modules/cjs/loader.js:782:14),
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'Site validation failed'
}

Seems like mongoose uses an internal function isSelected, and is failing due to the fact that we're using a field isSelected in the subschema.

@AbdelrahmanHafez AbdelrahmanHafez added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 23, 2020
vkarpov15 added a commit that referenced this issue Sep 23, 2020
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

3 participants