1
00:00:00,180 --> 00:00:04,770
All right, and once we've got that out of the way, next, I want to talk about validation.

2
00:00:05,490 --> 00:00:12,470
You say even though we only accept the properties that are specified in the schema, our current setup

3
00:00:12,660 --> 00:00:14,450
has one big doozy.

4
00:00:14,940 --> 00:00:16,490
There is no validation.

5
00:00:17,010 --> 00:00:20,100
So essentially we can pass in empty.

6
00:00:20,940 --> 00:00:22,020
How is that going to look like?

7
00:00:22,050 --> 00:00:24,930
Well, again, we can go back through the postman.

8
00:00:25,440 --> 00:00:33,310
And in first example, I'll just remove completed and I'll pass in the name as an empty string.

9
00:00:33,480 --> 00:00:41,190
Now I do need to fix this comma here, but you'll see that actually we're successful so I can create

10
00:00:41,460 --> 00:00:42,260
items this way.

11
00:00:42,720 --> 00:00:49,460
And what's even more interesting is that I can send an empty object and I'll still be successful.

12
00:00:50,010 --> 00:00:53,640
And you have to agree that actually this is not the best setup.

13
00:00:53,940 --> 00:00:54,350
Correct.

14
00:00:54,660 --> 00:00:58,180
So that way we can just set up a bunch of empty items.

15
00:00:58,560 --> 00:00:59,820
Now, what can we do about it?

16
00:01:00,030 --> 00:01:04,250
Well, we can give validation to our schema.

17
00:01:04,590 --> 00:01:10,140
Now, before we continue, though, let me just say that when it comes to validation, since it's a

18
00:01:10,140 --> 00:01:13,440
pretty big topic, we'll just be scratching the surface.

19
00:01:13,890 --> 00:01:18,720
The plan is to show you more features as we progress with the upcoming project.

20
00:01:19,110 --> 00:01:26,250
Since I have a feeling that it will spend the next hour or two on just validation, understandably,

21
00:01:26,280 --> 00:01:28,860
a lot of you will lose interest and attention.

22
00:01:29,220 --> 00:01:33,920
Don't worry, though, the end of the video, I will show you where you can get more info.

23
00:01:34,200 --> 00:01:41,100
So at the moment we have a very minimal setup where we have properties and we just specify the type

24
00:01:41,740 --> 00:01:50,010
or we can set up our properties as objects and then we can also set up built in validators.

25
00:01:50,350 --> 00:01:51,410
How is that going to look like?

26
00:01:51,420 --> 00:01:58,530
Well, our move the string part from the name and then I'll set it equal to an object outcome, of course,

27
00:01:58,530 --> 00:02:00,690
since I need to keep the proper syntax.

28
00:02:01,110 --> 00:02:04,240
And here the first property we want to set up is type.

29
00:02:04,590 --> 00:02:09,220
Now it's still going to be a string, but now, of course, we can add more validators.

30
00:02:09,600 --> 00:02:11,590
Now, what Valderas can we use?

31
00:02:12,030 --> 00:02:14,500
For starters, we can go with required.

32
00:02:15,060 --> 00:02:24,000
So what this is going to do is if I pass in the value the object without my name properly, then it's

33
00:02:24,000 --> 00:02:26,220
going to spit back big fat error.

34
00:02:26,700 --> 00:02:33,330
Now, at the very basic level, you can set up equal to true or false, either it is required or not.

35
00:02:33,600 --> 00:02:39,660
But if you want a custom message, you can actually set it equal to an array where you go with the first

36
00:02:39,660 --> 00:02:45,270
value again, true or false, and then you add a comma and then you pass in that custom message.

37
00:02:45,570 --> 00:02:50,790
And in my case I'm going to go must provide and name now what else I can do?

38
00:02:50,820 --> 00:02:59,790
Well, I can try to imagine the scenario in a postman if I go here with name and if I pass in here spaces

39
00:02:59,790 --> 00:03:06,150
and then John effectively with our current setup, that's exactly how it in our database.

