Instructor: In the last section,
we spoke about how we're going to use a JSON Schema document
to validate our Redux State.
We're gonna do a little bit more footwork
inside this section,
so we're going to install the actual library
that's gonna handle the validation for us.
And I'm also gonna show you a tool
that you can use to generate a JSON Schema definition
for your own personal projects.
So first, let's install the library
that we're going to use for validation.
Back over at my terminal.
I'm gonna kill my running server,
and I'm going to install a library called tv4.
So tv4 stands for Tiny Validator four.
That's pretty much it.
It's intended to take some data, a JSON Schema,
and validate the data.
That's it.
That's all this library does.
So we'll let that install kick in.
And while it does,
I'm gonna show you a very quick-tool
that you can use to generate JSON Schema
for your own projects.
So I'm gonna open up a new browser tab,
and go to jsonschema.net.
At jsonschema.net,
we enter in some amount of JSON
over here on the left-hand side.
And that generates the schema file
that can be used to validate
the structure and values inside that JSON data.
So you can see that in this case,
they've got an object with a property of "checked,"
and it's a boolean value.
So if we now look inside this schema document over here,
you'll see that it's a type of "object,"
it has properties of "checked,"
and type must be a "boolean."
You'll see a bunch of other properties
inside of here as well.
So the tool that you're looking at is gonna try to generate
a very well fleshed out JSON Schema document.
We don't always have to put all the properties
that you see in here inside of our JSON file or JSON Schema,
a lot of these are optional.
For example, like title and description.
Title and description are meant to annotate
the purpose of this like "checked" property
that it's defining.
So all we have to do is enter in
our JSON data here on the left-hand side.
Now, a quick thing to be really aware of,
when we wrote out the Redux State right here,
and as we usually think of our Redux State,
usually it's in a JavaScript object format,
which is a little bit different than JSON.
So when we convert our Redux State to JSON,
all we really have to do is make sure that we wrap
all of our keys with double quotes.
And we don't use any single quotes,
we always use double quotes to represent strings as well.
So I'm gonna first reset this thing,
or maybe just delete everything inside of it.
And now I'm going to enter in our current JSON Schema,
or our current JSON structure.
So we've got a key of "comments,"
and notice I'm using the double quotes there.
It's gonna be an array,
and it has values like comment number one,
and comment number two.
Then we also have that "auth" piece of state,
and a sample value for it might be true.
So all we have to do over here is enter in some sample data,
and this tool will generate our schema for us.
So now I'll submit this.
And this is our schema that we can use
to validate the structure of our Redux store.
So we have a root type of "object,"
like that's what we're working with here, it's an object.
It has property of "comments,"
which has to be an "array."
And every item within that array must be of type "string."
So if we start to enter in an integer to this thing,
or an object, or an array, or anything like that,
the JSON Schema is gonna fail our validation.
You can also see that we have our "auth" property
down here as well,
so it must be of type "boolean."
By default it's false,
but the default in our case, not really super applicable.
Okay, so we've now generated our JSON Schema,
which means that we're now ready to start working
on the actual middleware itself and do this validation.
Now, I'll be honest with you,
the actual code that we're gonna write
is gonna be really straightforward.
So a lot of the work around this middleware
is just understanding exactly
what this JSON Schema stuff is.
So in the next section,
we're gonna start to put together the middleware itself,
and the code for it is gonna come together awfully quickly.
So a quick break, and I'll see you in just a second.