2017-04-09 33 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" 
     } 
     } 
    } 
    } 
}