question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Skip Validation of Sub-Documents

See original GitHub issue

I 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:closed
  • Created 10 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DesignByOnyxcommented, Jun 4, 2014

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.

0reactions
DesignByOnyxcommented, Jun 4, 2014

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.

    var tmpValues;
    schema.pre('validate', function(next) {
        tmpValues = { states:{} };
        if( this.modifiedPaths().length === 0 ) {
            tmpValues.states.require = this.$__.activePaths.states.require;
            tmpValues.states.init = this.$__.activePaths.states.init;
            tmpValues.states.modify = this.$__.activePaths.states.modify;
            tmpValues.states.default = this.$__.activePaths.states.default;

            this.$__.activePaths.states.require = {};
            this.$__.activePaths.states.init = {};
            this.$__.activePaths.states.modify = {};
            this.$__.activePaths.states.default = {};
        }
        next();
    });

    schema.post('validate', function(doc) {
        for( var state in tmpValues.states ) {
            this.$__.activePaths.states[ state ] = tmpValues.states[ state ];
        }
    });
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found