Firestore Security - allow only known fields

后端 未结 3 1479
遥遥无期
遥遥无期 2020-12-03 01:46

I can’t figure out how to properly set the ‘.validate’ rule in Firestore. Basically, I want to allow a User document to contain only the fields I know:

3条回答
  •  眼角桃花
    2020-12-03 02:30

    You're looking for both the size() and hasOnly() methods.

    allow write: if request.resource.data.size() == 3 
                 && request.resource.data.keys().hasOnly(['name', 'phone', 'address'])
    

    Using size() allows you to ensure an exact number of fields. Combining that with hasOnly() allows to you lock it to those specific fields.

    You can read more in the Cloud Firestore Rules reference docs.

提交回复
热议问题