Firebase multi-location rules - same value

后端 未结 1 1100
清歌不尽
清歌不尽 2021-01-26 18:17

This is what I want at the end:

{
  \"data\" : {
    \"account\" : {
      \"K1472290187836\" : {
        \"created\" : 1472290190043,
        \"id\" : \"K147229         


        
相关标签:
1条回答
  • 2021-01-26 18:54

    Trimming the data back to the minimum, it seems like you want:

    {
      "data" : {
        "account" : {
          "K1472290187836" : {
            "id" : "K1472290187836"
          }
        },
        "auth" : {
          "d182ddec-f1c7-41c5-8b0e-198bfb5d9efe" : {
            "account_id" : "K1472290187836",
          }
        }
      }
    }
    

    This is probably a good start:

    {
      "rules": {
        "data": {
          "account": {
            "$id": {
              "id": {
                ".validate": "newData.val() == $id"
              }
            }
          },
          "auth": {
            "$uid": {
              ".write": "$uid === auth.uid",
              "account_id": {
                ".validate": "
    newData.parent().parent().child('account').child(newData.val()).exists() &&
    newData.parent().parent().child('account').child(newData.val()).child('id').val() === newData.val()"
              }
            }
          }
        }
      }
    }
    

    Since root and data refer to the data as it exists before the write operation, the above uses newData, which refers to the data as it will exist after the write operation (if that operation succeeds).

    You don't really need the double condition on the account_id validation, since the second one duplicates the functionality of the first. But since I wasn't sure whether you want to validate the account key or the value of the id property, I added both conditions for easy copy/paste/remove-the-one-you-don't-care-about.

    I recommend reconsidering if you really need to store the ID twice in that fragment. Data duplication is common, but in this case I don't see a lot of value gain (and it leads to considerations like the one above: which is leading?).

    0 讨论(0)
提交回复
热议问题