How to format a firestore.rules file with a tool like prettier?
Asked Answered
M

2

7

For example, I have these rules:

    function isValidTimestamp(timestampValue){return request.time==timestampValue;}

    match /users/{userID}{allow get: if resource.auth != null;allow create:if isValidTimeStamp(request.resource.data.createdAt)}

i would like to have something like this:

function isValidTimestamp(timestampValue){  
  return request.time == timestampValue;
}

match /users/{userID} {
  allow get: if resource.auth != null; 
  allow create: if isValidTimeStamp(request.resource.data.createdAt);
}

Is it possible to lint/format this file?

Mosque answered 11/2, 2020 at 23:55 Comment(0)
C
5

Unfortunately, there isn't a software developed yet, that formats the structure of Firestore rules like Prettier does. As mentioned in this conversation on Github here, it seems that talking about improving Prettier to format Firebase rules - which would include the Firestore ones - but for now, indeed, it seems that it's not possible. (I would really like that it worked as well, it would be really helpful)

Let me know if the information clarified you!

Cordiacordial answered 12/2, 2020 at 8:26 Comment(0)
T
0

VS Code users only

Although this is not what the OP asked for, I found this extension that does both syntax highlighting and formatting for my Firestore rules file.

Turdine answered 4/12, 2023 at 12:45 Comment(3)
Well, that extension does not work. Even with that extension installed, when I press SHIFT+ALT+F to format my firestore.rules file, VSCode still says that 'there is no formatter' for this file...Exactly
It works for me on save (Cmd + S) but I'm not sure why it doesn't work for you. Can you see "Firestore Rules" appearing on the bottom right corner of VSCode?Turdine
Hey @Turdine - there is the 'Firebase Rules' button. When clicked it prompts a 'Select Language Mode' in the command line. Should it not format the file when I press the 'format' key combo? The ALT+SHIFT+F shows an alert 'There is no formatter for 'firerules' files installed.' and options 'Install formatter...' and 'Cance'...am I missing something here?Exactly

© 2022 - 2025 — McMap. All rights reserved.