- [Instructor] The easiest way to validate JSON data
against the JSON Schema is to use a validator
written for that purpose.
Free open source validators exist for a number of languages,
including Java, Python, Ruby and PHP,
as well as browser-based utilities.
For the H+ Sport website,
I'll use a JavaScript validator called Ajv.
Ajv is designed for backend use,
but I can try the tool out without installing it
by using an online instance called a REPL.
The left pane shows the JavaScript code
that the validator uses.
I only need to work with two parts of the code.
The schema variable and the test function calls.
I'll open the schema.js file for this video.
And I want to copy the schema content to the clipboard.
Now, I only want the object itself
and not the variable declaration or the equal sign.
So, I'll click the beginning,
click to the end, but not include the semi-colon,
and copy that.
And then over in the REPL,
I want to delete the existing value for schema
and paste in my schema.
Now, that I've told the REPL
what schema I want to validate against,
I need to give it some sample data to test.
To do that, down here on line 34,
I can call the test method and specify the value
to test against between parentheses.
So, I'm going to start by deleting the existing values
between parentheses for the first function call.
And I'll just delete the second function call
altogether for right now,
then I'll go to my data.js file,
and I'll copy the value for the rawProductData variable.
And this is a JSON string containing an array.
So, I want everything from the opening quote
to the closing quote,
but not that closing semi-colon,
and not the variable name and equal sign.
Copy that to the clipboard.
And then over in the REPL,
I'll paste that between the parens
after the test function call.
And now I'm ready to test,
so I'm going to scroll down all the way to the bottom
and select the Run button.
So, this is telling me my data has to be an array
and it looks like my error here is that
I included the quotes
when I wanted to just give it the data instead.
So, now I've just pasted in an array
rather than a string containing an array,
and I'll go ahead and click Run again.
And this time I get the message valid.
So, my data is valid according to the schema I created,
which is exactly what I expect.
Now, what happens if there are errors in my data?
I would expect that to be invalid and I can test that out.
So, I'll go back to my data, I'll introduce a couple errors,
and I'll make sure that those get logged as I expect.
So, in the code for the first product,
Raspberry Mineral Water,
I'm going to replace the description value,
which my schema specifies has to be a string,
and I'm going to replace that with a number.
And for that to be a numerical value,
I have to take the quotes off.
Remember that only strings are quoted in JSON.
And then here for the second product,
the Strawberry Mineral Water,
I'm going to take the quotes off of the ID value.
So, my schema is expecting a string value for the ID,
but I'm going to throw a number at it and see what happens.
So, I'll scroll down and I'll click Run again.
And this time I get the message invalid
followed by a couple specific messages.
And the validators pointing out the errors
that I introduced exactly.
Description must be string, ID must be string.
A validator is really helpful
in making sure your schema matches the data
that you want to work with.
And it can also flag errors in data coming in
before you try to work with the data.