Last active
April 10, 2023 06:35
-
-
Save katowulf/e0afb342851f6463ff98214b1c434179 to your computer and use it in GitHub Desktop.
Firestore security rules validation example
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
import * as firebase from "firebase/app"; | |
import 'firebase/firestore'; | |
import config from './config'; | |
firebase.initializeApp(config); | |
const data = { | |
string: 'foo', | |
integer: 23, | |
boolean: false, | |
timestamp: firebase.firestore.Timestamp.fromDate(new Date()) | |
}; | |
const doc = firebase.firestore().doc('path/to/doc'); | |
doc.set(data).then(() => console.log('success')).catch(e => console.error(e)); |
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
match /path/to/data { | |
allow read: if true; | |
allow write: if | |
// must have exactly 4 fields with keys 'string', 'integer', 'boolean', and 'timestamp' | |
request.resource.data.size() == 4 && request.resource.data.keys().hasAll(['string', 'integer', 'boolean', 'timestamp']) && | |
// string field must be less than 10 characters | |
request.resource.data.string is string && request.resource.data.string.size() < 10 && | |
// integer field must be between 0 and 1000 | |
request.resource.data.integer is int && request.resource.data.integer > -1 && request.resource.data.integer < 1001 && | |
// boolean field must be a boolean | |
request.resource.data.boolean is bool && | |
// timestamp must by a timestamp | |
request.resource.data.timestamp is timestamp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment