Instructor: In the last section we spoke about
the purpose of the next middleware
we're going to work on.
So, the idea is that we wanna make sure
that we do not accidentally mutate our state
in some unpredictable fashion.
Every single time we dispatch an action
and our reducers calculate a new value for state,
we're going to validate the structure of our Redux store.
So if we have an object
inside of our comments list right here,
well, we should probably throw an air message
or a warning of some sort and say,
hey, something just went wrong.
So one of the chief concerns around doing this
is going to be how we actually validate our state.
We're not gonna write out code
that's going to manually look at every value inside of here
and say like, hey, are you a string,
are you a string and so on,
instead, we're going to to be making use
of an automated validation framework
that's built upon something called JSON Schema.
So in this section,
we're gonna very quickly talk about what JSON Schema is
and how we can use it to validate our Redux store.
I'm gonna get started by opening up a new browser tab
and navigating to json-schema.org.
JSON Schema is a standard for validating JSON documents.
And so if we think of all the data
inside of our Redux store as being just JSON data
and that's really what it is,
it's just a collection of objects,
strings, integers, arrays, and so on,
then we can use JSON Schema
to validate all the data inside of our Redux store
very quickly and really quite easily.
To give you a better idea
of how JSON Schema works in general
what we do is we first form up
something called a schema document
and it's gonna look something like what you see down here.
So we would refer to this as our JSON Schema file.
It's gonna be a file that contains a definition
of the overall structure of what our Redux store should be
or whatever our arbitrary random JSON data might be.
We'll see an example of a JSON Schema file in just a second.
Once we create this file,
we can then make use of a library
that will install in just a moment
to take some actual data like our Redux state up here.
So this is an actual pile of data inside of our application
and we'll validate this data against our JSON Schema file.
So again, we've got kinda like
two or three moving pieces here.
We've got the JSON Schema file
that describes the structure of our data.
We've got our actual Redux state,
which is the data we wanna validate,
and then we've got a library that we're going to use
to essentially compare the two against each other
and determine whether or not
our Redux state is valid.
Let's very quickly look at an example of a JSON Schema file.
So back over on this page,
I'll go to examples
and that I'll pull up a very quick example
of what a JSON Schema might look like.
So inside this example right here,
every property that you see inside the object
has some meaning inside of the JSON Schema standard.
So the property that you see right here
of say type indicates that the root object
or the root thing that we're looking at right here
should be of type object.
And that object should have
this list of properties assigned to it.
It should have a first name property that is a string,
it should have a last name that is a string
and it should have an age that is an integer
and that integer must be at least the value zero,
so zero or greater.
Out of all three of these properties right here
only the first name and the last name properties
are required to be defined.
So we can kind of imagine that a valid person object
might have a structure that looks a little bit like this.
So, it might be an object
that has a first name property that is a string.
So I'll say, I don't know a person's name, Alex
and a last name property,
which would also be a string of,
I don't know, what's the last name, Smith.
And then it must also have an age
that is an integer in at least zero.
So maybe 10 like so.
So this over here on the left hand side
is our JSON Schema file,
this over here is the actual data.
We use the schema file to validate the data.
What I put down for our data over here
on the right hand side is currently valid,
but if I say made my age less than zero,
so like negative 10
then it would now be invalid
because we had said that age must be an integer
with a minimum of zero.
In addition, if I removed, say the last name property
now my object over here is no longer valid as well
because we had said that last name
is a required property on this object.
Okay, so that's just a very quick example
of what a schema file looks like.
Now, it might look a little bit scary right here,
you have to memorize all these kind of property thing
and these types and minimum
but there's actually a tool
that I will show you very shortly
that makes generating JSON Schema very very straightforward.
So let's take a quick pause right here
now that we have a better idea of what we're doing
and we'll start implementing our middleware
in the next section.