如何在Firebase规则中使用未知键检查现有数据的值? [英] How can I check the values of existing data with unknown keys in a Firebase rule?

查看:122
本文介绍了如何在Firebase规则中使用未知键检查现有数据的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase安全规则,如何检查兄弟值以确定 newData 是否有效?

在我的应用程序中,每个游戏都有 moves 列表。这里的游戏是 1405584062293



当提交新的提案时,我想验证它参照已经发挥的举措。对于初学者来说,玩家应该只能玩每一秒的动作(玩家可以从索引 0 开始动作,玩家二可以玩奇数动作)。 >

因为每一个移动的键都是一个未知的随机字符串(按照这个建议在文档)我不知道如何检查以前的举动。

以下是我的结构:

<$ p $
rules:{
.read:true,
games:{
$ game:{
moves:{
$ move:{
.write:auth!== null,
//沿着这些线的东西会让我走。
.validate:data.parent()。keys.count%2 == 0
}
}
}
}
在现实中,游戏的规则要复杂得多,我希望我需要使用Firesafe或类似的解决方案,但现在我想知道如何实现这些和其他这样简单的规则。

解决方案不是每个用例都可以适用于管理通用列表的单一推荐方法。当然不是你的。您可能会在增量ID 方法中找到更多的用处。



由于您无法对子记录键/值执行一个包含,因此您将保留一个可以递增的值。例如,利用上面的增量ID方法,我可以编写一个安全规则,以确保我正在写奇数键(只在我的轮到),如下所示:



< ((root.child('counter')。val()|| 0)+1)%2 === root.child('users' ).child(auth.uid).child('evenOrOdd').val()


Using Firebase security rules, how can I check sibling values to determine whether or not newData is valid?

In my app each game has list of moves. Here the game is 1405584062293:

When a new move is submitted, I want to validate it with reference to the moves that have already been played. For starters, a player should only be able to play every second move (player one can play even moves starting at index 0, player two can play odd moves).

Since each move's key is an unknown random string (as per this recommendation in the docs) I don't know how to check the previous move. I also don't know how to count the number of existing moves.

Here's my structure:

{
  "rules":{
    ".read":true,
    "games":{
      "$game":{
        "moves":{
          "$move":{
            ".write":"auth !== null",
            // Something along these lines would get me going...
            ".validate":"data.parent().keys.count % 2 == 0"
          }
        }
      }
    }
  }
}

In reality the rules of the game are far more complex and I expect I'll need to use Firesafe or a similar solution, but for now I'd like to know how to achieve these and other such simple rules.

解决方案

Not every use case can fit into a single, recommended approach for managing generic lists. Certainly not yours. You would probably find more use in an incremental ID approach.

Since you can't do a contains on child record keys/values, you'll maintain a value that can be incremented. For example, utilizing the incremental ID approach above, I could then write a security rule to make sure I'm writing to odd keys (and only on my turn) as follows:

".validate": "((root.child('counter').val()||0)+1) % 2 === root.child('users').child(auth.uid).child('evenOrOdd').val()"

这篇关于如何在Firebase规则中使用未知键检查现有数据的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