{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/7135","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/7135/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/7135/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/7135/events","html_url":"https://github.com/Automattic/mongoose/issues/7135","id":370193061,"node_id":"MDU6SXNzdWUzNzAxOTMwNjE=","number":7135,"title":"Array validation doesn't occur with $set but does with $push during an updateOne operation","user":{"login":"asafch","id":10197506,"node_id":"MDQ6VXNlcjEwMTk3NTA2","avatar_url":"https://avatars0.githubusercontent.com/u/10197506?v=4","gravatar_id":"","url":"https://api.github.com/users/asafch","html_url":"https://github.com/asafch","followers_url":"https://api.github.com/users/asafch/followers","following_url":"https://api.github.com/users/asafch/following{/other_user}","gists_url":"https://api.github.com/users/asafch/gists{/gist_id}","starred_url":"https://api.github.com/users/asafch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asafch/subscriptions","organizations_url":"https://api.github.com/users/asafch/orgs","repos_url":"https://api.github.com/users/asafch/repos","events_url":"https://api.github.com/users/asafch/events{/privacy}","received_events_url":"https://api.github.com/users/asafch/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/262","html_url":"https://github.com/Automattic/mongoose/milestone/262","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/262/labels","id":3741090,"node_id":"MDk6TWlsZXN0b25lMzc0MTA5MA==","number":262,"title":"5.3.6","description":"","creator":{"login":"vkarpov15","id":1620265,"node_id":"MDQ6VXNlcjE2MjAyNjU=","avatar_url":"https://avatars2.githubusercontent.com/u/1620265?v=4","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":0,"state":"open","created_at":"2018-10-15T20:42:21Z","updated_at":"2018-10-15T20:42:29Z","due_on":"2018-10-23T07:00:00Z","closed_at":null},"comments":2,"created_at":"2018-10-15T14:34:29Z","updated_at":"2018-10-16T06:54:31Z","closed_at":null,"author_association":"NONE","body":"<!-- *Before creating an issue please make sure you are using the latest version of mongoose -->\r\n\r\n**Do you want to request a *feature* or report a *bug*?**\r\nReport a bug.\r\n\r\n**What is the current behavior?**\r\nWhen attempting to update a document's property, which is an array, during an `updateOne` operation and turning on validation with the `{runValidators: true}` option, the array isn't validated when it's updated with the `$set` operator, but it does get validated when using the `$push` operator.\r\nThe reason for using a `$set` operator is to add new elements in the _head_ of the array (index 0) rather than its tail, via application code. However, it is more important for me to have validation than to insist on a specific array structure.\r\n\r\n**If the current behavior is a bug, please provide the steps to reproduce.**\r\n<!-- If you can, provide a standalone script / gist to reproduce your issue -->\r\n```javascript\r\nconst bluebird = require(\"bluebird\");\r\nconst mongoose = require(\"mongoose\");\r\nmongoose.set(\"debug\", true);\r\nmongoose.Promise = bluebird.Promise;\r\n\r\n(async () => {\r\n\ttry {\r\n\t\tconst db = mongoose.createConnection(\"mongodb://localhost/test_db\");\r\n\t\tconst ElementSchema = mongoose.Schema({\r\n\t\t\ta: { type: String, required: true },\r\n\t\t\tb: {\r\n\t\t\t\tc: {type: String, required: true}\r\n\t\t\t}\r\n\t\t}, { _id: false });\r\n\t\tconst ArraySchema = mongoose.Schema({\r\n\t\t\tanArray: [ElementSchema]\r\n\t\t});\r\n\r\n\r\n\t\tconst TestModel = db.model(\"ArraySchema\", ArraySchema, \"test_collection\");\r\n\t\tconst document = TestModel({})\r\n\r\n\t\tawait document.save();\r\n\r\n\t\tconst fetchedDocument1 = (await TestModel.find({}))[0];\r\n\r\n\t\tconsole.log(fetchedDocument1);\r\n\r\n\t\tconst setResult1 = await TestModel.updateOne({_id: fetchedDocument1._id}, {\r\n\t\t\t$set: {\r\n\t\t\t\tanArray: [{}]\r\n\t\t\t}\r\n\t\t},\r\n\t\t{ runValidators: true });\r\n\r\n\t\t// I expect the validation to prohibit saving the document due to an empty object inside the array\r\n\r\n\t\tconsole.log(setResult1);\r\n\r\n\t\tconst setResult2 = await TestModel.updateOne({_id: fetchedDocument1._id}, {\r\n\t\t\t$push: {\r\n\t\t\t\tanArray: {\r\n\t\t\t\t\ta: \"hello\",\r\n\t\t\t\t\tb: {\r\n\t\t\t\t\t\tc: \"world\"\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t},\r\n\t\t{ runValidators: true });\r\n\r\n\t\t// I expect the validation to pass because the element conforms to the schema\r\n\r\n\t\tconsole.log(setResult2);\r\n\t} catch (err) {\r\n\t\tconsole.log(err);\r\n\t}\r\n})();\r\n\r\n```\r\n**What is the expected behavior?**\r\n- The update operation result stored in `setResult1` should report an error, not a success, due to adding an empty element to the array.\r\n\r\n**Please mention your node.js, mongoose and MongoDB version.**\r\nNode - `10.0.0`\r\nMongoDB - `3.4.14`\r\nmongoose - `5.0.5`"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/430122197","html_url":"https://github.com/Automattic/mongoose/issues/7135#issuecomment-430122197","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/7135","id":430122197,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDEyMjE5Nw==","user":{"login":"asafch","id":10197506,"node_id":"MDQ6VXNlcjEwMTk3NTA2","avatar_url":"https://avatars0.githubusercontent.com/u/10197506?v=4","gravatar_id":"","url":"https://api.github.com/users/asafch","html_url":"https://github.com/asafch","followers_url":"https://api.github.com/users/asafch/followers","following_url":"https://api.github.com/users/asafch/following{/other_user}","gists_url":"https://api.github.com/users/asafch/gists{/gist_id}","starred_url":"https://api.github.com/users/asafch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asafch/subscriptions","organizations_url":"https://api.github.com/users/asafch/orgs","repos_url":"https://api.github.com/users/asafch/repos","events_url":"https://api.github.com/users/asafch/events{/privacy}","received_events_url":"https://api.github.com/users/asafch/received_events","type":"User","site_admin":false},"created_at":"2018-10-16T06:54:31Z","updated_at":"2018-10-16T06:54:31Z","author_association":"NONE","body":"@vkarpov15 I haven't tested this bug in `update` and `updateMany` operations to see if it exists there as well - I think it would be prudent to do so."}}
{"actor":{"display_login":"asafch"}}