40
00:03:06,570 --> 00:03:11,760
And if I don't want that, I can simply go back where we have the object and then the property that

41
00:03:11,760 --> 00:03:15,930
you're looking for is Trem and will set it equal to true.

42
00:03:16,410 --> 00:03:22,200
And what's also really nifty that four strings we can get many values or max values.

43
00:03:22,590 --> 00:03:29,670
And my since I don't want my value to be bigger than 20 characters for the name one, I can go with

44
00:03:29,670 --> 00:03:33,470
Max and property that we're looking for is length.

45
00:03:33,880 --> 00:03:35,580
Make sure that you spelled correctly.

46
00:03:36,010 --> 00:03:42,120
This is always a doozy for me when it comes to length and again will set it equal to honor.

47
00:03:42,570 --> 00:03:48,540
And I'll say that the value for my string is going to be 20 and then in here we'll just go with name.

48
00:03:49,170 --> 00:03:51,390
Cannot be more.

49
00:03:52,230 --> 00:03:59,710
Van, and, of course, we'll set it equal to 20 online characters and essentially we're done with basic

50
00:03:59,710 --> 00:04:05,340
Valadares for name, because like I already mentioned, I really just want to scratch the surface.

51
00:04:05,650 --> 00:04:07,440
So you get the main idea.

52
00:04:07,660 --> 00:04:13,210
And then as we're progressing with the project, then, of course, will implement more features.

53
00:04:13,630 --> 00:04:18,399
And I want to do the same thing for completed where I'm not going to go with the required.

54
00:04:18,640 --> 00:04:24,880
Hopefully this is clear and you'll see in a second what kind of error we have if we don't pass the name

55
00:04:25,150 --> 00:04:26,150
part for the completed.

56
00:04:26,170 --> 00:04:29,400
I just want to show you that we can also set some default values.

57
00:04:29,740 --> 00:04:36,430
And again, we want to remove this boolean and we're going to go type we'll set it equal to Boulin.

58
00:04:36,910 --> 00:04:41,400
And as far as the default value, the property is simply default.

59
00:04:41,410 --> 00:04:48,040
And as far as default in this case for the task properly completed, think it's going to make most sense

60
00:04:48,490 --> 00:04:54,630
if we'll set it equal to false again, just to showcase how is that going to look like in a real project?

61
00:04:54,940 --> 00:05:01,900
So as I'm adding the task, new task by default, it's not going to be completed.

62
00:05:02,260 --> 00:05:09,010
And only when we navigate to a specific task page and I say, yep, it's completed now, we can add

63
00:05:09,010 --> 00:05:11,860
it and then of course, we can navigate back to all the tasks.

64
00:05:12,370 --> 00:05:13,780
This was just my setup.

65
00:05:13,790 --> 00:05:16,270
Of course, there's a million different ways how you can do that.

66
00:05:16,570 --> 00:05:23,920
But that's why in here, when I'm setting up my Valadares, I set my completed one to be equal to force.

67
00:05:24,320 --> 00:05:27,940
So now of course we can save it and we can test it out.

68
00:05:28,360 --> 00:05:33,190
So I'm going to navigate back to my setup here in a post man.

69
00:05:33,340 --> 00:05:38,320
And as you can see, completed is still not required and that is done on purpose.

70
00:05:38,320 --> 00:05:41,050
So essentially already set this up as default.

71
00:05:41,050 --> 00:05:41,440
False.

72
00:05:41,650 --> 00:05:44,140
So I don't want to set it up as required.

73
00:05:44,510 --> 00:05:46,420
Therefore, we don't need to pass it here.

74
00:05:46,690 --> 00:05:54,550
But I just want to see how it nicely is going to trim and how we also will get errors if we don't pass

75
00:05:54,550 --> 00:05:55,560
in any values.

76
00:05:55,780 --> 00:05:58,610
So in here, let me just try with the John and notice.

77
00:05:58,630 --> 00:06:03,550
Once we send we actually get trimmed value back and complete.

78
00:06:03,550 --> 00:06:06,040
It is set to force right away.

