2016-08-01 149 views
1

我正在使用Firebase & iOS并尝试验证用户何时尝试提交评论。Firebase验证规则不起作用

这里是我的规则:

{ 
    "rules": { 
    "comments": { 
     ".read": true, 
     ".write": "auth.uid != null", 
     "$comment_id": { 
     ".validate": "newData.isString() && newData.val().length < 100" 
     } 
    } 
    } 
} 

这里是我的数据库结构: enter image description here

已成功发布是当我删除了".validate"部分的注释。我也试过在$comment_id区块之外放置".validate": "newData.isString() && newData.val().length < 100",但这仍然导致失败。

无论如何,我从模拟器这个错误,当我尝试添加评论: enter image description here

为什么不是我的工作".validate"?我错过了什么?

回答

1

您的数据路径是/comments/$comment_id/comment。您的验证规则不适合那里最后的comment

{ 
    "rules": { 
    "comments": { 
     ".read": true, 
     ".write": "auth.uid != null", 
     "$comment_id": { 
     "comment": { 
      ".validate": "newData.isString() && newData.val().length < 100" 
     } 
     } 
    } 
    } 
} 

这些规则将起作用。但我可能会更新数据结构以删除comment节点,因为它不会添加任何值。