- [Instructor] I want to create a schema
to describe the product data available
from the H+ Sport API.
To start, I want to look at the data return
for a sample request.
So, I'm looking at data.js.
Now, this is the data I would normally get
from an HTTP request to the API.
But for the purpose of creating a schema,
I've stored the data from a request in my file.
Right now, this is JSON.
It's a string and the editor won't display it for me
with line breaks and indents.
But I can open product study HTML,
start up my live server and look at it in the console.
So, I'll go ahead and open that HTML file.
Start up my HTTP server and then opening up the console.
My project includes JavaScript code
that parses the JSON strings into JavaScript objects
and then logs these to the console.
And now, I can examine the structure of this data.
It's an array and it contains six objects.
And each object contains the same five properties.
I'm going to build my schema in the schema.js file.
A schema is written in JSON, but it's easier to work with it
by writing it as an object
within the language I'm working in, which is JavaScript.
But I'm going to follow the rules of JSON
to make conversion easier.
So, we'll start by creating a variable.
And so, I'll just use VAR and call this product schema.
And the overall structure of this is going to use opening
and closing braces.
The content of the schema is simply a set of keys
and values, so a JavaScript object.
By convention, I start with the dollar schema keyword
as the first key.
And this makes it clear that this object is a JSON schema,
rather than just generic JSON.
Remember that JSON requires key names as well as values
to be double quoted.
So, I have to put double quotes around this.
And I'll follow that with a colon and then the value.
And this is a special value that's defined
in the JSON schema specification.
And it's a URI.
That's http://json-schema.org/draft-07/schema#.
And this value indicates
that this schema implements draft seven of JSON schema.
And I'll end this line with a comma.
And on the next line,
I also want to include the dollar ID property.
This lets me specify a unique identifier for this schema.
By convention, this is in the form of URI as well,
but it doesn't need to reference an existing resource.
Generally, you want to use a domain you control
and specify a unique path.
I'm creating a schema for the products displayed
on the H+ Sport website.
So, I'll use http://hplussport.com/schemas/product.json.
This isn't a valid URL at this point and that's fine.
As long as it's a domain I control,
it provides a unique reference for the schema.
And if in the future I choose to,
I can publish the completed schema at this address.
JSON schema also supports a couple additional properties
that are optional, but recommended.
You use the title property to provide a short summary
of the purpose of the schema.
So, I'll specify a value of eight plus sport products.
And then, you use the description property
to give a more detailed idea of the schema's use.
So description and I'll say schema
for H+ Sport product data.
Looking at my data in data.js,
I can see that at the top level,
it has an array as its value.
In my schema, I indicate the data type
of the top level JSON content with the type property.
So in this case, I'm going to say type
and that's going to be a value of array.
Then for array data, I specify an item's property.
And the value of items is going to be an object.
And it's going to specify what kind of data should be found
within the top level array.
So, the items within that array are objects.
And so, I want to use another type keyword.
So, the type of data within here is object.
So then, I can specify the properties
of each of those objects using the properties keyword.
And properties is also going to be an object
and within properties, I can list each property name
that is to say each key name that occurs
within these objects and specify its data type.
I can also give a lot of other information
about each one of those properties,
but for now, I'm just going to specify the data type.
So, my first property is ID and I specify its value
as an object, but I only have one key value
within that object and that's type and string.
And then, comma after that object value.
And then, I just need to go through all the rest
of my objects.
And so, I can switch back to my console and just look here.
I have ID, which I started with.
There's also description, image, image title and name
and I can do these in any order.
The order doesn't matter within an object.
So, I'll just go with the name next
and that's going to be a of string.
And all the rest of these are going to be string types.
So, I can really just copy and paste the code
that I've just typed and change the key name.
So, we need a description.
I need an image_title.
And then I need an image.
All five of those are strings,
but if there were other data types like number,
I could specify those here as well.
And then, I need to be sure because this is JavaScript,
I want to end my statement with a semi-colon.
Remember that JSON schema code is itself, JSON.
I've created this as JavaScript.
My editor is able to recognize this as a JavaScript object
and verify that the syntax matches what it should match
for a JavaScript object.
But again, it's important to recognize
that this doesn't ensure that the schema I've written
provides the validation I expect.
That requires testing it against JSON that I know is good
and verifying that each good JSON string passes validation
with this schema.
I also want to create some JSON that has specific errors
and test those against the schema
and verify that those errors are flagged.