Firestore数据库规则中的ID匹配 [英] Id match in firestore database rules
问题描述
我正在制定我的安全规则,但是当我尝试将文档ID与正则表达式匹配时,它将无法正常工作.我尝试使用 matches
函数,但似乎不接受该方法.
即使我尝试使用Firebase模式YYYY-MM-DD(/^(19 | 20)[0-9] [0-9] [-\\/.](0 [1-9] | 1 [012])[-\\/.](0 [1-9] | [12] [0-9] | 3 [01])$ /
)来自
我正在尝试检查此模式的 roomId
(/^(\\ d){6,}#[a-zA-Z0-9] {65,} $/
)
我尝试删除""
,但它给了我这个错误:不匹配的输入')'期望{'{','/',PATH_SEGMENT}
(我知道正则表达式可以,但是我不知道为什么它不能在我编写的代码中使用)
您正在将Realtime Database和Firestore之间的语法混淆.
在实时数据库安全规则中,正则表达式特定为JavaScript正则表达式,因此将其包含在/
中以进行打开和关闭.
在Firestore安全规则中,正则表达式需要以字符串形式传递,这也意味着不应将其包装在/
符号中.
所以:
允许创建:if docId.matches("^(19 | 20)[0-9] [0-9] [-\\/.](0 [1-9] | 1 [012])[-\\/.](0 [1-9] | [12] [0-9] | 3 [01])$");
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])$");
这篇关于Firestore数据库规则中的ID匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!