create firebase database rule on key name -


i need on creating firebase database rule. use case this: user (gamer) have 3 treasure boxes can hold treasure. user can purchase treasure box. tree structure in firebase database came is:

user   |- settings        |- max-t-box : 3   |- boxes        |- 1 : {} // complicated data treasure or null        |- 2 : {} // complicated data treasure or null        |- 3 : {} // complicated data treasure or null 

the rule on 'user/settings/max-t-box' trivial: user read , not allowed write (it can modified server side admin).

the rule under 'user/boxes' supposed be: key of new data should number , it's value should > 0 , <= 'user/setting/max-t-box' value.

according firebase document, can use $variable capture path segment, not provide enough api me check path node name value.

so far solution come write rules path 'user/boxes/1', 'user/boxes/2' , 'user/boxes/3'. looks stupid after user buys many boxes.

you going run lot of issues using sequential numbers keys. (can be) treated array , in firebase arrays evil , should avoided.

arrays cannot modified, searched , if want modify them, have totally re-written.

structuring data using push() or childbyauto() create keys way go:

root      settings        max-t-box 3      boxes        -yuy8jj09j9090f          box_num: 1          box_name: "big box"          box_location: "sewer"        -y8jokokoais9g          box_num: 2          box_name: "small box"          box_location: "tower" 

and rules snap

{   "rules": {     ".read": "auth != null",     ".write": "auth != null",     "boxes": {       "$box_num": {         ".validate": "newdata.child('box_num').val() > 0 &&                        newdata.child('box_num').val() <=                                          root.child('settings').child('max-t-box').val()"        }     }   } } 

i did directly within root node can substitute root.child('users') firebase structure.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -