我试图检查传入的字符串是否是电子邮件,所以我编写了以下Firestore安全规则:
service cloud.firestore {
match /databases/{database}/documents {
match /signups/{uid} {
// RegExp copied from https://www.regextester.com/19
allow create: if newData().email.matches("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
}
}
}
但是当我使用firebase deploy --only firestore
部署它时:
=== Deploying to '[my-project-id]'...
i deploying firestore
i firestore: checking firestore.rules for compilation errors...
i firestore: reading indexes from firestore.indexes.json...
Error: Compilation errors in firestore.rules:
[E] 5:139 - Unexpected '['.
[E] 5:149 - Unexpected ']'.
[E] 5:151 - token recognition error at: '?'
[E] 5:171 - Unexpected '['.
[E] 5:183 - token recognition error at: '?'
[E] 8:1 - Unexpected '}'.
这是Firebase控制台上的屏幕截图:
显然,RegExp字符串以'
字符(5:73)结尾。如何在Firestore规则中转义字符串?我找不到有关此问题的任何文档。
答案 0 :(得分:0)
首先,我发现'
并未结束字符串。问题是\
之前没有转义的.
。我将\
替换为\\
,并成功编译了规则。
allow create: if newData().email.matches("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");