Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore: Is there a way to enforce required fields for all documents in a collection?

I am manually adding data for an app I am creating. I have to add many documents to a collection, all of which have to contain the same 5 fields. Is there a way to automatically create these fields when I create a new document in the Firebase console? And to also enforce all documents have those 5 fields (so that I don't make mistakes) ?

Additionally, is there a better way to manually adding this data than from the Firebase console? Through JSON for example?

like image 936
Marco Avatar asked Oct 22 '25 14:10

Marco


1 Answers

There are two separate questions here. Both have been answered before, but never in a single question, so I'll do that here.

Can you set default values for new documents in a collection?

This is not possible. You will have explicitly write those values into each new document you create.

Can you enforce that new documents in a collection have values for certain fields?

This is possible through the use of Firebase's server-side security rules.

For example, to ensure a document has values for field1, field2 and field3, you could use hasAll:

allow write: if resource.data.keys().hasAll(['editor', 'admin']);
like image 88
Frank van Puffelen Avatar answered Oct 25 '25 12:10

Frank van Puffelen