Design
node
whose val
I think that you can insert up to 5 characters because your .length
for your newData.val().lenght
start at 0, like an array
. In other way, if you want to link uniqueness of a customerCode for each userId you should create a new table in your firebase database, Something like:
{
"customers": {
"UserId1": {
"custId1": {
"customerCode": "customerId1",
"customerLimit": "58866",
"customerName": "Test New "
},
"custId2": {
"customerCode": "customerId2",
"customerLimit": "5698",
"customerName": "Yeth"
}
},
"UserId2": {
"custId3": {
"customerCode": "customerId3",
"customerLimit": "5886",
"customerName": "Test "
},
"custId4": {
"customerCode": "customerId4",
"customerLimit": "58669",
"customerName": "New Test"
}
}
}
"customerCode": {
"custId1": {
"customerId1": "thh"
}
"custId2": {
"customerId2": "thk"
}
"custId3": {
"customerId3": "thh"
}
"custId4": {
"customerId4": "tbh"
}
}
}
Using this model, you can modify your rules to so something like this:
{
"rules": {
"customers": {
".read": "auth != null",
".write": "auth != null",
"$CID": {
"customerName": {
".validate": "newData.isString() && newData.val().length < 100"
},
"customerCode": {
//Code shouls be string, less than 4 characters and it can't be already stored on customerCode table for the current userId
".validate": "newData.isString() && newData.val().length<3 && !root.child('customerCode').child(auth.uid).exists()"
},
"customerLimit": {}
}
}
"customerCode": {
"$CID": {
"CustomerCodeId": {
".read": "auth != null",
".write": "auth != null",
}
}
}
}
Maybe the rules are not perfect in the last example, but I'm sure that you can find the way about how it can fits in your app taking a look about firechat database rules. I learned a lot looking that repository.