/firebase-json

Parser for Firebase rules JSON files

Primary LanguageJavaScriptMIT LicenseMIT

firebase-json

Parser for Firebase rules JSON files.

It supersedes RFC 7159 with those additions:

  • support multi line ("/* [...] */") and single line ("// [...]") comments;
  • support multi line strings (without escaping the line feed);
  • disallow duplicate keys in objects;
  • and allow trailing commas in objects and array.

Example

To parse Firebase rules:

const json = require('firebase-json');
const fs = require('js');
const rules = json.parse(fs.readFileSync('./rules.json', 'utf8'));

console.log(rules);

Motivation

This is primarily used to load rules for targaryen test.

The alternatives are:

  • To strip the comment with strip-json-comments and to not use multi line string.
  • To write the rules in json5 and compile them before testing them in targaryen or upload them (json5 rules using multi line string won't be compatible with Firebase format).
  • To write the rules in Firebase's bolt and compile the rules before testing them with targaryen; for deployment, firebase-tools support bolt rules.

Those solutions might limit or disrupt your current work flow or deployment. Parsing the rule might also allow us later to map a rule syntax or evaluation error to a specific place in the JSON file similarly to Firebase simulator.

Installation

npm install firebase-json

API

  • parse(json: rules): any

    Parse the firebase-json encoded string.

  • load(filePath: string, options: void|string|object): Promise<any,Error>

    Read the file and parse its firebase-json encoded content.

  • loadSync(filePath: string, options: void|string|object): any

    Read the file synchronously and parse its firebase-json encoded content.

  • ast(json: rules): {type: string, expression: object, loc: object}

    Parse the firebase-json encoded string into an to intermediary AST. It uses the ESTree AST schema and returns an "ExpressionStatement" node.

Tests

git clone git@github.com:dinoboff/firebase-json.git
cd firebase-json
npm i
npm test

Contributors

Firebase-json is maintained by Damien Lebrun.

The parser is generated by David Majda's Pegjs. The firebase-json grammar definition is based on Pegjs JSON grammar example.

License

MIT License

Copyright (c) 2017 Damien Lebrun