I need to exclude a document field during a manual validation of the document, but then I need to validate it during save.
According to Mongoose documentation, there is a method called $ignore:
Don't run validation on this path or persist changes to this path.
Example:
doc.foo = null;
doc.$ignore('foo');
doc.save(); // changes to foo will not be persisted and validators won't be run
This is almost what I need since I can use $ignore before manually validating the document, but I need some method to tell Mongoose to actually validate and persist the field later on:
doc.$ignore('foo');
await doc.validate();
//Do some stuff
doc.foo = 'new data';
//doc.something() that tells Mongoose now I need to include foo in the validation and persits it
await doc.save();
The hypothetical method "something" exists?
PS: I know I can run validate passing an array of the fields that I need to validate, but the document has many fields, and I just need one to be ignored. It won't be an elegant solution to literally indicate all fields but one.