使用Firestore安全规则检查可选的日期和地图是否具有有效的类型

发布于 2025-01-18 14:03:09 字数 398 浏览 4 评论 0原文

在 Firestore 安全规则中,我们可以检查可选字符串和列表的类型,如下所示:

function reviewFieldsAreValidTypes(docData) {
  return docData.get('photo_url', '') is string &&
         docData.get('tags', []) is list;
}

将使用什么作为可选时间戳的默认值?

docData.get('dateModified', ???) is timestamp;

还有可选地图?

docData.get('translated', ???) is map;

In Firestore security rules we can check the types of optional strings and lists as follows:

function reviewFieldsAreValidTypes(docData) {
  return docData.get('photo_url', '') is string &&
         docData.get('tags', []) is list;
}

What would one use as the default value for optional timestamps?

docData.get('dateModified', ???) is timestamp;

And for an optional map?

docData.get('translated', ???) is map;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2025-01-25 14:03:09

我们只需要设置正确类型的默认值即可。我们可以使用请求对象:

function reviewFieldsAreValidTypes(docData) {
  return docData.get('photo_url', '') is string &&
         docData.get('tags', []) is list &&
         docData.get('dateModified', request.time) is timestamp &&
         docData.get('translated', request.resource.data) is map;
}

We just need to set a default value of the correct type. We can use the request object:

function reviewFieldsAreValidTypes(docData) {
  return docData.get('photo_url', '') is string &&
         docData.get('tags', []) is list &&
         docData.get('dateModified', request.time) is timestamp &&
         docData.get('translated', request.resource.data) is map;
}
各空 2025-01-25 14:03:09

没有直接解决您的问题的解决方案,但是您可以选择使用下面已发布的代码的条件:

// resource.data.keys => check if the key exists. 

if !("dateModified" in resource.data.keys()) || (resource.data.dateModified is timestamp)

if !("translated" in resource.data.keys()) || (resource.data.translated is map)

只需注意,请记住规则不是过滤编写查询以检索文档时。

There's no direct solution for your question, but you have an option to use a condition like the posted code below:

// resource.data.keys => check if the key exists. 

if !("dateModified" in resource.data.keys()) || (resource.data.dateModified is timestamp)

if !("translated" in resource.data.keys()) || (resource.data.translated is map)

Just a note, keep in mind that rules are not filters when writing queries to retrieve documents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文