Firebase Rules validation and catching validation fail in android application

前端 未结 1 558
后悔当初
后悔当初 2021-01-23 02:43

Design

  • A user who can login to the application
  • Logged in user can create customers which will be stored under node whose val
相关标签:
1条回答
  • 2021-01-23 03:30

    I think that you can insert up to 5 characters because your .length for your newData.val().lenght start at 0, like an array. In other way, if you want to link uniqueness of a customerCode for each userId you should create a new table in your firebase database, Something like:

    {
      "customers": {
        "UserId1": {
          "custId1": {
            "customerCode": "customerId1",
            "customerLimit": "58866",
            "customerName": "Test New "
          },
          "custId2": {
            "customerCode": "customerId2",
            "customerLimit": "5698",
            "customerName": "Yeth"
          }
        },
        "UserId2": {
          "custId3": {
            "customerCode": "customerId3",
            "customerLimit": "5886",
            "customerName": "Test "
          },
          "custId4": {
            "customerCode": "customerId4",
            "customerLimit": "58669",
            "customerName": "New Test"
          }
        }
      }
      "customerCode": {
        "custId1": {
          "customerId1": "thh"
        }
        "custId2": {
          "customerId2": "thk"
        }
        "custId3": {
          "customerId3": "thh"
        }
        "custId4": {
          "customerId4": "tbh"
        }
      }
    }
    

    Using this model, you can modify your rules to so something like this:

    {
      "rules": {
        "customers": {
          ".read": "auth != null",
          ".write": "auth != null",
          "$CID": {
            "customerName": {
              ".validate": "newData.isString() && newData.val().length < 100"
            },
            "customerCode": {
              //Code shouls be string, less than 4 characters and it can't be already stored on customerCode table for the current userId
              ".validate": "newData.isString() && newData.val().length<3  && !root.child('customerCode').child(auth.uid).exists()"
            },
            "customerLimit": {}
          }
        }
        "customerCode": {
          "$CID": {
            "CustomerCodeId": {
              ".read": "auth != null",
              ".write": "auth != null",
            }
          }
        }
      }
    

    Maybe the rules are not perfect in the last example, but I'm sure that you can find the way about how it can fits in your app taking a look about firechat database rules. I learned a lot looking that repository.

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