Firebase 验证确保子项存在并且在初始写入和更新时具有正确的值

Firebase validation make sure child exists and has correct value on initial write and update

我如何确保电子邮件和创建的字段始终存在并且电子邮件始终具有有效的电子邮件值?如果我使用 newData.hasChildren(['email','created']) 如果我只是更新记录的电子邮件字段,这将在更新时失败。

"uPrivate": {
    "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid",
        ".validate": "root.child('u/'+$uid).exists()",
        "email": {
            ".validate": "root.child('u/'+$uid).exists()",
        },
        "created": {
            ".validate": "root.child('u/'+$uid).exists()",
        }
    }
}

newData 变量在写入后将存在的位置包含新数据。来自 Firebase documentation:

the newData variable contains the new data that will exist if the write operation is successful. newData represents the merged result of the new data being written and existing data.

因此对于不在更新中的任何数据,它将包含现有值。