Keno
Keno

Reputation: 3945

Usage of multiple Validation rules in Firebase Database

I am unclear on a couple of aspects of Firebase Database validation rules, and have already read the documentation a couple of times my specific case/questions are as follow:

My data scheme is this:

{
  "aNumber" : 0.794180524700695,
  "clicks" : 11
}

My current validation rules are this:

{
  "rules": {
    ".validate": "newData.child('aNumber').isNumber()",
     ".read": true,
    ".write": true      
  }
}

This rule currently allows the write for "aNumber" if it is a number, but I would like to also validate for the field "clicks" to be a number.

I tried using the and operator (&&):

{
  "rules": {
   ".validate": "newData.child('aNumber').isNumber() && 
                 newData.child('clicks').isNumber()",
     ".read": true,
    ".write": true      
  }
}

But it fails when I only update one field:

{
 "aNumber": 2
}

How would I validate for this ? , what is a good way of validating multiple independent fields in general ?

Thanks

Upvotes: 3

Views: 1456

Answers (1)

Keno
Keno

Reputation: 3945

So after some experimenting I found an answer to my own question:

My new rules look like this:

{
 "rules": {
  ".read": true,
  ".write": true,
  "aNumber": {
      ".validate": "newData.isNumber()"
     },
   "clicks": {
      ".validate": "newData.isNumber()"
     }                
 }
}

So instead of validating for a specific child node globally, the validation happens at the specific node level in the rules, this might be obvious to some, but it took me a bit to realize.

Upvotes: 5

Related Questions