Contract testing is a powerful concept.
In contract testing we're verifying that
an API does what a contract says that it
will do. This contract could be something
like documentation, but it's usually
defined with a schema. So in this video
we're going to look at how we can use
tv4 inside of Postman to validate some
API schemas. And a schema is just a way
of defining the rules at different parts
of the API say they will conform to, so
it's a way of defining a contract. So for
example you could have a field that
comes back from the API, and the schema
might say this field must be a string. If
the API was to return another data type
for that field, say an integer it would
be violating the contract that has been
set up in the schema. Schemas are fairly
common in APIs as they allow you to
better know and understand what exactly
an API should be doing. Now this might
all be confusing, so let's take a look at
it in practice. I have here a call to the
Star Wars API set up, and this API
actually publicly provides us with its
schema. So let's take a look at it, we'll
get rid of this and instead we'll put
schema in here, and we'll send that
request, and that gives us back the
schema, so it shows us what fields are
required for this API, and then it shows
what each of these fields should have
what properties they should have. So the
name field should come back as a string.
So let's actually just copy this, and
let's go over to tests here and we'll
create a variable, we'll call it SW
schema, and we'll paste in the schema
that we got back from the API. Now
Postman gives us a way to validate
against a schema using library, it's a
JavaScript validation library called tv4.
So let's create a test that uses that, so
PM test and let's call it schema
validation, and here give it a function
and then inside of our test here let's
create a constant called validation
results. And for this we will call tv4
dot balla date result, and inside of here
we're
take the response dot JSON, and we're
going to validate that against this
schema that we made, so SW schema the one
that we've defined up here. And then
we'll also say false and true to fill
out the other arguments. So this is just
saying whether we want to fail if there
is parameters that we don't expect in
there. And we do so we'll put that in as
true. And then what we expect out of this,
so PM dot expect, we expect this
validation result here to the valid to
come back as valid, so validation result
valid should be dot to be true. So we
expect it to come back as true. And then
let's put a closing bracket here. N,ow
we'll change this so instead of schema
let's look at people slash one and let's
send that request off. And you can see
that the test results the schema
validation has passed. So it looked at
this response, it looked at each of the
fields in this response, it said okay
those are all all the required fields
are there and they all come back with
the correct data type. So now what
happens if we change something, so I
don't know let's look at skin color here,
and instead of saying we expect it to be
a string let's tell it that we expect it
to be an int. So let's do that and let's
send the request. You can see that now it
has failed with a validation error. So
what we got back in our body for skin
color was a string, but the schema was
saying it should be an int, and that is
causing a mismatch so it's failing. So
let's just change this back to string
and everything should be fine again. So
this is a way of defining a contract, the
schema is the contract this is what this
API should do, things that should always
be true in this API. And we can use this
tv4 library in Postman to very quickly
see that our API is indeed sending all
the correct structure and data back that
we need. So this is a very powerful
validation tool, it's a very powerful
concept with contract testing.
Something that is very useful to us.
We're coming to the end of our time
together in this course. And so in the
next video we'll wrap things up and pull
everything together in a conclusion.