pupuupup
pupuupup

Reputation: 275

firebase rule validate all value in the newData (unknown key validation)

Is there a way to write firebase rule to validate all value in "newData" object?

In my app, my value of any key in "newData" object is quite flexible (my key is unpredictable, user can set their own keys and value) However, I don't really care what value the user give me on each key, but I would like to limit the data length to, for instant, 100 characters.

So I would to validate all value in any key in "newData" whether they are limited to 100 characters in firebase database security rule.

Current stage in my application: Here is how my application works.. basically, my application have a profile page. User can input value in my existing fields. User can also add their own custom fields and save them. All of them will be turned into string. Add it doesn't matter how many custom fields they added. I just need to limit the string's length.

If this is a bad approach to do flexible kind of key value type application, please suggest me other way around.

Here is user newData: { name: "abc", age: "30", xxx: "yyy" } xxx could be anything (xxx is optional, and user can create other customer field such as aaa, bbb, or ccc.)

Here is the JSON security:

{
  "rules": {
  ".read": false,
  ".write": false,
  "user": {
    "$uid": {
      ".read": true,
      ".write": "auth.uid === $uid"
  }
}

Thanks.

Upvotes: 0

Views: 1058

Answers (1)

pupuupup
pupuupup

Reputation: 275

use of wildcard to validate all data with unknown key stated here answered by Frank van Puffelen

https://firebase.google.com/docs/database/security/securing-data#using_variables_to_capture_path_segments

"$unknownKey": { ".validate": "$unknownKey.length < 100}

{
  "rules": {
  ".read": false,
  ".write": false,
  "user": {
    "$uid": {
      ".read": true,
      ".write": "auth.uid === $uid",
      "$unknownKey": { ".validate": "$unknownKey.length < 100"}
    }
  }
}

Upvotes: 1

Related Questions