如何在 Firebase 中引用子通配符变量

How can I refer to a child wildcard variable in Firebase

在我正在创建的 iOS 消息传递应用程序中,消息节点的结构大纲如下

    "messages": {
        "$uid":{
            ".read": "auth.uid == $uid",
            "$messageId":{

                // insert .write rule here

                "toUid":{
                    // if its a group message then there would be multiple "$toUid" children
                    "$toUid":{

                    },
                },
                "fromUid":{
                    "$fromUid":{

                    }
                },
                "timeStamp":{

                },
                "group":{
                    "isGroupMessage":{

                    },
                    "groupId":{

                    }
                }
            }
        }
    },

我想在 $messageId 之后添加一条写入规则,以确保 "$toUid" 或 "$fromUid" 等于 $uid 。

有什么办法可以做到这一点吗?

以下是最好的方法吗:

 ".write": "newData.child('toUid').hasChild($uid) || newData.child('fromUid').hasChild($uid)",

** 我采用了以下解决方案 - 更改结构,但这是我们目前使用的最快/最安全的安全规则。

MVP 最初不会有组消息(可能无关紧要,但逻辑可能不同)- 因此我可以添加一个通配符子变量“$friendUid”,然后在其下方添加以下规则。

 "$messageId":{
    "$friendUid":{
    ".write": "$uid == auth.uid || $friendUid == auth.uid",

这就是我决定为我的消息节点构建我的 Firebase 数据库规则的方式。我添加了一个带有通配符变量“$friendUid”的额外子节点,以便轻松引用它并锁定我的消息分支。

    "messages": {
        "$uid":{
            ".read": "auth.uid == $uid",
            "$messageId":{
                "$friendUid":{
                    ".write": "$uid == auth.uid || $friendUid == auth.uid",
                    ".validate": "root.child('friends/'+$uid+'/'+$friendUid).exists()",

                    "toUid":{
                        "$toUid":{
                            ".validate": "$toUid == $friendUid || $toUid == $uid"
                        },
                    },
                    "fromUid":{
                        "$fromUid":{
                            ".validate": "$fromUid == $friendUid || $fromUid == $uid"
                        },
                    },
                    "timeStamp":{
                        ".validate":"newData.isNumber()"
                    },
                    "$other":{
                        ".validate": false
                    }
                }
            }
        }
    },