Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String functions in Cloud Firestore Rules

I need to remove a plus signal at string returned by request.auth.phone_number. For that, I've tried to use the replace function, but I've received the following error: "Function not found error: Name: [replace].";

match /test/{id} {
  allow read, update, delete, create: if request.auth != null && (resource.data.items[request.auth.token.phone_number.replace('+', '')] == true || resource == null);
}

This works fine when I run at Realtime Database. For instance:

"tests": {
  "$uid": {
    ".write": "auth.uid.replace('+', '') === '5521999991234'"
  }
}   

Is there any way to use string functions like "contains(), replace(), toLowerCase()" and so on, in Cloud Firestore Databases?

Thanks

like image 781
Cristian Medina Avatar asked Jun 17 '26 11:06

Cristian Medina


2 Answers

You could write a function like this :

function replace(string, replace, by) {
  return string.split(replace).join(by)
}

and use it so..

replace('+3312312345', '+', '')

in your example :

match /test/{id} {
  allow read, write: if request.auth != null && 
  (resource.data.items[replace(request.auth.token.phone_number, '+', '')] 
  == true || resource == null);
}

// below your rules
function replace(string, replace, by) {
  return string.split(replace).join(by)
}
like image 112
MichelDelpech Avatar answered Jun 19 '26 14:06

MichelDelpech


Firestore security rules use a completely different language than Realtime Database.

You can see a list of all methods available on string objects in the API documentation. The only thing you are asking to do that's actually available is lower and matches.

like image 43
Doug Stevenson Avatar answered Jun 19 '26 14:06

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!