2016-10-11 12 views
1

no_of_ordersフィールドに保存できる最大整数を2に制限するルールを追加したいとします。 OrderScheduleドキュメントデータは、私が開発しているアプリを介してあらかじめ設定されています。Firebase DBにルールを追加する必要がある

ここにはjsonExportというデータがあります。

{ 
    "OrderSchedule" : { 
    "13-Oct-2016" : { 
     "13:00 - 14:00" : { 
     "no_of_orders" : 4 
     } 
    } 
    }, 
    "Orders" : { 
    "-KTpIn4HeuDBDliCnTSA" : { 
     "car_Model" : "Van", 
     "date_Created" : 1476212105, 
     "date_Scheduled" : "13-Oct-2016", 
     "service_Type" : "Shine", 
     "status" : "Scheduled", 
     "time_Scheduled" : "13:00 - 14:00", 
     "uid" : "sms|57f7b267e618d33da23e65ce" 
    }, 
    "-KTpJ6BREc695pv4Ue6s" : { 
     "car_Model" : "SUV", 
     "date_Created" : 1476212188, 
     "date_Scheduled" : "13-Oct-2016", 
     "service_Type" : "Detailed", 
     "status" : "Scheduled", 
     "time_Scheduled" : "13:00 - 14:00", 
     "uid" : "sms|57f7b267e618d33da23e65ce" 
    }, 
    "users" : { 
    "sms|57f7b267e618d33da23e65ce" : { 
     "created_at" : 1475868493, 
     "email" : "[email protected]", 
     "phone" : "+0000000000", 
     "username" : "balouza" 
    }, 
    "sms|57fa9701e618d33da240efa5" : { 
     "created_at" : 1476040501, 
     "email" : "[email protected]", 
     "phone" : "+00000000000", 
     "username" : "bolzo" 
    } 
    } 
} 

セキュリティルール:

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null", 
     "max": { 
     ".write": "auth != null" 
     }, 
     ".OrderSchedule": { 
      "$date": { 
      "$timeSlot": { 
       "no_of_orders": { 
       ".validate": "newData.val() <= root.child('max').val()" 
       } 
      } 
      } 
     } 
    } 
} 

Screenshot of firebase db

+1

databaseJSONツリーをスニペットではなくテキストとして提供します。 – Dravidian

+0

妥当な要件のようです。どうしたの? –

+0

@Dravidianここに行きます。 –

答えて

1

データベース内の親ノードkey-valueペアを保存します。

セキュリティルールで
{ 
    max : 2, 
    "OrderSchedule" : { 
    "13-Oct-2016" : { 
     "13:00 - 14:00" : { 
     "no_of_orders" : 4 
     } 
    } 
    }, 
    "Orders" : { 
    "-KTpIn4HeuDBDliCnTSA" : { 
     "car_Model" : "Van", 
     "date_Created" : 1476212105, 
     "date_Scheduled" : "13-Oct-2016", 
     "service_Type" : "Shine", 
     "status" : "Scheduled", 
     "time_Scheduled" : "13:00 - 14:00", 
     "uid" : "sms|57f7b267e618d33da23e65ce" 
     }, 
     ... 

    } 

だけ追加します -

{ 
    "rules": { 
    "max" : { 
    ".write" : "false" 
    }, 
    "OrderSchedule": { 
     "$date":{ 
     "$timeInterval":{ 
      "no_of_orders":{ 
      ".validate": "newData.val() <= root.child('max').val()" 
      } 
     } 
    } 
    } 
    } 
} 

そして、あなたが読んで、あなたのデータベースに書き込み、これらを使用するだけ認証されたユーザーをしたい場合: -

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null", 
    "max" : { 
    ".write" : "false" 
    }, 
    "OrderSchedule": { 
     "$date":{ 
     "$timeInterval":{ 
      "no_of_orders":{ 
      ".validate": "newData.val() <= root.child('max').val()" 
      } 
     } 
    } 
    } 
    } 
} 
+0

おかげドラヴィダが、なぜそれは私にこれを与えていますエラー:キー名に "。"、 "#"、 "$"、 "/"、 "["、 "]"を含めることはできません。中括弧は大丈夫です。しかし、それは$記号を受け入れません。奇妙なことは、私がそれを取り除いてもです。同じエラー!!! –

+0

質問をセキュリティルールで更新できますか?それはある種のタイプミスでなければなりません。あなたのルールやコンソールでこのエラーはどこで発生しますか? – Dravidian

+0

ああ私はそれが ".OrderSchedule"を見つけた。ありがとう@Dravidian –

関連する問題