In the last lecture, we added a model and we used it for validating incoming requests bodies.
Now I just want to highlight it again even though I said it before, this is optional,
the whole idea of using models is optional and of course, you don't have to do validation of incoming
bodies or you could also validate headers and so on here, you could implement the same logic in lambda.
So if you pass data from your request body to lambda,
since you use a normal programming language like Javascript in lambda, you could of course also do the
validation lambda,
you could simply validate there if the requests body holds an age property
and if that age property holds a valid integer, that could be done there.
The idea of using a model for that and using this built-in validator API Gateway offers you simply
is to give you an easier way of adding such implementation.
You get an out-of-the-box working implementation of validation which returns an error if it fails and
that can be very convenient
but again, it's optional.
Now validation of incoming requests is not everything you can do with models.
You can also use models to map data or you can use them in general in this mapping process I should say,
so how does this work?
Let's go back to our main page with all these boxes and let's go back to the integration request box
which is all about transforming data and forwarding it to the action, to the lambda function here
in the end. There if you have a look at our current body mapping template, we see that right now, we're still
accessing a person data object on the request body and there on that person data object, we tried
to access an age property.
Now clearly, we don't have that because we have a model in place which makes sure that since we use validation,
only an object with age, height and income reaches our back-end you could say. Therefore there is no chance
of a person data property being valid or being there on this object,
so hence this current type of data extraction doesn't really make a whole lot of sense.
So what we can do instead is on this body mapping template, we can simply extract age like that,
right because we know on that request body, there will be an age property because that is what we're
validating for.
Now if we had a more complex model and we're not really interested in all the data we get, we don't want
to pass all that data which is part of our model to lambda,
we could also use a different way of generating this template. Here on this drop-down next to generate
template,
you see you can select from all the models you have set up.
There are the two default models API Gateway gives you out-of-the-box and there's our compare data model.
Now if we click this, we see that we get this template generated for us and what's happening there is
that in the first line with this set call, we simply set our own variable which we can use inside of
this template,
the inputRoot variable, this is a variable which you could of course also rename,
that's no reserved name and what it does here is it simply extracts the request
body with this path method here.
Before we used $input.json ('$'),
well now we're using path in the end here,
this will give us the same,
it will give us access to our request body. So inputRoot is our whole request body and therefore, we
could of course map a custom age variable to $inputRoot.age and maybe omit the other two if we don't want
to pass them to lambda.
So now we will make sure that lambda only gets an age property which holds, well the value of our actual
age property we have on the request body.
And of course, this is kind of a constructed example,
there are also more useful cases where you again, have more structured, more complex data reaching your
back-end and you want to extract the data and then again, you can conveniently map that against your
model with this little helper which does some set up for you.
Now if we save this, we should see that if we test it again, so we go back to test on the client here
and we submit data, so we have our age field, 28,
then we have our height
field, 72, and keep in mind, you need all these fields because we still have validation in place,
so even though we don't forward them all to lambda, we still need them all, income could be 3000,
whatever
and now hit test and you should see that your age which we're getting back is again 56.
So it seems to work that we pass age to lambda, even though we cut the two fields, income and height
we're not interested in. Now just as we can use this little helper of creating a mapping template for integration
request, we can do the same for integration response.
So let's go there and let's open the body mapping template of our example
response here. If we have a look at it, right now we're returning this your age property where we simply
output whatever lambda returns to us.
Now we can of course also generate a template here based on our compare data and it does the same
as before,
it accesses whatever
in this case lambda returns to us with $input.path ('$') and maps it to our inputRoot
variable.
Now we can use this inputRoot variable here and we could therefore again create a your age property and
simply set this equal to inputRoot,
this time not .age because inputRoot
keep in mind is here whatever lambda returns to us
and if you have a look at our function, you of course see that lambda still only returns the age times two,
so it returns a number.
So with that, we can also cut the other two properties here,
we could also set them to null or to some other default values if we'd like to and hit save here.
And with that, we should see the same behavior as before but now again using this generate template helper
and again, this is super useful if you've got a more complex model and want to quickly generate a template
which incorporates it.
This is something I want to show you too, again just as everything else connected to models,
this is optional,
you don't have to use models but if you want to use them, they offer some nice benefits, validation, this
easy mapping here.
So I'd say it's time to practice them in the next assignment.