Created
December 31, 2019 07:33
-
-
Save sgr-ksmt/56543e02736f16041211b9637ecc2cc7 to your computer and use it in GitHub Desktop.
Convenience `firestore.rules`'s function.
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
rules_version = '2'; | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
// Create `Path` from array of string. | |
function documentPath(paths) { | |
return path([ | |
['databases', database, 'documents'].join('/'), | |
paths.join('/') | |
].join('/')); | |
} | |
// Requested User is authenticated. | |
function isAuthenticated() { | |
return request.auth != null; | |
} | |
// Requested User is authenticated and `auth.uid` equal to `userID` | |
function isUserAuthenticated(userID) { | |
return request.auth.uid == userID; | |
} | |
// Returns request.resource.data | |
function incomingData() { | |
return request.resource.data; | |
} | |
// Returns resource.data. | |
function existingData() { | |
return resource.data; | |
} | |
// Retrieve data from `path`. | |
function getData(path) { | |
return get(path).data; | |
} | |
// Retrieve data from `path` (for atomic operation such as batch and transaction). | |
function getAfterData(path) { | |
return getAfter(path).data; | |
} | |
// Whether request.time matches the specified time. (Use `FieldValue.serverTimestamp()` | |
// (FieldValue.serverTimestamp()を使うことで一致する) | |
function isRequestedTime(time) { | |
return time == request.time; | |
} | |
// string validation | |
function validateString(text, min, max) { | |
return text is string | |
&& min <= text.size() | |
&& text.size() <= max; | |
} | |
// number validation | |
function validateNumber(num, min, max) { | |
return num is number | |
&& min <= num | |
&& num <= max; | |
} | |
// Whether the value is the same before and after the change | |
function isNotChanged(key) { | |
return incomingData()[key] == existingData()[key]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment