Relm
Relm

Reputation: 8291

Firebase : How to write a validating rule that targets more than one node.

I want to validate First Name and Last Name not be empty, and its practically the same rule for both. How do I write one rule to validate both?

Consider the following.

-- profile
     -- birthday:        "1996-February-2"
     -- email:           "[email protected]"
     -- firstname:       "human"   
     -- gender:          "1"
     -- lastname:        "man"

And the rule :

"profile":{
          "firstname, lastname":{
                   ".validate": "newData.isString()
                                 && newData.val().length < 30
                                 && newData.val().length > 2
                                "
           },

I assume that "firstname, lastname : {}" is not a valid syntax since neither firstname or lastname are being selected.

Is there a way to do this?

Upvotes: 1

Views: 121

Answers (1)

mumayank
mumayank

Reputation: 1700

Firebase encourages flattening of data. And hence it is okay to have many parent nodes rather than many child nodes. This also directly means it is okay to have duplicate rules.

One major advantage of having flat single node rules is that it makes your rules very readable. Keep similar rules following nodes together and separate different rule following nodes with spaces to make them further readable and clubbed.

This way, whenever you need to change similar rules, you can easily do so. It is not worthy to spend more time on this.

Upvotes: 1

Related Questions