Firebase 规则长度验证错误

Firebase rules length validation error

我使用 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 和许多其他示例,但我找不到错误。非常感谢您的建议和愉快的编码。

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

{
  "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"
        }
      }
    }
  }
}

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

    {
  "rules": {

    "users":{
       "$uid":{

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

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