Reputation: 93
I have a firestore collection whose documents contain two optional date fields (not all documents contain them). I am not sure how to use the security rules to enforce the fields content type although they may be missing.
allow write: if request.resource.data.get("RecordDate", ?) is timestamp;
but I am not sure what could be the second argument for get() in this case (the "?").
*(based on the documentation here: https://firebase.google.com/docs/firestore/security/rules-fields#enforcing_types_for_optional_fields)
allow write: if request.resource.data.recordDate == request.time;
Hope the questions are clear enough- pls bear with me, I am a newbe.
Upvotes: 3
Views: 680
Reputation: 599571
You can use a simple or condition like this:
if !("RecordDate" in request.resource.data.keys)
||(request.resource.data["RecordDate"] is timestamp)
Upvotes: 1
Reputation: 4162
Firebase Security Rules cannot modify or change the values requested, you can only allow or deny a request. If you want to ensure a certain field is of a timestamp, you can only compare it to the valid types that are listed in the reference documentation:
Firestore Security Rules: Types - Timestamp
To allow for missing fields you can use an OR operator ||
and wrap conditions inside ()
to group their logic together.
request.auth.uid != null || (resource.data.visibility == 'public' && request.auth.uid == resource.data.owner);
Upvotes: 0