{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/1860","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/1860/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/1860/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/1860/events","html_url":"https://github.com/Automattic/mongoose/issues/1860","id":25053923,"number":1860,"title":"Required fields on non-required nested documents","user":{"login":"keithminder","id":1496281,"avatar_url":"https://avatars.githubusercontent.com/u/1496281?v=3","gravatar_id":"","url":"https://api.github.com/users/keithminder","html_url":"https://github.com/keithminder","followers_url":"https://api.github.com/users/keithminder/followers","following_url":"https://api.github.com/users/keithminder/following{/other_user}","gists_url":"https://api.github.com/users/keithminder/gists{/gist_id}","starred_url":"https://api.github.com/users/keithminder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keithminder/subscriptions","organizations_url":"https://api.github.com/users/keithminder/orgs","repos_url":"https://api.github.com/users/keithminder/repos","events_url":"https://api.github.com/users/keithminder/events{/privacy}","received_events_url":"https://api.github.com/users/keithminder/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2014-01-04T14:01:42Z","updated_at":"2016-01-14T23:09:00Z","closed_at":"2015-04-29T21:17:20Z","body":"/*\r\nWhen a schema contains a field that is a nested document, then the nested document is by default non-required.  Adding a required field to a nested document makes the nested document required field validation fail in cases in which property's value is null.   This should not be the case and the document should pass validation in this case.\r\n \r\nConsider the following:\r\n*/\r\n var applicationSchema = mongoose.Schema( {\r\n firstName: { type: String, required: true }\r\n , lastName: { type: String, required: true }\r\n , father: {\r\n  firstName: { type: String, required: true }, middleName: String, lastName: { type: String, required: true }, address1: { type: String, required: true }, address2: String, city: { type: String, required: true }, state: { type: String, required: true }, zip: { type: String, required: true }\r\n}\r\n);\r\nvar Application=  mongoose.model('application',applicationSchema);\r\n/*\r\n     the following document should pass validation but does not:\r\n     {firstName:'shaheed', lastName:'isthatdude'}\r\n*/\r\nvar application = {firstName:'shaheed', lastName:'isthatdude'};  //pojo\r\nvar applicationToSave = new Application(application);\r\napplicationToSave.save(function(err,saved){if(err){console.log('error: '+ err);}});\r\n \r\n/*\r\n    Using custom validation works as expected:\r\n*/\r\nfunction requiredOnOptional(val)\r\n{\r\n\r\n  return (val instanceof String || typeof val == 'string') && val.length;\r\n\r\n}\r\n var applicationSchema = mongoose.Schema( {\r\n firstName: { type: String, required: true }\r\n , lastName: { type: String, required: true }\r\n , father: {\r\n  firstName: { type: String, validate:requiredOnOptional }, middleName: String, lastName: { type: String, , validate:requiredOnOptional }, address1: { type: String, , validate:requiredOnOptional }, address2: String, city: { type: String, , validate:requiredOnOptional }, state: { type: String, , validate:requiredOnOptional }, zip: { type: String, , validate:requiredOnOptional }\r\n}\r\n);\r\nvar Application=  mongoose.model('application',applicationSchema);\r\nvar application = {firstName:'shaheed', lastName:'isthatdude'};  //pojo\r\nvar applicationToSave = new Application(application);\r\napplicationToSave.save(function(err,saved){if(err){console.log('error: '+ err);}});\r\n\r\n//the above example bypasses the validation of the null nested field's properties.  This is inconsistent and I think that it should work as it does with custom validation."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/171811277","html_url":"https://github.com/Automattic/mongoose/issues/1860#issuecomment-171811277","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/1860","id":171811277,"user":{"login":"ruh-collective","id":15674968,"avatar_url":"https://avatars.githubusercontent.com/u/15674968?v=3","gravatar_id":"","url":"https://api.github.com/users/ruh-collective","html_url":"https://github.com/ruh-collective","followers_url":"https://api.github.com/users/ruh-collective/followers","following_url":"https://api.github.com/users/ruh-collective/following{/other_user}","gists_url":"https://api.github.com/users/ruh-collective/gists{/gist_id}","starred_url":"https://api.github.com/users/ruh-collective/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ruh-collective/subscriptions","organizations_url":"https://api.github.com/users/ruh-collective/orgs","repos_url":"https://api.github.com/users/ruh-collective/repos","events_url":"https://api.github.com/users/ruh-collective/events{/privacy}","received_events_url":"https://api.github.com/users/ruh-collective/received_events","type":"User","site_admin":false},"created_at":"2016-01-14T23:09:00Z","updated_at":"2016-01-14T23:09:00Z","body":"I'm having an issue with this still.  I tried the above solution but even when returning false (hardcoded), the resource get's created.\r\n\r\n     var cannotBeBlank = function(x){\r\n         if(!this.name || !this.address || !this.state || !this.city || !this.zip){\r\n           console.log('Well we caught what we\\'re looking for...')\r\n           return false\r\n         }\r\n      return true\r\n     }\r\n    addresses: [{\r\n      name: { type: String, required: cannotBeBlank },\r\n      ...\r\n    }],"}}