简体   繁体   中英

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).

在此处输入图像描述

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}

在此处输入图像描述

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

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])$");

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM