Alan Scarpa
Alan Scarpa

Reputation: 3560

Firebase validation rule not working

I'm using Firebase & iOS and trying to validate when a user tries to submit a comment.

Here are my rules:

{
  "rules": {
    "comments": {
      ".read": true,
      ".write": "auth.uid != null",
      "$comment_id": {
        ".validate": "newData.isString() && newData.val().length < 100"
      }
    }
  }
}

Here is my database structure: enter image description here

The comment that has been posted successfully was when I deleted the ".validate" portion. I've also tried putting ".validate": "newData.isString() && newData.val().length < 100" outside of the the $comment_id block but this still resulted in failure.

Regardless, I get this error from the simulator when I try to add a comment: enter image description here

Why isn't my ".validate" working? What am I missing?

Upvotes: 1

Views: 394

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

Your data path is /comments/$comment_id/comment. Your validation rule doesn't cater for that last comment in there.

{
  "rules": {
    "comments": {
      ".read": true,
      ".write": "auth.uid != null",
      "$comment_id": {
        "comment": {
          ".validate": "newData.isString() && newData.val().length < 100"
        }
      }
    }
  }
}

These rules will work. But I'd probably update the data structure to remove the comment node, since it doesn't add any value.

Upvotes: 1

Related Questions