{"id":15154568,"url":"https://github.com/jahed/firebase-rules","last_synced_at":"2025-05-02T07:33:39.799Z","repository":{"id":57120038,"uuid":"194335698","full_name":"jahed/firebase-rules","owner":"jahed","description":"A type-safe Firebase Real-time Database Security Rules builder. Compose and re-use common rules. Reference constants used throughout the project. Catch any errors and typos. Auto-completion.","archived":false,"fork":false,"pushed_at":"2021-04-24T21:20:38.000Z","size":346,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T23:07:14.179Z","etag":null,"topics":["firebase","firebase-database","firebase-realtime-database","firebase-rules","json","type-safety","typescript"],"latest_commit_sha":null,"homepage":"https://jahed.github.io/firebase-rules/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jahed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-28T21:26:57.000Z","updated_at":"2024-06-01T14:32:59.000Z","dependencies_parsed_at":"2022-08-23T06:30:19.354Z","dependency_job_id":null,"html_url":"https://github.com/jahed/firebase-rules","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahed%2Ffirebase-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahed%2Ffirebase-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahed%2Ffirebase-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahed%2Ffirebase-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jahed","download_url":"https://codeload.github.com/jahed/firebase-rules/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224153588,"owners_count":17264937,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["firebase","firebase-database","firebase-realtime-database","firebase-rules","json","type-safety","typescript"],"created_at":"2024-09-26T17:41:38.386Z","updated_at":"2024-11-12T16:04:58.744Z","avatar_url":"https://github.com/jahed.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jahed/firebase-rules\n\n[![npm](https://img.shields.io/npm/v/@jahed/firebase-rules.svg)](https://www.npmjs.com/package/@jahed/firebase-rules)\n[![author](https://img.shields.io/badge/author-jahed-%23007fff)](https://jahed.dev/)\n\nA type-safe Firebase Real-time Database Security Rules builder.\n\n- Compose and re-use common rules.\n- Reference constants used throughout the project.\n- Catch any errors and typos.\n- Auto-completion.\n\n## Installation\n\n```bash\nnpm install --save-dev @jahed/firebase-rules\n```\n\n## Usage\n\nImport the modules you need to build your rules. You can create helper functions\nto reduce reptition and give your rules more context. For this example, we'll\njust use the modules directly to keep it simple.\n\n```typescript\nimport { node, props, validate, newData, read, write, equal, auth, allowAll, param, between, oneOf, not, data } from '@jahed/firebase-rules'\n\nconst rules = {\n  rules: node(props({\n    app: node(\n      props({\n        update: node(props({\n          version: node(validate(newData.isString())),\n          force: node(validate(newData.isBoolean())),\n          timestamp: node(validate(newData.isNumber()))\n        })),\n        delay: node(validate(newData.isNumber()))\n      }),\n      read(allowAll),\n      write(equal(auth.uid, 'service-admin'))\n    ),\n    users: node(\n      param('$userId', $userId =\u003e node(\n        props({\n          name: node(validate(\n            newData.isString(val =\u003e between(val.length, 0, 24))\n          )),\n          created_at: node(validate(\n            newData.isNumber(newVal =\u003e oneOf(\n              not(data.exists()),\n              data.isNumber(val =\u003e equal(val, newVal))\n            ))\n          ))\n        }),\n        write(equal($userId, auth.uid)),\n        validate(newData.hasChildren(['name', 'created_at']))\n      )),\n      read(allowAll)\n    )\n  }))\n}\n\nconst json = JSON.stringify(rules, null, 2)\n```\n\nNow you can write `json` to a file and push it to Firebase. The configuration\nabove will look like this in JSON:\n\n```json\n{\n  \"rules\": {\n    \"app\": {\n      \"update\": {\n        \"version\": {\n          \".validate\": \"newData.isString()\"\n        },\n        \"force\": {\n          \".validate\": \"newData.isBoolean()\"\n        },\n        \"timestamp\": {\n          \".validate\": \"newData.isNumber()\"\n        },\n        \"$other\": {\n          \".validate\": false\n        }\n      },\n      \"delay\": {\n        \".validate\": \"newData.isNumber()\"\n      },\n      \"$other\": {\n        \".validate\": false\n      },\n      \".read\": true,\n      \".write\": \"(auth.uid === \\\"service-admin\\\")\"\n    },\n    \"users\": {\n      \"$userId\": {\n        \"name\": {\n          \".validate\": \"(newData.isString() \u0026\u0026 ((newData.val().length \u003e 0) \u0026\u0026 (newData.val().length \u003c 24)))\"\n        },\n        \"created_at\": {\n          \".validate\": \"(newData.isNumber() \u0026\u0026 (!data.exists() || (data.isNumber() \u0026\u0026 (data.val() === newData.val()))))\"\n        },\n        \"$other\": {\n          \".validate\": false\n        },\n        \".write\": \"($userId === auth.uid)\",\n        \".validate\": \"newData.hasChildren([\\\"name\\\",\\\"created_at\\\"])\"\n      },\n      \".read\": true\n    },\n    \"$other\": {\n      \".validate\": false\n    }\n  }\n}\n```\n\nFor more thorough examples, see [the tests](tests/database.rules.ts).\n\n## API\n\nFor complete API documentation, [see the documentation website](https://jahed.github.io/firebase-rules/).\n\n## License\n\n[MIT](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahed%2Ffirebase-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjahed%2Ffirebase-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahed%2Ffirebase-rules/lists"}