- [Instructor] In addition to specifying data types
for the properties of JSON data,
JSON schema also allows you to specify that one
or more properties are required.
At this point, my schema specifies a data structure
for an array that contains objects
and each object contains five properties,
ID, name, description, image title and image.
What if I want to require that these properties
must be included in order for my JSON data
to be considered valid?
Well, remember that each sub object within the array
is itself specified by an object in JSON schema
with a type property set to object
and a property's property specifying the names of properties
it can contain.
To indicate that one or more of these properties
are required, I simply add another property to the object
in my JSON schema.
The property must have the name required
and its value is an array containing one or more strings
as its elements.
Each string in the array must correspond
to a property defined in the properties object.
In my schema.js file, for my items property,
I already have a type value of object.
And a property's value containing the properties to include
with their data types.
To specify that all of these properties are required,
I add another property after that data structure.
because I've got so many nested braces here.
I'm going to click the curly brace after the properties key
and that's got a box around it in my editor
is going to highlight the closing brace for that,
which is down here on line 26.
So, I'm going to click at the end of line 26.
I'm going to enter a comma.
And then I can enter another key value pair
that's on the same level as the properties key.
And the name of this is going to be required
and I'm going to set the value of the required property
to an array.
And in that array, I'm going to add five elements,
which are strings containing the names
of the required properties.
And these can be in any order.
So, ID...
Name...
Description...
Image_title...
And image.
This code specifies that some value is required
for each of these five properties within each object
for each product.
Now, I want to test this.
So, I'll save my changes to the schema
and I'll copy the value of the product schema variable
without the quotes.
So, starting at the curly brace after the equal sign.
And at the very end, before the semi-colon.
And then I want to open up the validator I'm using,
which is AJV and I need to swap out my new scheme
of value for the scheme of variable.
So, we'll select that scheme of variable
and paste mine in its place.
Then back in my editor, I'm going to open up the data.js file,
which contains my data.
And I want to copy the value of the raw product data variable
without the quotes.
So, after that opening apostrophe
and all the way down to the end of the line,
excluding that closing apostrophe in semi-colon.
And I'm going to copy that to clipboard.
And then over in AJV, I'm going to scroll down
to lines 41 and 42.
I'm going to delete that second test function call on line 42.
There we go.
And within the parentheses for the test function call
on line 41, I want to select the contents.
And then, I want to paste in what I just copied.
And now, I can scroll down and hit run.
And after a moment, I get the message valid.
All of my required properties are included
throughout the data.
Now, all of my data currently contains all five properties,
but I can test my schema
by deleting a few properties here and there.
So, for my first item, I'll delete the image key
and its value.
I seem to have some scrolling behavior going on
when I click in here, so I'm just working around
that the best I can.
So, there's the image key and value and the comma after it.
And then, for the next property,
I'll just go ahead and delete the ID key and value
and the comma after it.
And I'll scroll down.
And I'll click run.
And I get an error.
So, it says invalid and it has two messages
about specific elements of the array that are missing.
And in particular, that there's a required property image
and a required property ID,
which are exactly the two I deleted.
So, I'm really happy to see this error message
because it means the schema is catching potential issues
that are important to me.
And now, in addition to catching potential issues
with incorrect data structure,
extra data and incorrect data types,
my schema also catches missing data.