Validation using the Bolt Compiler

You may notice that these rules for user friends look similar to the rules
that we wrote for the users themselves. I could essentially copy
the user validation rules for a user objects stored here and paste
them for the user validation rules for user objects stored in the user’s node. But copying and pasting this user
structure throughout my rules kind of makes me cringe a bit and
it also makes the rules a lot harder to maintain when I want
to change one thing. So I did a little bit of research
on how to simplify my rules, and on the Firebase and rule security page,
they suggest the Bolt compiler. The Bolt compiler is a officially
supported high-level modeling and security language that compiles
to Firebase JSON rules. And Bolt’s pretty cool because it
allows you to abstract out some of this repetitive validation code,
among other features. Now, before you dive into
the Bolt quick start, it should be noted that using
Bolt is completely optional. Also, you should have a firm grasp of
the rules language itself before diving too far into Bolt. The compiler is self described
as a bit experimental. Meaning that it is relatively young and
actively being worked on. So there are still a few kinks and
things might change. With those caveats. Let’s take a look at how you’d write
the rules that you just wrote. But using bolt. To start, you’ll need to set up. Now if everything goes smoothly,
this should only take a few minutes. And involves first downloading npm. Npm stands for a node package manager
and is a JavaScript package manager. Bolt is written in JavaScript,
hence the need for npm. Once you have npm you can install Bolt. I’ve provided the instructions
in the instructor notes, so take a moment now and install Bolt. Once you have Bolt installed,
you can write a rules dot Bolt file. Ended the directory with
your rules stop.bolt file, you could then read
the following in a terminal. This will convert the bolt
file into a JSON rules file which you can then copy over
manually into fire base. So let’s see the bolt file for
user friends might look like. Remember the bolt is
a modeling language. This means that it allows you to write
typed models for your fire base object and then it generates all
the necessary validation code. We have a user type, a timestamp
type and an encoded email type. This allows you to abstract out
all the qualities of a user or a timestamp into one place. These objects here can
contain property keys and you can specify the type of
each of these properties. This EncodedEmail type extends a string which means that everything that’s
an encoded email will also be a string. But there’s an additional validation
that the string has to have the format of an email address. And we can see up here
that this property email is using this type EncodedEmail. Now this is all well and good. But it isn’t saying where in our rules. To use these types. To do that you have to use
something that both calls paths. Now instead of forcing you to write
out the nested JSON structure. You right paths and
then specify the rules within the path. You could also say the value. At the location that is
referenced by the path. Is of one of the types that you defined
above using is the type that it is. So here we’re specifying that whatever
is stored at the path userFriends, userEncodedEmail, friendEncodedEmail
is a user object. And then in turn,
we say what’s stored at users and then userEncodedEmail
is also a user object. So the validation for these two things
is going to look exactly the same. And then once you’ve got your
bolt file you can save it, and then run this command
that I showed you before. Which then in turn
generates a json file, which should look pretty darn similar
to the json file that we just made. And that was with very minimal,
to no, Copy and Paste.

Leave a Reply

Your email address will not be published. Required fields are marked *