Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convenience `firestore.rules`'s function.
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