如何在Firestore中键入检查可选属性

时间:2017-10-08 11:14:59

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

我无法弄清楚如何在Firestore中执行可选属性。它似乎没有在文档中涵盖,以下内容对我失败。

sum_adult=[]

    class Guest:
        def __init__(self, name, adult, child, infant):
            self.name = name
            self.adult = adult
            self.child = child
            self.infant = infant

            sum_adult.append(self.adult)


    adam_s = Guest('Adam Smith', 2, 0, 1)
    sandra_b = Guest('Sandra Bind', 2, 0,1)
    alince_d = Guest('Alice Donovan',2,0,0)
    parents_smith = Guest('Mr&Ms Smith', 2,0,0)

    print(sum(sum_adult))

1 个答案:

答案 0 :(得分:2)

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

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