1

I have the following data structure:

{"applicants":[
{
"-KNbKoNzPGsz2TAQp-j9" : {
"email" : "zahavi@post.tau.ac.il",
"id" : "056042344",
"name" : "Jacob",
"prefix" : "Prof.",
"surname" : "Zahavi"
},
"-KNbWPpsrFGJBDaWK2EN" : {
"email" : "chamud@gmail.com",
"id" : "314965211",
"name" : "Ilay",
"prefix" : "Mr.",
"surname" : "Shvo"
},
"-KNd8HvkzAYi0LtG9qNj" : {
"email" : "zahavi@zpost.tau.ac.il",
"id" : "056042344",
"name" : "Jacob",
"prefix" : "Prof.",
"surname" : "Zahavi"
}]
}
}

I would like to enforce uniqueness rule on the field id, so it will not be possible to create two entries in applicants collection that has the same id value.

currently my rules are:

{
"rules": {
".read": true,
".write": true,
  "Tables":{"applicants":{"id":{".indexOn":".value"}}}
}
}

I explored two possible solutions that are: 1. transaction. unfortunately i did not find a way to run transaction from a query. 2. rules. but failed to find something suitable.

Edit (from comments):

I tried the following rules:

{
    "rules": {
        ".read": true,
        ".write": true,
        "Tables": {
            "applicants": {
                "$app": {
                    "id": {
                        ".indexOn": ".value",
                        ".validate": "!root.child('Tables').child('applicants').child(newData.child('id').val()).exis‌​ts()"
                    },
                }
            }
        }
    }
}

and I always got creation failed permission denied even for values that were not exist

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
David Shvo
  • 11
  • 1
  • Show what you've tried already and why it didn't work. Otherwise this question has been asked before, e.g. in http://stackoverflow.com/questions/16339092/a-way-to-enforce-a-unique-property-in-firebase and http://stackoverflow.com/questions/35243492/firebase-android-make-username-unique – Frank van Puffelen Jul 26 '16 at 23:12
  • i tried the following rules: { "rules": { ".read": true, ".write": true, "Tables": { "applicants": { "$app":{ "id": { ".indexOn": ".value", ".validate": "!root.child('Tables').child('applicants').child(newData.child('id').val()).exists()" }, } } }} } and i always got creation failed permission denied even for values that were not exist – David Shvo Jul 30 '16 at 16:01

0 Answers0