jimsis
jimsis

Reputation: 311

Firebase Realtime Database rules

I am using the following rule to check that only 'admins' can write to the traps section.

"traps": {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
        },

This works fine. However if I change the rule to the following,

"traps": {
    "$trap" : {
      ".read": "auth != null",
      ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
       }
    },

then I get a permission error. W/SyncTree: Listen at /traps failed: DatabaseError: Permission denied.

The reason I'm making this structure change is I'm trying to use the validate function but as I can't get past this first issue, I can't get as far as the validate.

"traps": {
    "$trap" : {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
      "trapNumber": {
         ".validate": "newData.isString()"
       }
    }
},

I'm sure this is a rookie comprehension error but I've made little progress with this after trying many permutations. You assistance would be greatly appreciated.

Update 20160629

Frank, the admin section looks as follows

admins
   somegeneratedkey: true

Upvotes: 0

Views: 2282

Answers (1)

jimsis
jimsis

Reputation: 311

This worked for me

"rules": {
    "traps": {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
    "$trap" : {
            ".validate": "newData.hasChildren(['trapNumber']) && newData.child('trapNumber').isString()"
        }
    },
    etc.

I had to leave the read and write at the traps level as that was the level my database reference was using.

A little piece of additional information, isNumber() is not testing a String for a numeric value, it is testing that a numeric field (int) is passed. Not the behavior I was expecting.

Upvotes: 1

Related Questions