How can one protect the structure if the input data is a json object composed of multiple json objects with un-predefinable unique keys per parent node (per user for example)?
The same question with an example:
- Every user may have favorite foods.
- Details of the food is not important as that information come from somewhere else.
- A user may have more than -1 and less than 1001 number of favorite foods.
- Only the changed data should be synced to minimise Firebase costs.
So, assuming a user has marked 3 foods with id of E5435, 59z195J, k311 as favorites: The following structure seems to be OK.
{ fav: { $uid { "E5435":1, "59z195J":1, "k311":1 } } }
But! How one would prevent the structure from expanding with arbitrary data? You could add validation rules to check data type and length etc.. but child names are undefined and can be anything like the above example.
I do not want to keep 1 long favorites string as that means sync won't work partially and I must overwrite the data and download every single time.
I cannot add all possible food IDs as it will make the structure very long and that list may grow any time.
How can one add a rule to secure this structure when possible input is something like this:
/fav/UID1234567/ <-- {"AAAA5":1,"G545345":1,"D454WW":1}
(Notes: Food IDs are intentionally written with mixed up characters. If that makes it easier to analyse the question, they can be replaced with names.
AAAA5 --> APPLE, D454WW --> RICE ) forming: /fav/UID1234567/ <-- {"APPLE":1,"RICE":1}
And keep in mind that there cannot be predefined food list to select from: So, nothing like [BANANA, APPLE, RICE, SUSHI,.....] exists. That HUGE list exists but outside of Firebase.
When the client downloads content of /fav/USERID/ fully (only 1 time), it will compare the whole list against some other list to filter those items and show them as favorites.
End of notes)
Additional info: To be clear, how would you prevent the usage of some long text instead of "APPLE" and again a long text instead of it's value '1'. As these data will increase firebase costs and can be considered as attacks. Usual validation rules do not seem to work as in this example we are not pushing values 1 by 1, but multiple of them. So, there is no newData().children().key.isString() and newData().children().key.length(), etc...