Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit string length in firebase

I working in a firebase database. I need to limit the length of a string field. How do I do that?

The path to the field is:

Col1/doc1///description

That is, starting with the collection col1, then into doc1, then for all collections under doc1, and all documents under that collection, the description field needs to be limited to 100 characters.

Can someone please explain to me how to do this? Thanks

like image 285
Gibran Shah Avatar asked Dec 11 '22 04:12

Gibran Shah


1 Answers

I tried to get the validation from the other answer to work but was unsuccessful. Firestore just doesn't like checking .length on resource data values.

I searched around and this article helped me: https://fireship.io/snippets/firestore-rules-recipes/

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
      match /posts/{postId} {
        allow read: if true;
        allow write: if request.auth != null 
                     && request.auth.uid == request.resource.data.uid
                     && request.resource.data.body.size() > 0
                     && request.resource.data.body.size() < 255
                     && request.resource.data.title.size() > 0
                     && request.resource.data.title.size() < 255;
      }
  }
}
like image 157
RoboKozo Avatar answered Dec 25 '22 10:12

RoboKozo