Home > other >  Firebase rule for collection start with number doesn't work
Firebase rule for collection start with number doesn't work

Time:05-03

I have a collection called 2022collection but when I go to set the read access in Firestore rules it errors out, I've searched the docs and stackoverflow and am not seeing anything anywhere on how to write the rule properly to allow a collection starting with a number. I can write and read to it just fine if I'm logged in to the app, but unable to make a firestore rule to make it publicly readable. Rules for collections that start with a letter work just fine but the number errors out completely.

How do I make a Cloud Firestore rule for a document/collection that starts with a number?

Here are a couple things I've tried, clearly neither work. I would argue that there should be better documentation on this as I've failed to find anything that explains it. Happy to be proved wrong if such docs exist, I've not found them in an all my time searching enter image description here

enter image description here

CodePudding user response:

You can use a wildcard to read collection name and then use matches() to check if it starts with a number. Try the follwoing rules:

match /{col}/{docId} {
  allow read, write: if col.matches("^[0-9].*");
}   

Do note that this rule will be applied for all collections since you have a wildcard.

CodePudding user response:

I ended up fixing it by changing my code from

match /2022users/{2022users} {
    allow read;
    }

to

match /2022users/{users2022} {
    allow read;
    }

After changing the second field I was able to save the rule and read the collection 2022users.

  • Related