user3452568
user3452568

Reputation: 283

Firebase matches() doesn't work with regex

I want to implement this regex https://regex101.com/r/9m7vMC/1/ (it checks coordinate latitude) in my firebase validation rule. This is my code:

"coordinate": {
    "latitude": {
        ".validate": "newData.isString() && newData.val().matches(/^(\\+|-)?(?:90(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\\.[0-9]{1,6})?))$/)"
    }
}

I've escaped + and ., hence there are double backslashes \\+ and \\.

But this code doesn't work. When I try to write a correct latitude I got an error FIREBASE WARNING: update at / failed: permission_denied

Upvotes: 1

Views: 998

Answers (1)

cartant
cartant

Reputation: 58430

It's the non-capturing groups (?:) that you have in the regular expression that are the problem.

I remember having a similar issue with a regular expression that I was using to validate ISO 8601 times in a Firebase database. Changing the non-capturing groups to capturing groups solved the problem for me.

To replace the non-capturing groups with capturing groups, just remove the uses of ?: from your regular expression:

"coordinate": {
  "latitude": {
    ".validate": "newData.isString() && newData.val().matches(/^(\\+|-)?(90((\\.0{1,6})?)|([0-9]|[1-8][0-9])((\\.[0-9]{1,6})?))$/)"
  }
}

Upvotes: 1

Related Questions