Firestore Security - allow only known fields

后端 未结 3 1466
遥遥无期
遥遥无期 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.

    0 讨论(0)
  • 2020-12-03 02:39

    To add on to Mike McDonald's answer, to check for particular keys, the form is now:

    request.resource.data.keys().hasAll
    

    instead of

    request.resource.data.hasAll
    

    Full example:

    // allows for creation with name and phone fields
    allow create: if request.resource.data.size() == 2
                  && request.resource.data.keys().hasAll(['name', 'phone'])
                  && request.resource.data.name is string
                  && request.resource.data.phone is string;
    // allows a single update adding the address field
    // OR (||) in additional constraints
    allow update: if request.resource.data.size() == resource.data.size() + 1
                  && !('address' in resource.data)
                  && request.resource.data.address is string;
    

    More information here: https://firebase.google.com/docs/reference/rules/rules.Map

    0 讨论(0)
  • 2020-12-03 02:42

    You can separate your rules to include different create and update (as well as delete) logic:

    // allows for creation with name and phone fields
    allow create: if request.resource.data.size() == 2
                  && request.resource.data.hasAll(['name', 'phone'])
                  && request.resource.data.name is string
                  && request.resource.data.phone is string;
    // allows a single update adding the address field
    // OR (||) in additional constraints
    allow update: if request.resource.data.size() == resource.data.size() + 1
                  && !('address' in resource.data)
                  && request.resource.data.address is string;
    
    0 讨论(0)
提交回复
热议问题