如何在firebase中限制字符串长度 [英] How to limit string length in firebase

查看:107
本文介绍了如何在firebase中限制字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase数据库中工作.我需要限制字符串字段的长度.我该怎么办?

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

该字段的路径为:

Col1/doc1///description

Col1/doc1///description

也就是说,从集合col1开始,然后到doc1,然后对于doc1下的所有集合以及该集合下的所有文档,描述字段必须限制为100个字符.

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

推荐答案

我试图从另一个答案中获得验证,但没有成功.Firestore只是不喜欢检查资源数据值的.length.

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.

我到处搜索,这篇文章对我有帮助: https://fireship.io/snippets/firestore-rules-recipes/

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;
      }
  }
}

这篇关于如何在firebase中限制字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