Firestore安全规则检查引用是否存在

Ich*_*sos 14 firebase firebase-security google-cloud-firestore

我想知道如何检查文档值是否是对另一个文档的引用,并且该文档是否存在使用firebase安全规则.

我尝试了什么:

function validate(document) {
    return exists(document.reference)
}

match /collection/{document} {
    allow read: if request.auth != null;
    allow create, update: if isAdmin(request.auth.uid) && validate(request.resource.data);
}
Run Code Online (Sandbox Code Playgroud)

由于这不起作用,我试图弄清楚是什么类型document.ref.不幸的是,它似乎没有列出的任何类型:https: //firebase.google.com/docs/firestore/reference/security/?autuser = 0#data_types

我试过,path因为这是最明显的工作存在.我没想到它会起作用.另一个猜测可能是mapstring.两者都不正确.

因为我不知道这可能是什么,并且没有任何记录如何将引用转换为路径,我现在必须在这里问.

有没有人找到解决方案?

TL; DR:

我需要检查使用Firestore安全规则是否存在文档中保存的引用并且它是否存在于数据库中.

谢谢,丹尼斯

Dig*_*iro 10

resource == null您可以使用或检查资源是否已存在resource != null

例子:

allow write: if resource == null //Only can create, not update
Run Code Online (Sandbox Code Playgroud)


Ger*_*rdo 5

在这一行:

allow create, update: if isAdmin(request.auth.uid) && validate(request.resource.data)

而不是调用"request.resource.data",你应该只调用"resource.data":

allow create, update: if isAdmin(request.auth.uid) && validate(resource.data)
Run Code Online (Sandbox Code Playgroud)

如所提到的在这里,资源变量代表公司的FireStore文件,而" 请求 "变量代表在该路径制成,因此不包含关于文档中的实际值的信息的请求.

试一试,让我知道它是否适合你.