【问题标题】:How to type-check optional properties in Firestore如何在 Firestore 中对可选属性进行类型检查
【发布时间】:2017-10-08 11:14:58
【问题描述】:

我不知道如何在 Firestore 中设置可选属性。它似乎没有包含在文档中,以下对我来说失败了。

service cloud.firestore {
  match /databases/{database}/documents {
    function maybeString(val) {
      return val == null || val is string
    }

    match /myCollection/{document} {
      function mySchema() {
        return request.resource.data.name is string
          && maybeString(request.resource.data.optionalProp);
      }

      allow read: if request.auth != null;
      allow create, update: if mySchema();
    }
  }
}


service cloud.firestore {
  match /databases/{database}/documents {
    match /myCollection/{document} {
      function mySchema() {
        return request.resource.data.keys().hasAll(['name'])
          && request.resource.data.name is string
          && request.resource.data.optionalProp is string;
      }

      allow read: if request.auth != null;
      allow create, update: if mySchema();
    }
  }
}

【问题讨论】:

    标签: schema firebase-security google-cloud-firestore


    【解决方案1】:

    我正在使用第二种解决方案,但您需要使用 'fieldName' in resource.data.keys() 检查是否存在 optionalProp

    service cloud.firestore {
      match /databases/{database}/documents {
        match /myCollection/{document} {
          function mySchema() {
            return request.resource.data.keys().hasAll(['name'])
              && request.resource.data.name is string
              && (
                ! ('optionalProp' in request.resource.data.keys())
                || request.resource.data.optionalProp is string
              );
          }
    
          allow read: if request.auth != null;
          allow create, update: if mySchema();
        }
      }
    }
    

    【讨论】:

    • 有人知道这些检查方法的文档链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2010-10-02
    • 2022-01-06
    • 2018-01-20
    • 1970-01-01
    相关资源
    最近更新 更多