Over the last lectures, we learned a lot about body mapping templates and how to control what's passed into
lambda and we get out of lambda,
that's nice.
When we talk about controlling data, there's one other concept I
briefly touched on before and that's models.
Let's have a look at our course project.
If we have a look at it here, we see that we got age, height and income as data
we want to store in our database finally. So we can create a compare data model which defines that the data
we want to work with in our application
shall just have these three properties.
So let's do that, we can do that with a model.
So I'm in the model section and I'll click create to create a new model and I'll give it any name, compare
data seems fitting, I'll define for which kind of data this is applicable,
JSON data and give it a description, data we use for comparing users.
Now the model schema is provided by me in a file attached to this video and I just copy it in. This
uses the JSON schema language,
you can find an article with links to that after this lecture,
I won't dive detailed in it but it's not that difficult.
As you can see, what we define here is the schema version,
the title of this object compare data and that most importantly, that it is an object, that we have
three path properties - age, height and income which are all of type integer and that all three properties
are required.
With that, I can click create model and now we get our own model which we can use and a good place to start using
it is on our post method in our gatekeeper to validate incoming requests whether they are fulfilling
our schema or not.
So let's click on method request and here, under request body,
we can register our model. So for data which has the JSON format, I'll choose compare data, our brand new
model and confirm
and with that, we can now go to request validator and enable validate body, so that incoming requests are
validated
whether the request body fits our model or not.
If we now go to test and set up a request body and we have age which is a number, 28 and with height
in inches let's say which is 72 and we hit test, we get an error here, invalid request body with status
code 400 because the request is blocked, which makes sense.
Now I can of course add my other missing property income, let's say dollar, monthly, 2500,
whatever you like
and let's hit test again
and now you see we get back a valid response.
The age is wrong because we still extract the data invalidly but we make it through the gatekeeper because
the request body fits our validation and that of course makes a whole lot of sense if we want to have a clear
API.
We are pretty clear about what is allowed and everything else is blocked.