How to limit string length in firebase

ぃ、小莉子 提交于 2020-04-30 16:36:15

问题


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


回答1:


For Cloud Firestore you can validate that the description field is no longer than 100 characters with:

service cloud.firestore {
  match /databases/{database}/documents {
    match /col1/doc1 {
      allow write: if resource.data.description.length <= 100;

        match /subcollection1/{doc=**} {
          allow  write: if resource.data.description.length <= 100;
        }
    }
  }
}

This applies to col1/doc and all documents in subcollection1. Note that these rules will not limit the length of the description, since security rules cannot modify the data that is written. Instead the rules reject writes where the description is longer than 100 characters.

There is no way (that I know of) to apply rules to each subcollection of only one document. The closest I know is to apply it to all documents and their subcollections:

match /col1/(document=**} {
  allow write: if resource.data.description.length <= 100;
}

This applies the validation to all documents in col1 and in all subcollections under that.



来源:https://stackoverflow.com/questions/53437482/how-to-limit-string-length-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!