1
00:00:02,290 --> 00:00:07,960
In the last lecture, we added a model and we used it for validating incoming requests bodies.

2
00:00:07,970 --> 00:00:12,800
Now I just want to highlight it again even though I said it before, this is optional,

3
00:00:12,830 --> 00:00:19,370
the whole idea of using models is optional and of course, you don't have to do validation of incoming

4
00:00:19,370 --> 00:00:25,550
bodies or you could also validate headers and so on here, you could implement the same logic in lambda.

5
00:00:25,580 --> 00:00:29,640
So if you pass data from your request body to lambda,

6
00:00:29,750 --> 00:00:35,120
since you use a normal programming language like Javascript in lambda, you could of course also do the

7
00:00:35,120 --> 00:00:36,660
validation lambda,

8
00:00:36,770 --> 00:00:42,640
you could simply validate there if the requests body holds an age property

9
00:00:42,660 --> 00:00:47,200
and if that age property holds a valid integer, that could be done there.

10
00:00:47,210 --> 00:00:53,780
The idea of using a model for that and using this built-in validator API Gateway offers you simply

11
00:00:53,780 --> 00:00:56,940
is to give you an easier way of adding such implementation.

12
00:00:56,960 --> 00:01:03,770
You get an out-of-the-box working implementation of validation which returns an error if it fails and

13
00:01:03,770 --> 00:01:05,210
that can be very convenient

14
00:01:05,210 --> 00:01:07,080
but again, it's optional.

15
00:01:07,220 --> 00:01:12,090
Now validation of incoming requests is not everything you can do with models.

16
00:01:12,230 --> 00:01:18,870
You can also use models to map data or you can use them in general in this mapping process I should say,

17
00:01:18,890 --> 00:01:20,600
so how does this work?

18
00:01:20,630 --> 00:01:26,060
Let's go back to our main page with all these boxes and let's go back to the integration request box

19
00:01:26,060 --> 00:01:30,880
which is all about transforming data and forwarding it to the action, to the lambda function here

20
00:01:30,890 --> 00:01:37,340
in the end. There if you have a look at our current body mapping template, we see that right now, we're still

21
00:01:37,400 --> 00:01:44,930
accessing a person data object on the request body and there on that person data object, we tried

22
00:01:44,930 --> 00:01:47,300
to access an age property.

23
00:01:47,300 --> 00:01:54,860
Now clearly, we don't have that because we have a model in place which makes sure that since we use validation,

24
00:01:55,190 --> 00:02:02,750
only an object with age, height and income reaches our back-end you could say. Therefore there is no chance

25
00:02:02,750 --> 00:02:08,280
of a person data property being valid or being there on this object,

26
00:02:08,330 --> 00:02:14,100
so hence this current type of data extraction doesn't really make a whole lot of sense.

27
00:02:14,180 --> 00:02:20,840
So what we can do instead is on this body mapping template, we can simply extract age like that,

28
00:02:20,840 --> 00:02:27,560
right because we know on that request body, there will be an age property because that is what we're

29
00:02:27,710 --> 00:02:29,340
validating for.

30
00:02:29,510 --> 00:02:36,560
Now if we had a more complex model and we're not really interested in all the data we get, we don't want

31
00:02:36,560 --> 00:02:40,870
to pass all that data which is part of our model to lambda,

32
00:02:41,060 --> 00:02:46,600
we could also use a different way of generating this template. Here on this drop-down next to generate

33
00:02:46,600 --> 00:02:47,620
template,

34
00:02:47,750 --> 00:02:51,380
you see you can select from all the models you have set up.

35
00:02:51,380 --> 00:02:57,440
There are the two default models API Gateway gives you out-of-the-box and there's our compare data model.

36
00:02:57,440 --> 00:03:03,770
Now if we click this, we see that we get this template generated for us and what's happening there is

37
00:03:03,770 --> 00:03:11,210
that in the first line with this set call, we simply set our own variable which we can use inside of

38
00:03:11,210 --> 00:03:12,170
this template,

39
00:03:12,170 --> 00:03:17,490
the inputRoot variable, this is a variable which you could of course also rename,

40
00:03:17,490 --> 00:03:22,770
that's no reserved name and what it does here is it simply extracts the request

41
00:03:22,770 --> 00:03:25,540
body with this path method here.

42
00:03:25,760 --> 00:03:30,240
Before we used $input.json ('$'),

43
00:03:30,320 --> 00:03:32,360
well now we're using path in the end here,

