Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase data validation

Tags:

firebase

I'm considering to use Firebase for a project but can't seem to find any informations on server-side data validation.

Lets say i'm making a game and a player deals damage to another player i would like to validate the following:

  • That the players are actually close to eachother
  • That the damage points corresponds to the attack given
  • That the data has not been tampered from the client to the server
  • ETC.

Is it possible to validate this kind of stuff /Adding serverside logic directly with Firebase or do i have to make an intermediate-server, basically smashing the whole point in using Firebase in the first place?

Thanks in advance Jonas

like image 533
Jonas m Avatar asked Mar 11 '15 12:03

Jonas m


1 Answers

Validating data is definitely possible with Firebase. It is part of its "security" rules, for which the documentation can be found here and here.

A simple example from that last documentation link:

a sample .validate rule definition which only allows dates in the format YYYY-MM-DD between the years 1900-2099, which is checked using a regular expression.

".validate": "newData.isString() &&
              newData.val().matches(/^(19|20)[0-9][0-9][-\\/. ](0[1-9]|1[012])[-\\/. ](0[1-9]|[12][0-9]|3[01])$/)"

You can build pretty complicated validation rules. In case you need those, you might want to have a look at Firebase's blaze compiler. It translates a higher-level language into Firebase's relatively low-level rules. The author of the blaze compiler originally wrote it for your second and third use-case and wrote an article about it here.

I hope these are enough to get you started. If you get stuck, just post a question with the rules you tried.

like image 144
Frank van Puffelen Avatar answered Nov 15 '22 06:11

Frank van Puffelen