【问题标题】:Firebase rules length validation errorFirebase 规则长度验证错误
【发布时间】:2017-04-10 11:25:51
【问题描述】:

我使用 firebase 已经有一段时间了,我很喜欢它,但今天我正在研究安全规则,但我遇到了模拟器错误,我的代码如下所示:

    {
  "rules": {

    "users":{
       "$uid":{

        ".read": "auth.uid != null",
        ".write": "auth.uid != null",

         ".validate":"newData.child('profile').child('userName').isString()&& newData.val().length < 15"
      }
    }  
  }
}

仅在我添加长度验证时出现错误。当我这样做时:

{
  "rules": {

    "users":{
       "$uid":{

        ".read": "auth.uid != null",
        ".write": "auth.uid != null",

         ".validate":"newData.child('profile').child('userName').isString()"
      }
    }  
  }
}

工作正常,知道为什么会这样,我已经阅读了以下文档:https://firebase.google.com/docs/database/security/securing-data 和许多其他示例,但我找不到错误。非常感谢您的建议和愉快的编码。

【问题讨论】:

    标签: firebase firebase-realtime-database firebase-security


    【解决方案1】:

    您可以按照此示例向您的字段添加这样的验证。

    {
      "rules": {
        "users": {
          "$user_id": {
            // grants write access to the owner of this user account
            // whose uid must exactly match the key ($user_id)
            ".write": "$user_id === auth.uid",
            ".read" : "$user_id === auth.uid",
            "familyName" : ".validate": "newData.isString() && newData.val().length > 1 && newData.val().length < 100",
            "givenName" : ".validate": "newData.isString() && newData.val().length > 1 && newData.val().length < 100",
            "age" : ".validate": "newData.isNumber() && newData.val() > 13 && newData.val() < 110",
            "email": {
              // an email is only allowed in the profile if it matches
              // the auth token's email account (for Google or password auth)
              ".validate": "newData.val() === auth.email"
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      好的,我已经解决了正确的语法:

          {
        "rules": {
      
          "users":{
             "$uid":{
      
              ".read": "auth.uid != null",
              ".write": "auth.uid != null",
      
               ".validate":"newData.child('profile').child('userName').isString()&& newData.val().length < 15"
            }
          }  
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        相关资源
        最近更新 更多