deltastar
deltastar

Reputation: 45

Id match in firestore database rules

I am working on my security rules, but when I try to match the document id with a regex, it doesn't work. I tried to use the matches function, but it doesn't seem to accept the method.

Even when I tried using the Firebase pattern YYYY-MM-DD (/^(19|20)[0-9][0-9][-\\/. ](0[1-9]|1[012])[-\\/. ](0[1-9]|[12][0-9]|3[01])$/) from here, but it didn't work (I tried with 1950-01-01).

enter image description here

I am trying to check roomId for this pattern (/^(\\d){6,}#[a-zA-Z0-9]{65,}$/)

Edit: I tried removing the " " around the regex but it gives me this error: mismatched input ')' expecting {'{', '/', PATH_SEGMENT}

enter image description here

(I know the regex is OK, but I don't know why it won't work in the code I wrote)

Upvotes: 1

Views: 296

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

You're getting the syntax mixed up between Realtime Database and Firestore.

In Realtime Database security rules, the regular expression is specific as a JavaScript regex, so enclosed in / for opening and closing.

In Firestore security rules the regular expression needs to be passed as a string, which also means it shouldn't be wrapped in / symbols.

So:

allow create: if docId.matches("^(19|20)[0-9][0-9][-\\/. ](0[1-9]|1[012])[-\\/. ](0[1-9]|[12][0-9]|3[01])$");

enter image description here

Upvotes: 2

Related Questions