Reputation: 45
I am trying to do some validation on incoming data into my firebase app. My structure is at the bottom. I have removed existing validation rules for clarity - however we can assume that reads and writes are allowed at the root rules
level.
$categoryid
will look something like this:
1234: {1:{...}, 2:{...}, 3:{...}}
I want to ensure that $categoryid
(which is 1234
in the above example) is numerical - however the rule ".validate": "$categoryid.isNumeric()"
results in an "no such method or property" error.
I could check for data.child($categoryid)
in categories
, however the variable doesn't exist at that level and results in an "unknown variable" error.
I'm sure I'm missing a trick here...
{
"rules": {
"categories": {
"$categoryid": {
"$itemid": {
"members": {
"$id": {
}
}
}
}
}
}
}
Upvotes: 1
Views: 303
Reputation: 13
To do this validation you can use RegEx /^[0-9]+$/
{
"rules": {
"categories": {
"$categoryid": {
.validate": "$categoryid.matches(/^[0-9]+$/)"
"$itemid": {
"members": {
"$id": {
}
}
}
}
}
}
}
Upvotes: 0
Reputation: 1378
There is currently no good way to do this, but there is a hacky work around that involves storing the $categoryId
in a field, then checking that that field is numeric.
Using these security rules:
{
"rules": {
"categories": {
"$categoryid": {
".validate": "'' + newData.child('meta/id') === $categoryId && newData.child('meta/id').isNumber()"
"meta": {},
"items": {
"$itemid": {
"members": {
"$id": {
}
}
}
}
}
}
}
}
We can then create a new category by running:
categoriesRef.child(1234).set({meta: {id: 1234}});
These rules will check that a) the $categoryId
matches $categoryId/meta/id
and that $categoryId/meta/id
is a number.
Upvotes: 2