79
00:06:06,310 --> 00:06:08,170
OK, that's an awesome start.

80
00:06:08,320 --> 00:06:11,170
But notice what happens if we send an empty string.

81
00:06:11,650 --> 00:06:13,900
So let me remove all the values.

82
00:06:14,110 --> 00:06:17,110
Let me click on Send and now check it out.

83
00:06:17,470 --> 00:06:24,430
So we have here it is sending a request and the reason why we're not getting anything back, because

84
00:06:24,430 --> 00:06:27,580
we have a big fat error in our console.

85
00:06:27,850 --> 00:06:29,680
We have validation error.

86
00:06:29,980 --> 00:06:32,580
And there's a few things that we need to understand about this error.

87
00:06:33,250 --> 00:06:40,240
First, it is caused in a task schema because we finally set up default there and we set it equal to

88
00:06:40,240 --> 00:06:40,630
true.

89
00:06:40,870 --> 00:06:43,510
And of course, we're sending empty string.

90
00:06:43,810 --> 00:06:50,020
So that's the same thing as if I were to send a completely empty object over here.

91
00:06:50,020 --> 00:06:55,540
So if I cancel it and if I send it again, notice again, we're not getting the response.

92
00:06:55,900 --> 00:06:57,130
That's point number one.

93
00:06:57,250 --> 00:07:05,040
Number two, if you take a look at the console, you see that we have this unhandled promise rejection,

94
00:07:05,980 --> 00:07:08,470
this errors original bla bla bla bla bla.

95
00:07:08,710 --> 00:07:15,520
And long story short, it is caused in our create task because we have a wait keyword.

96
00:07:15,700 --> 00:07:17,890
However, we're not handling this gracefully.

97
00:07:18,190 --> 00:07:23,710
We don't have the try catch block where we set up the await in the try.

98
00:07:23,980 --> 00:07:27,610
And if there is an error, then of course we do something about it.

99
00:07:27,970 --> 00:07:32,950
And since this video is already getting quite long, I'll actually do that in the next one.

100
00:07:33,490 --> 00:07:34,950
So I'll just put this one on ice.

101
00:07:34,990 --> 00:07:39,010
And then in next video, we'll set up a proper response.

102
00:07:39,430 --> 00:07:46,450
And just to recap, as far as basic validation, we just need to set our property equal to an object.

103
00:07:46,780 --> 00:07:48,940
Then we need to set up still to type.

104
00:07:49,270 --> 00:07:50,920
And in our case, that was a string.

105
00:07:51,310 --> 00:07:53,110
And then we can go with the required.

106
00:07:53,500 --> 00:07:57,310
If we want to have a custom message, then we just set it equal to an array.

107
00:07:57,880 --> 00:08:03,070
And the first item is going to be the value, whether that's true or false for acquired.

108
00:08:03,280 --> 00:08:07,660
And then the second one will be the custom message, one for destroying.

109
00:08:07,660 --> 00:08:09,160
We can also use Trem.

110
00:08:09,400 --> 00:08:13,810
I'm not just going to make sure that we don't have any whitespace and you can probably already guess

111
00:08:13,810 --> 00:08:16,360
that there's one for the main length as well.

112
00:08:16,780 --> 00:08:22,090
And in a completed one, we covered how we can add a default value as well.

113
00:08:22,450 --> 00:08:25,090
So for this one, we set it equal to false.

114
00:08:25,450 --> 00:08:32,409
And if one of our validations is going to fail, then of course we won't be successful of adding that

115
00:08:32,409 --> 00:08:32,770
item.

116
00:08:33,280 --> 00:08:40,299
And like promised, if you want to learn more about validation, just head on over to Montague's docs.

117
00:08:40,870 --> 00:08:44,800
So navigate here and then you're looking for the validation.

118
00:08:45,280 --> 00:08:48,910
And as you can see, there's quite a bit of info here.

119
00:08:49,240 --> 00:08:51,460
So if your idea of good time.

120
00:08:51,730 --> 00:08:54,730
Scouring through the ducts, you won't be disappointed.

