Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active April 10, 2023 06:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save katowulf/e0afb342851f6463ff98214b1c434179 to your computer and use it in GitHub Desktop.
Save katowulf/e0afb342851f6463ff98214b1c434179 to your computer and use it in GitHub Desktop.
Firestore security rules validation example
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));
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