Skip to content

Commit 1085a42

Browse files
authored
Merge pull request #5 from Bullfrog1234/expanded-config
Introduce new expanded configuration
2 parents 3382012 + fca9261 commit 1085a42

15 files changed

+488
-106
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
rules/*
2+
rules/*
3+
rulez.config.js
4+
firebase.json

README.md

Lines changed: 110 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,45 @@ Give it a go and feel free to add additional helper functions to the repository!
1717

1818
Make sure you have Node.js installed on your system (the newer, the better).
1919

20-
## Setup
20+
## Installation
2121

22-
1. Install firestore-rulez in the folder where your firestore.rules file needs to be generated:
22+
NPM:
2323

2424
```bash
2525
npm i firestore-rulez --save-dev
2626
```
2727

28-
2. Create a subfolder called `/rules`.
28+
yarn:
2929

30-
3. Add as many .rules files to the folder as you like.
30+
```bash
31+
yarn add firestore-rulez --dev
32+
```
33+
34+
# Basic Usage
35+
36+
1. Create a subfolder called `/rules`.
37+
38+
2. Add as many .rules files to the folder as you like.
39+
40+
3. Generate firestore.rules via the following command in your console:
41+
42+
```bash
43+
firestore-rulez
44+
```
45+
46+
# Advanced Usage
3147

32-
## Run
48+
1. Create [config file](#configuration-file-syntax) at project root
3349

34-
## Generate firestore.rules file
50+
2. Generate .rules files and helper functions inline with config file
51+
52+
3. Generate firestore.rules via the following command in your console:
53+
54+
```bash
55+
firestore-rulez
56+
```
57+
58+
## Generate rules file
3559

3660
You can run Firestore-Rulez by hitting `firestore-rulez` in your CLI.
3761

@@ -45,67 +69,130 @@ This will create the firestore.rules file combining your files in the following
4569
service cloud.firestore {
4670
match /databases/{database}/documents {
4771

48-
// -> HELPER FUNCTIONS, if enabled
72+
// -> LIBRARY HELPER FUNCTIONS, if enabled
73+
74+
// -> YOUR HELPER FUNCTIONS, if enabled
4975

50-
// -> YOUR FILES
76+
// -> YOUR RULES FILES
5177

5278
}
5379
}
5480
```
5581

56-
## Config
82+
# Configuration File Syntax
83+
84+
Firestore-Rulez can be configured by adding a rulez.config.js file to the `project root` or `./rules` folders.
85+
86+
The file is to export a object with the following syntax:
87+
88+
| Field | Default Value | Type | Description |
89+
| ----- | --------------- | ----- | ------------- |
90+
| helpers | `["authUserEmail", "authUserEmailIsVerified", "authUserUid", "existingData", "hasAmtOfWriteFields", "incomingData", "isAuthenticated"]` | array(strings) \| boolean | used to add helper functions to the output, this can be `true` to include all helper functions or `false` to include non of the helper functions or an array of the [function names](#helper-functions) |
91+
| custom_helpers_folder | `null` | `null` \| string | path to user defined helper functions |
92+
| rules_version | `"1"` | `"1"` \| `"2"` \| `1` \| `2` | which version is the rules written in |
93+
| rules_folder | `"rules"` | string | folder where the rule fragments can be found |
94+
| rules_output | `"firestore.rules"` | string | name of the file to output to |
95+
| use_firebase_config | `false` | boolean | use the firebase config file `firebase.json` to get the rules output file name and location |
5796

58-
Firestore-Rulez can be configured by adding a rulez.config.js file to the `./rules` folder. At this point, the file can contain following settings:
97+
## Default Configuration File
5998

6099
```js
61100
module.exports = {
62-
// Enables helper functions as specified below
63-
helpers: false,
64-
rules_version: '1'
65-
}
101+
helpers: [
102+
"authUserEmail",
103+
"authUserEmailIsVerified",
104+
"authUserUid",
105+
"existingData",
106+
"hasAmtOfWriteFields",
107+
"incomingData",
108+
"isAuthenticated",
109+
],
110+
custom_helpers_folder: null,
111+
rules_version: 1,
112+
rules_folder: "rules",
113+
rules_output: "firestore.rules",
114+
use_firebase_config: false,
115+
};
66116
```
67117

68118
# Helper Functions
69119

70-
The following helper functions are present, if the helpers option is enabled:
120+
The following helper functions are present, if the helpers option is enabled or the function is included:
121+
| name | description |
122+
| --- | ---|
123+
| isAuthenticated | Checks if user is authenticated |
124+
| authUserUid | Returns Current Auth User's Uid |
125+
| authUserEmail | Returns Current Auth User's Email |
126+
| authUserEmailIsVerified | Returns wether Current Auth User's Email is verified |
127+
| existingData | Returns the existing data |
128+
| incomingData | Returns the incoming data |
129+
| hasAmtOfWriteFields | Checks if the request has X write fields |
130+
131+
Use the name of the functionin the rules files and in the configuration file to enable them in the configuration or set the helpers function to true to include them all.
132+
133+
## isAuthenticated
71134

72135
```js
73136
// Checks if user is authenticated
74137
function isAuthenticated() {
75-
return request.auth != null
138+
return request.auth != null;
76139
}
140+
```
141+
142+
## authUserUid
77143

144+
```js
78145
// Returns Current Auth User's Uid
79146
function authUserUid() {
80-
return request.auth.uid
147+
return request.auth.uid;
81148
}
149+
```
150+
151+
## authUserEmail
82152

153+
```js
83154
// Returns Current Auth User's Email
84155
function authUserEmail() {
85-
return request.auth.token.email
156+
return request.auth.token.email;
86157
}
158+
```
159+
160+
## authUserEmailIsVerified
87161

162+
```js
88163
// Returns wether Current Auth User's Email is verified
89164
function authUserEmailIsVerified() {
90-
return request.auth.token.email_verified
165+
return request.auth.token.email_verified;
91166
}
167+
```
168+
169+
## existingData
92170

171+
```js
93172
// Returns the existing data
94173
function existingData() {
95-
return resource.data
174+
return resource.data;
96175
}
176+
```
97177

178+
## incomingData
179+
180+
```js
98181
// Returns the incoming data
99182
function incomingData() {
100-
return request.resource.data
183+
return request.resource.data;
101184
}
185+
```
102186

187+
## hasAmtOfWriteFields
188+
189+
```js
103190
// Checks if the request has X write fields
104191
function hasAmtOfWriteFields(size) {
105-
return request.writeFields.size() == size
192+
return request.writeFields.size() == size;
106193
}
107194
```
108195

109-
### Credits
196+
# Credits
110197

111198
Thanks to [OneLunch Man](https://stackoverflow.com/users/10747134/onelunch-man) for inspiring me to build this module on Stack Overflow.

package-lock.json

Lines changed: 106 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
"access": "public"
1919
},
2020
"dependencies": {
21+
"chalk": "^4.1.0",
2122
"concat": "^1.0.3",
22-
"glob": "^7.1.6"
23+
"figlet": "^1.5.0",
24+
"glob": "^7.1.6",
25+
"yup": "^0.29.3"
2326
},
2427
"bin": {
2528
"firestore-rulez": "./src/index.js"

0 commit comments

Comments
 (0)