44
00:03:32,360 --> 00:03:33,730
this will give us the same,

45
00:03:33,770 --> 00:03:41,030
it will give us access to our request body. So inputRoot is our whole request body and therefore, we

46
00:03:41,030 --> 00:03:48,320
could of course map a custom age variable to $inputRoot.age and maybe omit the other two if we don't want

47
00:03:48,320 --> 00:03:49,830
to pass them to lambda.

48
00:03:50,180 --> 00:03:56,030
So now we will make sure that lambda only gets an age property which holds, well the value of our actual

49
00:03:56,090 --> 00:03:59,000
age property we have on the request body.

50
00:03:59,240 --> 00:04:01,630
And of course, this is kind of a constructed example,

51
00:04:01,640 --> 00:04:08,750
there are also more useful cases where you again, have more structured, more complex data reaching your

52
00:04:08,750 --> 00:04:13,760
back-end and you want to extract the data and then again, you can conveniently map that against your

53
00:04:13,760 --> 00:04:17,420
model with this little helper which does some set up for you.

54
00:04:17,420 --> 00:04:24,500
Now if we save this, we should see that if we test it again, so we go back to test on the client here

55
00:04:25,010 --> 00:04:30,670
and we submit data, so we have our age field, 28,

56
00:04:30,800 --> 00:04:32,190
then we have our height

57
00:04:32,210 --> 00:04:37,610
field, 72, and keep in mind, you need all these fields because we still have validation in place,

58
00:04:37,640 --> 00:04:43,400
so even though we don't forward them all to lambda, we still need them all, income could be 3000,

59
00:04:43,400 --> 00:04:44,300
whatever

60
00:04:44,300 --> 00:04:50,580
and now hit test and you should see that your age which we're getting back is again 56.

61
00:04:50,660 --> 00:04:57,300
So it seems to work that we pass age to lambda, even though we cut the two fields, income and height

62
00:04:57,320 --> 00:05:04,870
we're not interested in. Now just as we can use this little helper of creating a mapping template for integration

63
00:05:04,870 --> 00:05:08,070
request, we can do the same for integration response.

64
00:05:08,080 --> 00:05:12,320
So let's go there and let's open the body mapping template of our example

65
00:05:12,340 --> 00:05:18,880
response here. If we have a look at it, right now we're returning this your age property where we simply

66
00:05:18,910 --> 00:05:21,640
output whatever lambda returns to us.

67
00:05:21,670 --> 00:05:27,130
Now we can of course also generate a template here based on our compare data and it does the same

68
00:05:27,130 --> 00:05:27,890
as before,

69
00:05:27,940 --> 00:05:29,520
it accesses whatever

70
00:05:29,520 --> 00:05:35,860
in this case lambda returns to us with $input.path ('$') and maps it to our inputRoot

71
00:05:35,860 --> 00:05:36,790
variable.

72
00:05:36,790 --> 00:05:43,690
Now we can use this inputRoot variable here and we could therefore again create a your age property and

73
00:05:43,690 --> 00:05:46,260
simply set this equal to inputRoot,

74
00:05:46,330 --> 00:05:49,330
this time not .age because inputRoot

75
00:05:49,330 --> 00:05:52,710
keep in mind is here whatever lambda returns to us

76
00:05:52,720 --> 00:05:59,230
and if you have a look at our function, you of course see that lambda still only returns the age times two,

77
00:05:59,290 --> 00:06:00,810
so it returns a number.

78
00:06:01,150 --> 00:06:04,880
So with that, we can also cut the other two properties here,

79
00:06:04,900 --> 00:06:11,170
we could also set them to null or to some other default values if we'd like to and hit save here.

80
00:06:11,180 --> 00:06:16,750
And with that, we should see the same behavior as before but now again using this generate template helper

81
00:06:16,750 --> 00:06:21,700
and again, this is super useful if you've got a more complex model and want to quickly generate a template

82
00:06:21,730 --> 00:06:23,240
which incorporates it.

83
00:06:23,270 --> 00:06:28,670
This is something I want to show you too, again just as everything else connected to models,

84
00:06:28,680 --> 00:06:30,000
this is optional,

85
00:06:30,010 --> 00:06:35,700
you don't have to use models but if you want to use them, they offer some nice benefits, validation, this

86
00:06:35,710 --> 00:06:36,900
easy mapping here.

87
00:06:37,030 --> 00:06:40,330
So I'd say it's time to practice them in the next assignment.
