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

Subdocument schema not propagating validate error #7187

Closed
ivanseidel opened this issue Oct 28, 2018 · 5 comments
Closed

Subdocument schema not propagating validate error #7187

ivanseidel opened this issue Oct 28, 2018 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ivanseidel
Copy link
Contributor

The following code uses a subschema that produces errors during validate. If that same code is placed in the root document, it fails, but all subschemas does not prevent saving or validating.

The following code should have prevented saving, but succeeded:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const InnerSchema = new Schema(
  {
    name: {
      type: String,
    },
  },
  { strict: "throw", timestamps: true },
);

InnerSchema.pre('validate', async function () {
  console.log('pre-failing')
  throw new Error('I have failed you')
  console.log('post-failing')
})

const TestSchema = new Schema(
  {
    subdoc: [ InnerSchema ]
  },
  { strict: "throw", timestamps: true },
);

const Test = mongoose.model("Test", TestSchema);

mongoose.connect("mongodb://localhost:27017/test");

const data = {
  subdoc: [{name: 'Hey'}]
};

// should throw an error
async function main() {
  const res = await Test.create(data);
  console.log(res);
}
main()
  .then(() => process.exit(0))
  .catch((err) => {
        console.log(err);
        process.exit(1)
});

output:

pre-failing
{ _id: 5bd529a8cc5068b0aaf70cc5,
  subdoc:
   [ { _id: 5bd529a8cc5068b0aaf70cc6,
       name: 'Hey',
       createdAt: 2018-10-28T03:14:48.894Z,
       updatedAt: 2018-10-28T03:14:48.894Z } ],
  createdAt: 2018-10-28T03:14:48.895Z,
  updatedAt: 2018-10-28T03:14:48.895Z,
  __v: 0 }
@ivanseidel
Copy link
Contributor Author

Even with the old next callback it still succeeds:

InnerSchema.pre('validate', function (next) {
  console.log('pre-failing')
  return next(new Error('I have failed you'))
})

output:

pre-failing
{ _id: 5bd52b133fb801b134f441fd,
  subdoc:
   [ { _id: 5bd52b133fb801b134f441fe,
       name: 'Hey',
       createdAt: 2018-10-28T03:20:51.324Z,
       updatedAt: 2018-10-28T03:20:51.324Z } ],
  createdAt: 2018-10-28T03:20:51.324Z,
  updatedAt: 2018-10-28T03:20:51.324Z,
  __v: 0 }

@ivanseidel ivanseidel changed the title Subdocument schema not propagating validate and save errors Subdocument schema not propagating validate error Oct 28, 2018
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 31, 2018
@vkarpov15 vkarpov15 added this to the 5.3.9 milestone Oct 31, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will fix asap 👍

vkarpov15 added a commit that referenced this issue Nov 2, 2018
@ivanseidel
Copy link
Contributor Author

Perfect! thanks

@bopfer
Copy link

bopfer commented Sep 27, 2019

I am seeing this same issue when updating a schema. Possibly a bug?

https://stackoverflow.com/questions/58126872/a-subdocument-validation-throw-is-not-blocking-an-update

@vkarpov15 vkarpov15 reopened this Oct 7, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.3.9, 5.7.4 Oct 7, 2019
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Oct 7, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.7.4, 5.7.5 Oct 7, 2019
@vkarpov15
Copy link
Collaborator

@bopfer we don't run pre('validate') hooks on query validation currently. We've made some improvements on that front and we'll fix this issue, but for now, as a workaround, define a custom validator on the countryId path:

  const addressSchema = new mongoose.Schema({
    address: String,
    city: String,
    regionId: String,
    postalCode: String,
    countryId: { type: String, validate: () => { throw new Error('fail'); } }
  });

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