Skip Validation of Sub-Documents
See original GitHub issueI have a situation where I needed to modify some attributes of the schemas making them mandatory, my idea was that the validations were made only when changes have direct objects that, because I have separate pages for maintaining address and phone for example.
Initially my modeling was thus:
var AddressSchema = new Schema({
name: {type: String, required: true},
street1: {type: String},
street2: {type: String},
});
var PhoneSchema = new Schema({
number: {type: Number, required: true}
});
var CustomerSchema = new Schema({
name: {type: String, required: true},
addresses: [AddressSchema],
phones: [PhoneSchema]
});
var customer = new Customer();
customer.name = 'Tiago';
customer.addresses.push({name: 'address 1'});
customer.phones.push({number: 123456});
customer.save(function(err){
if(err) throw err;
});
Then I had to modify some attributes required putting in Address but have saved many addresses that do not have those data filled and going forward that would be required.
var AddressSchema = new Schema({
name: {type: String, required: true},
street1: {type: String, required: true},
street2: {type: String, required: true}
});
Now I would like to remove or change the phone this client and I can not because I have the address validation, have some way to do this using the save method?
Customer.find({name: "Tiago"}, function(err, customer){
if(err) throw err;
customer[0].phones.remove();
customer[0].save(function(err){
if(err) throw err; //Validation Valid
});
});
Customer.find({name: "Tiago"}, function(err, customer){
if(err) throw err;
customer[0].name = 'Tiago Changed';
customer[0].phones[0].number = 654321;
customer[0].phones.push({number: 98765432});
customer[0].save(function(err){
if(err) throw err; //Validation Valid
});
});
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
javascript - Insert Subdocument without Validation on Document
I am using Mongoose with Javascript (NodeJS) to read/write to MongoDB. I have a Document (Parent) that has a bunch of Subdocuments (Children)...
Read more >Mongoose v5.13.15: SubDocuments
Subdocuments have save and validate middleware just like top-level documents. Calling save() on the parent document triggers the save() middleware for all its ......
Read more >Schema validation for multiple types of subdocuments
I'd like to use validation for an object that has several variants (polymorphic) it would seem that oneOf is the place to implement...
Read more >Validation Rules — Cerberus is a lightweight and extensible ...
This can be used in conjunction with the schema rule when validating a mapping in order to set the allow_unknown property of the...
Read more >Splitting HIPAA Subdocuments - BizTalk Server | Microsoft Learn
If a transaction set fails EDI or extended validation during subdocument splitting, the individual failing transaction set is suspended.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a necessary feature. Sub documents should skip validation if they have no modified paths. This would drastically improve performance when saving large documents and would prevent unnecessary errors being thrown on a document which was not modified.
So I absolutely needed this working on my project. I have lots of nested schemas, and future requirements should not affect existing sub documents. If I need to perform a mass update due to new fields or new business logic, I will build that into my update/deployment process. Otherwise, legacy content can get updated whenever it gets updated and should never be treated as invalid until then. Here’s my hack… very very hack - it short circuits mongoose’s internal logic during validation. This should work for any schema (mongo v.2.6.1) and can be easily be wrapped in a plugin:
NOTE: If you are using any plugins to do things like “modified_on” fields, make sure they check that
this.modifiedPaths().length === 0
before setting any values.