私に続いてprevious question私のアプリケーションは以下のような設計になっています。Firebaseのルールの検証でデータの有効性が確認されない
デザイン
- 値がユーザーIDでログインして現在になりますノード 下に格納される顧客を作成することができますログインしていたアプリケーション
- にログインできるユーザー
ここに私のアンドロイドアプリケーションを通してデータを追加する方法があります。
それは...データベースでは、それが何らかの形で以下のように表現されていますFirebaseInstance mFirebaseInstance = FirebaseDatabase.getInstance();
FirebaseDatabase mFirebaseDatabase = mFirebaseInstance.getReference("tbl-customers").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
//This will create or fetch user id node under tbl-customers.
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = inputName.getText().toString();
String email = inputCode.getText().toString();
String limit= inputLimit.getText().toString();
createUser(name, email,limit);
}
});
private void createUser(String name, String email,String limit) {
userId = mFirebaseDatabase.push().getKey();
Customer customer = new Customer(name, email,limit);
mFirebaseDatabase.child(userId).setValue(customer);
}
:
tbl-customers
|___loggedInUserId1
|___customerID1
|___customerName
|___customerCode
|___customerLimit
|___customerID2
|___customerName
|___customerCode
|___customerLimit
|___customerID3
|___customerName
|___customerCode
|___customerLimit
|___loggedInUserId2
|___customerID4
|___customerName
|___customerCode
|___customerLimit
と私は残念ながら、データのいずれかを考慮せずに挿入されている
{
"rules": {
"tbl-customers": {
".read": "auth != null",
".write": "auth != null",
"$custId": {
"customerName": {
".validate": "newData.isString() && newData.val().length < 100 && newData.val().length > 8"
},
"customerCode": {
".validate": "newData.isString() && newData.val().length<4 && !newData.exists() && newData.val().length>1"
},
"customerLimit": {}
}
}
}
}
として定義されたルールを持っています検証は各プロパティーのために書かれています。 空のデータでも、が挿入されます。
書かれたルールが適切な構造であるかどうかを考え始めたのは、データが挿入されていれば、それはtbl-customer-->loggedInUserId-->customerId
の3つのレベルしかないので、ルールはtbl-customer-->customerId
のためだけ書かれているからです。
rules
を次のように変更しました。
"$user_id": {
".validate": "auth.uid===$user_id"
...
}
、を含むことにより、1つの以上のレベルにそれを拡張する
{
"rules": {
"tbl-customers": {
".read": "auth != null",
".write": "auth != null",
"$user_id": {
".validate": "auth.uid===$user_id",
"$custId": {
"customerName": {
".validate": "newData.isString() && newData.val().length < 100"
},
"customerCode": {
".validate": "newData.isString() && newData.val().length<4 && !newData.exists()"
},
"customerLimit": {}
}
}
}
}
}
しかし、今、これはPermission Denied
例外がスローされます。この時点でアイデアはありません。誰かが正しい方向に私を導くことができますか?私は以前の質問から多くの投稿を参照しましたが、悪いことに、私はそれから多くの情報を得ることができませんでした。ルールの検証が失敗している理由と、上記の第1のルールでデータが挿入される理由についての明確な説明があることを願っています。
あなたは_Even空のデータを取得すると言います挿入されました。 [ドキュメンテーション](https://firebase.google.com/docs/database/security/securing-data#validating_data): 'null値でない値に対してのみ.validateルールが評価される' –
シミュレータはFirebaseで提供されていますコンソールは、ルールをテストし、ルールのどの部分が読み取りまたは書き込み操作に適用されているかを確認するのに便利な方法です。 –
@qbix .. newData.val()。length <100 && newData.val()。length> 10'をプロパティに追加しても、検証されません。 –