{"action":"opened","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/7187","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/7187/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/7187/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/7187/events","html_url":"https://github.com/Automattic/mongoose/issues/7187","id":374712532,"node_id":"MDU6SXNzdWUzNzQ3MTI1MzI=","number":7187,"title":"Subdocument schema not propagating validate and save errors","user":{"login":"ivanseidel","id":3102127,"node_id":"MDQ6VXNlcjMxMDIxMjc=","avatar_url":"https://avatars2.githubusercontent.com/u/3102127?v=4","gravatar_id":"","url":"https://api.github.com/users/ivanseidel","html_url":"https://github.com/ivanseidel","followers_url":"https://api.github.com/users/ivanseidel/followers","following_url":"https://api.github.com/users/ivanseidel/following{/other_user}","gists_url":"https://api.github.com/users/ivanseidel/gists{/gist_id}","starred_url":"https://api.github.com/users/ivanseidel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivanseidel/subscriptions","organizations_url":"https://api.github.com/users/ivanseidel/orgs","repos_url":"https://api.github.com/users/ivanseidel/repos","events_url":"https://api.github.com/users/ivanseidel/events{/privacy}","received_events_url":"https://api.github.com/users/ivanseidel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-28T03:19:27Z","updated_at":"2018-10-28T03:19:27Z","closed_at":null,"author_association":"NONE","body":"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.\r\n\r\nThe following code should have prevented saving, but succeeded:\r\n\r\n```javascript\r\nconst mongoose = require(\"mongoose\");\r\nconst Schema = mongoose.Schema;\r\n\r\nconst InnerSchema = new Schema(\r\n  {\r\n    name: {\r\n      type: String,\r\n    },\r\n  },\r\n  { strict: \"throw\", timestamps: true },\r\n);\r\n\r\nInnerSchema.pre('validate', async function () {\r\n  console.log('pre-failing')\r\n  throw new Error('I have failed you')\r\n  console.log('post-failing')\r\n})\r\n\r\nconst TestSchema = new Schema(\r\n  {\r\n    subdoc: [ InnerSchema ]\r\n  },\r\n  { strict: \"throw\", timestamps: true },\r\n);\r\n\r\nconst Test = mongoose.model(\"Test\", TestSchema);\r\n\r\nmongoose.connect(\"mongodb://localhost:27017/test\");\r\n\r\nconst data = {\r\n  subdoc: [{name: 'Hey'}]\r\n};\r\n\r\n// should throw an error\r\nasync function main() {\r\n  const res = await Test.create(data);\r\n  console.log(res);\r\n}\r\nmain()\r\n  .then(() => process.exit(0))\r\n  .catch((err) => {\r\n        console.log(err);\r\n        process.exit(1)\r\n});\r\n```\r\n\r\noutput:\r\n```\r\npre-failing\r\n{ _id: 5bd529a8cc5068b0aaf70cc5,\r\n  subdoc:\r\n   [ { _id: 5bd529a8cc5068b0aaf70cc6,\r\n       name: 'Hey',\r\n       createdAt: 2018-10-28T03:14:48.894Z,\r\n       updatedAt: 2018-10-28T03:14:48.894Z } ],\r\n  createdAt: 2018-10-28T03:14:48.895Z,\r\n  updatedAt: 2018-10-28T03:14:48.895Z,\r\n  __v: 0 }\r\n```"}}
{"actor":{"display_login":"ivanseidel"}}