Meteor-Community-Packages / meteor-simple-schema

Meteor integration package for simpl-schema
https://github.com/Meteor-Community-Packages/meteor-simple-schema
MIT License
920 stars 162 forks source link

Get current value in database #675

Closed Taakn closed 7 years ago

Taakn commented 7 years ago

Hello,

I am a little confused. How do you get the value currently stored in the database and compare it to the this.field in the document that is being submitted?

Thanks

Tenari commented 7 years ago

I as well would like to know how to write an autoValue function which enforces the rule that field x can never be greater than y. it seems like to be able to enforce that, I have to know the current database value, because I can't just use this.value when the modifier is '$inc'

Tenari commented 7 years ago

after testing, it appears that even the built-in Number max validator does not work in the case of $inc operators where the current value is 10, the maxValue is 11 and the modifier is {$inc: {key: 5}} because 5 is less than 11, it passed the validation and sets the new value to 15. which is incorrectly greater than the max of 11

Tenari commented 7 years ago

upon further tinkering I used the this.docId param to just findOne() which seems inelegant, but whatever.

aldeed commented 7 years ago

Yes, comparing to current database values is outside the scope of this package. This package does validation only and is not aware of your database at all. If you use collection2, that would be a place to query current database values, but it currently does not handle $inc either. It's one of the known limitations where invalid data can sneak it. The only 100% safe check would be having MongoDB do the min/max validation since multiple people could $inc in between when you query the value to check and when you do the update after that.