-
-
Save StarWar/622c5327057062c5e414399f5b9b1d7c to your computer and use it in GitHub Desktop.
Firestore Rules utilities to reduce verbosity. Generator: https://scaffoldhub.io/firestore-rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service cloud.firestore { | |
match /databases/{database}/documents { | |
// START - Usage example | |
match /people/{document=**} { | |
function propertiesValid() { | |
return request.resource.data.keys().hasAll(['name', 'birthdate', 'yearsOfExperience']) | |
&& request.resource.data.size() == 3 | |
&& isString('name') && minlength('name', 3) && maxlength('name', 255) | |
&& isString('birthdate') | |
&& (isNull('yearsOfExperience') || (isNumber('yearsOfExperience'))); | |
} | |
function propertiesNonChanged() { | |
return !changed('name'); | |
} | |
allow read: if isAuthenticated(); | |
allow create: if isAuthenticated() | |
&& propertiesValid(); | |
allow update: if isAuthenticated() | |
&& propertiesValid() | |
&& propertiesNonChanged(); | |
allow delete: if isAuthenticated(); | |
} | |
match /projects/{document=**} { | |
function propertiesValid() { | |
return request.resource.data.keys().hasAll(['name', 'started', 'budget']) | |
&& request.resource.data.size() == 3 | |
&& isString('name') && minlength('name', 3) && maxlength('name', 255) | |
&& isBoolean('started') | |
&& isNumber('budget') && min('budget', 0.01) && max('budget', 50000); | |
} | |
allow read: if isAuthenticated(); | |
allow create: if isAuthenticated() | |
&& propertiesValid(); | |
allow update: if isAuthenticated() | |
&& propertiesValid(); | |
allow delete: if isAuthenticated(); | |
} | |
// END - Usage example | |
function isNull(property) { | |
return request.resource.data[property] == null; | |
} | |
function changed(property) { | |
return request.resource.data[property] != resource.data[property]; | |
} | |
function isList(property) { | |
return request.resource.data[property] is list; | |
} | |
function isString(property) { | |
return request.resource.data[property] is string; | |
} | |
function isNumber(property) { | |
return request.resource.data[property] is number; | |
} | |
function isBoolean(property) { | |
return request.resource.data[property] is bool; | |
} | |
function isMap(property) { | |
return request.resource.data[property] is map; | |
} | |
function isTimestamp(property) { | |
return request.resource.data[property] is timestamp; | |
} | |
function minSize(property, value) { | |
return request.resource.data[property].size() >= value; | |
} | |
function maxSize(property, value) { | |
return request.resource.data[property].size() <= value; | |
} | |
function maxlength(property, value) { | |
return request.resource.data[property].size() <= value; | |
} | |
function minlength(property, value) { | |
return request.resource.data[property].size() >= value; | |
} | |
function max(property, value) { | |
return request.resource.data[property] <= value; | |
} | |
function min(property, value) { | |
return request.resource.data[property] >= value; | |
} | |
function isCurrentUser(userId) { | |
return isAuthenticated() && request.auth.uid == userId; | |
} | |
function isAuthenticated() { | |
return request.auth != null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment