Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Documentation Please #359

Closed wcrisman closed 7 years ago

wcrisman commented 7 years ago

I have run into a complex data structure: product which has price data indexed by measure ID. So a sample would be project = {_id: xxxx, prices: {mID1: {12.50, ...}, mID2: {10.25, ...}} where mID 1 & 2 are mongo measure ID's. I would like to see some example code in the documentation on how to properly handle this.

At first I tried marking the prices attribute as {type: Object, blackbox: true ...}, but the blackbox property failed to work as expected. I was getting a validation error on the first measure ID. I then tried using custom validation: {type: Object, custom: function() {} ...} with various bits of code in the function. Unfortunately, the custom function doesn't appear to be called (logging in the function never runs). In addition, I appeared to get different responses between manually validating with a new context, validation via check(product, ProductsSchema), and what ever occurs when I call Product.update(..). I even tried turning off validation in the Products.update call Products.update(productId, {$set: {prices: prices, updateAt: new Date()}}, {validate: false, bypassCollection2: false});, but this also did not appear to work.

In the end I simply bypassed collection2 all together and my problems were all solved. I would prefer not to have to do this. This kind of complicated example would help a lot in the documentation, or even better - a reference to an example project where it works. I have only recently started working with Meteor, and while there is a lot of documentation, it is rarely deep enough for me to fully understand what is going on.

My preference would be to use a custom function if standard validation isn't appropriate. While the code was lengthy, it should have worked well - with the exception that the error output would not have referenced the correct attribute generating the error (it would always reference price - since there appears to be no way to specify what part of the price attribute is the problem).

I have attached the full product.js file containing the schema, and the setProductPrice function that I was having trouble with.

Product.js.txt

aldeed commented 7 years ago

blackbox: true should work. In case this is a bug, I'd recommend updating to use the newer SimpleSchema NPM package instead of the Meteor package.