Wow, thanks guys! I’ll have a look at what you shared. So basically, if I understand well enough, I would:
- create the mongoDB collections with validators on startup;
- create the Meteor collections normally.
Then, writes would be prevented, but without much details about why it failed, right?
Use case
Simpl-Schema is able, from what I understand, to interpret a modifier object and determine if the result should still yield a valid document when the update completes, without having to fetch each document beforehand. However, Simpl-Schema is really hard to work with when trying to define sophisticated checks referencing multiple fields or with varying schemas.
So, in my projet, I use a more robust validation library. Validating on insert with it is pretty easy, since I have the entire document in my hands before comit. Validating on updates is trickier, since I need to:
- fetch the existing document;
- simulate the impact of applying the modifier on it (with a Minimongo utility function)
- validate the document with validation library.
It’s all right when I update one document through a form, for example. However, I also use after update hooks on other collections to trigger updates. Those can target a large amount of documents at the same time. If I need to fetch each document, simulate modification, validate before commit, it’s a very big overhead for the application server and the DB.
Hence, I would be happy to validate in methods with my fancy library on single document updates coming from the client, ensuring changes are coherent, while using a simplified schema attached to the Mongo collection itself just to ensure data integrity in case I miss something crucial in an after update hook.
Would you have other suggestions?