1
00:00:02,360 --> 00:00:07,130
Over the last lectures, we learned a lot about body mapping templates and how to control what's passed into

2
00:00:07,130 --> 00:00:09,200
lambda and we get out of lambda,

3
00:00:09,200 --> 00:00:10,580
that's nice.

4
00:00:10,580 --> 00:00:14,540
When we talk about controlling data, there's one other concept I

5
00:00:14,570 --> 00:00:18,690
briefly touched on before and that's models.

6
00:00:19,100 --> 00:00:22,010
Let's have a look at our course project.

7
00:00:22,010 --> 00:00:28,160
If we have a look at it here, we see that we got age, height and income as data

8
00:00:28,160 --> 00:00:35,390
we want to store in our database finally. So we can create a compare data model which defines that the data

9
00:00:35,390 --> 00:00:37,630
we want to work with in our application

10
00:00:37,670 --> 00:00:42,190
shall just have these three properties.

11
00:00:42,380 --> 00:00:46,030
So let's do that, we can do that with a model.

12
00:00:46,100 --> 00:00:51,440
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

13
00:00:51,440 --> 00:00:55,490
data seems fitting, I'll define for which kind of data this is applicable,

14
00:00:55,580 --> 00:01:02,180
JSON data and give it a description, data we use for comparing users.

15
00:01:02,180 --> 00:01:07,670
Now the model schema is provided by me in a file attached to this video and I just copy it in. This

16
00:01:07,750 --> 00:01:10,280
uses the JSON schema language,

17
00:01:10,280 --> 00:01:14,320
you can find an article with links to that after this lecture,

18
00:01:14,330 --> 00:01:16,940
I won't dive detailed in it but it's not that difficult.

19
00:01:17,060 --> 00:01:19,950
As you can see, what we define here is the schema version,

20
00:01:19,970 --> 00:01:25,820
the title of this object compare data and that most importantly, that it is an object, that we have

21
00:01:25,910 --> 00:01:31,790
three path properties - age, height and income which are all of type integer and that all three properties

22
00:01:31,850 --> 00:01:33,400
are required.

23
00:01:33,440 --> 00:01:40,130
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

24
00:01:40,130 --> 00:01:47,360
it is on our post method in our gatekeeper to validate incoming requests whether they are fulfilling

25
00:01:47,360 --> 00:01:48,840
our schema or not.

26
00:01:49,190 --> 00:01:53,380
So let's click on method request and here, under request body,

27
00:01:53,480 --> 00:02:01,730
we can register our model. So for data which has the JSON format, I'll choose compare data, our brand new

28
00:02:01,730 --> 00:02:03,680
model and confirm

29
00:02:03,930 --> 00:02:11,060
and with that, we can now go to request validator and enable validate body, so that incoming requests are

30
00:02:11,060 --> 00:02:11,810
validated

31
00:02:11,840 --> 00:02:15,440
whether the request body fits our model or not.

32
00:02:15,770 --> 00:02:24,260
If we now go to test and set up a request body and we have age which is a number, 28 and with height

33
00:02:24,430 --> 00:02:32,600
in inches let's say which is 72 and we hit test, we get an error here, invalid request body with status

34
00:02:32,600 --> 00:02:37,180
code 400 because the request is blocked, which makes sense.

35
00:02:37,340 --> 00:02:46,760
Now I can of course add my other missing property income, let's say dollar, monthly, 2500,

36
00:02:46,760 --> 00:02:48,680
whatever you like

37
00:02:48,680 --> 00:02:50,780
and let's hit test again

38
00:02:50,780 --> 00:02:54,800
and now you see we get back a valid response.

39
00:02:54,800 --> 00:03:00,560
The age is wrong because we still extract the data invalidly but we make it through the gatekeeper because

40
00:03:00,560 --> 00:03:06,590
the request body fits our validation and that of course makes a whole lot of sense if we want to have a clear

41
00:03:06,590 --> 00:03:07,480
API.

42
00:03:07,640 --> 00:03:12,400
We are pretty clear about what is allowed and everything else is blocked.
