2017-04-09 34 views
0

我已经有一段时间与火力点工作,我喜欢它,但今天我工作的安全规则和我得到与模拟器的错误,我的代码看起来如下:火力地堡规则长度校验错误

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

工作正常,任何想法,为什么发生这种情况,我已经readed上的文档:https://firebase.google.com/docs/database/security/securing-data和许多其他的例子,我只是找不到错误。非常感谢你的建议和快乐的编码。

回答

0

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

{ 
    "rules": { 

    "users":{ 
     "$uid":{ 

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

     ".validate":"newData.child('profile').child('userName').isString()&& newData.val().length < 15" 
     } 
    } 
    } 
} 
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" 
     } 
     } 
    } 
    } 
}