1
00:00:00,210 --> 00:00:00,560
All right.

2
00:00:00,650 --> 00:00:04,320
And the first error that I want to work on is the missing field.

3
00:00:04,710 --> 00:00:10,470
So when we're setting up the schema, we set up three properties to be required.

4
00:00:11,190 --> 00:00:18,540
So if one or multiple are missing, we are going to be getting back the hours and we'll have access

5
00:00:18,540 --> 00:00:20,670
to those errors in the error handler.

6
00:00:21,180 --> 00:00:27,720
So now what I want to do is set up the condition where I check for that error and the error is going

7
00:00:27,720 --> 00:00:28,660
to look something like this.

8
00:00:28,680 --> 00:00:35,460
So if I'm going to go to a postman and if I'm going to remove the name, you'll notice that response

9
00:00:35,460 --> 00:00:36,690
comes back, something like this.

10
00:00:36,930 --> 00:00:40,170
So the message is going to be equal to a our object.

11
00:00:40,500 --> 00:00:45,750
And there we have the errors property, which is an object itself.

12
00:00:46,200 --> 00:00:53,700
And in there, every field that is going to be missing is going to be added as a property also in that

13
00:00:53,700 --> 00:00:54,630
error object.

14
00:00:54,810 --> 00:00:57,110
So the main one, not in the errors.

15
00:00:57,210 --> 00:00:58,290
That's a separate property.

16
00:00:58,560 --> 00:01:06,390
But on the main, our object, we also have these properties where we have the message name and also

17
00:01:06,420 --> 00:01:07,490
one more message.

18
00:01:07,500 --> 00:01:15,450
So this one is with an underscore where the fields are displayed, the ones that are missing, and if

19
00:01:15,690 --> 00:01:18,750
there's only one field, then we'll get this response.

20
00:01:18,750 --> 00:01:24,960
But if there's, for example, two fields missing, you'll notice that there are more properties in

21
00:01:24,960 --> 00:01:26,250
the error object.

22
00:01:26,280 --> 00:01:32,550
Again, there is one big error object and then inside of it first we have errors property.

23
00:01:33,530 --> 00:01:39,920
So these are all the fields that are missing at the moment is just one, but if I remove the emailing

24
00:01:39,920 --> 00:01:46,640
us, I don't need to set up proper syntax now in order not only I have the name, I also have the email

25
00:01:47,300 --> 00:01:52,250
in the Irish property and then on the object, the error object.

26
00:01:52,520 --> 00:01:56,300
There's also properties here for the name and message.

27
00:01:56,720 --> 00:01:57,930
And here's what I want to do.

28
00:01:58,460 --> 00:02:05,750
I want to first check the name property, and if it's going to be equal to validation error, then we

29
00:02:05,750 --> 00:02:11,420
know that we'll have to change some things around because the moment I'm sending back five hundred and

30
00:02:11,420 --> 00:02:12,380
that's not what I want.

31
00:02:13,040 --> 00:02:16,610
Actually, I want to send back 400 because it is a bad request.

32
00:02:17,030 --> 00:02:17,750
That's number one.

33
00:02:18,200 --> 00:02:21,800
Number two, I mean, we're sending back this giant object.

34
00:02:22,190 --> 00:02:23,870
How useful that is to the frontend?

35
00:02:24,140 --> 00:02:24,890
Well, not really.

36
00:02:25,190 --> 00:02:31,820
We kind of want to shorten this up a bit and send just, hey, listen, the field is missing or stuff

37
00:02:31,820 --> 00:02:32,570
along those lines.

38
00:02:33,020 --> 00:02:34,370
And the way we can do that first.

39
00:02:35,330 --> 00:02:37,670
I can just pass on the message if I want.

40
00:02:37,700 --> 00:02:38,720
That's definitely an option.

41
00:02:39,110 --> 00:02:47,270
But in our case, I'll also show you how we can iterate over these properties in the areas, object

42
00:02:47,750 --> 00:02:52,790
the property on the error object, and then we'll just grab the message over here.

43
00:02:53,270 --> 00:02:56,280
Senator, we have email and then we have the message.

44
00:02:56,300 --> 00:02:59,900
And the same goes for the name one again.

45
00:03:00,440 --> 00:03:01,010
Two options.

46
00:03:01,010 --> 00:03:07,430
This probably is a faster, but I think that one is a bit more concise.

47
00:03:07,580 --> 00:03:08,510
The second option?

48
00:03:08,810 --> 00:03:10,080
So let's start working on that.

49
00:03:10,100 --> 00:03:14,270
I'm going to navigate back to our project and where I have the air handler.

50
00:03:14,450 --> 00:03:22,430
First thing that I want to check is if in the air object, the name property is equal to validation

51
00:03:22,430 --> 00:03:22,630
error.

52
00:03:22,970 --> 00:03:23,900
How's that going to look like?

53
00:03:23,930 --> 00:03:25,670
Well, I can go with if error.

54
00:03:25,820 --> 00:03:29,870
So that's the main object, not the error property inside of it.

55
00:03:30,470 --> 00:03:31,640
A big distinction.

56
00:03:32,120 --> 00:03:37,070
And I'm going to say, if the error name is equal to and Huron, you can type it out.

57
00:03:37,070 --> 00:03:41,570
But in my case, just so I can avoid some silly bugs, I'm going to copy and paste.

58
00:03:41,990 --> 00:03:44,210
I'm going to set it up as validation error.

59
00:03:44,480 --> 00:03:48,860
And if this is the case, first thing that I one there is change the status code because like I said,

60
00:03:49,340 --> 00:03:50,570
it's not five hundred anymore.

61
00:03:50,750 --> 00:03:51,590
It is 400.

62
00:03:51,680 --> 00:03:53,240
It is a better request.

63
00:03:53,480 --> 00:03:57,380
How do we do that while we go with default error vanish status code?

64
00:03:57,380 --> 00:03:58,700
And that is equal to.

65
00:03:59,070 --> 00:04:03,500
And let's use our status code and then we're looking for bar request.

66
00:04:04,310 --> 00:04:05,150
So that's number one.

67
00:04:05,540 --> 00:04:12,170
Number two, what I can also do is change this message where I'm going to say, if this is the case,

68
00:04:12,440 --> 00:04:18,350
then the default error message property is going to be equal to my error.

69
00:04:18,740 --> 00:04:21,380
And then remember this property over here.

70
00:04:21,620 --> 00:04:27,950
We also have the message correct so we can just go here with error and message like show.

71
00:04:28,440 --> 00:04:31,430
And now, of course, you probably want to see the response.

72
00:04:31,880 --> 00:04:37,430
So it just changes around where I'm going to leave this one just in case I ever want to use it.

73
00:04:37,640 --> 00:04:42,290
I want to send back and then I'll copy and paste and uncommon this one.

74
00:04:42,620 --> 00:04:50,000
And instead of the error that we're sending back, what I want to do right now is send back the default

75
00:04:50,000 --> 00:04:53,510
error and then the message, not service one.

76
00:04:53,780 --> 00:05:00,110
And what you'll notice right now in the post, man, once we send it, we should get back four hundred.

77
00:05:00,350 --> 00:05:01,070
That's number one.

78
00:05:01,430 --> 00:05:02,800
And then we get this message.

79
00:05:02,810 --> 00:05:05,120
Here's a validation failed email.

80
00:05:05,370 --> 00:05:07,970
And I'm definitely you can keep it like this.

81
00:05:08,780 --> 00:05:11,810
But in my case, what I'm going to do, I'm going to comment this one out.

82
00:05:12,020 --> 00:05:15,980
Just search state for your friends and instead what we're going to do.

83
00:05:16,310 --> 00:05:22,580
We're going to look in that error object and in their outlook for the property values.

84
00:05:23,240 --> 00:05:29,960
So remember, that was a giant object where we had all the fields that are missing the name, the email

85
00:05:30,290 --> 00:05:31,370
and password.

86
00:05:31,850 --> 00:05:38,490
I will set them up as all right, I'll iterate over them and then I'll join the messages together.

87
00:05:38,810 --> 00:05:44,960
Now, since probably this is a little bit confusing, I'll come on this one out and let's send it one

88
00:05:44,960 --> 00:05:45,410
more time.

89
00:05:45,800 --> 00:05:46,760
So let's send it.

90
00:05:47,270 --> 00:05:49,010
And you'll notice this error, right?

91
00:05:49,370 --> 00:05:57,260
So in JavaScript, we have object values, which essentially is going to set up all these properties

92
00:05:57,710 --> 00:05:58,820
as an array.

93
00:05:59,630 --> 00:06:00,980
So whatever you have over here.

94
00:06:01,310 --> 00:06:07,970
So in my case, I have the email and I have the name, and then I'll iterate over that array and I'm

95
00:06:07,970 --> 00:06:14,390
just going to look for these messages just so have a little bit shorter error response.

96
00:06:14,660 --> 00:06:18,770
Just in case you need to jog your memory on object values method.

97
00:06:19,250 --> 00:06:22,010
The Amdocs is a very good place to start.

98
00:06:22,250 --> 00:06:28,610
So let's navigate back over here, and I'll just simply say a default error message is going to be equal

99
00:06:28,610 --> 00:06:31,100
to object values.

100
00:06:31,430 --> 00:06:37,000
And this is going to set up all the property values as an array and what properties I'm looking for

101
00:06:37,010 --> 00:06:38,360
while I'm looking for error.

102
00:06:38,660 --> 00:06:39,530
That's the main one.

103
00:06:39,770 --> 00:06:42,200
And then more specifically, errors object.

104
00:06:42,530 --> 00:06:47,570
And then once I have that, I can just change that map and then I'll call this an item.

105
00:06:47,810 --> 00:06:54,980
So now I'm talking about each and every property over here, and I want to access this message.

106
00:06:55,160 --> 00:06:56,600
So let's navigate back here.

107
00:06:57,670 --> 00:07:03,430
And let's say that as I'm iterating, I want to return the message and I want to join them together

108
00:07:03,670 --> 00:07:07,090
as one string, so chain not join.

109
00:07:07,300 --> 00:07:09,880
And I'll just add a comma in between.

110
00:07:10,270 --> 00:07:18,280
And once I have this code in place now again, I will uncommon this one and comment out into a response.

111
00:07:18,580 --> 00:07:24,880
So if we go back to the postman and if we're sending notice, not only I have four hundred, but I also

112
00:07:24,880 --> 00:07:26,110
have please provide email.

113
00:07:26,290 --> 00:07:27,220
Please provide a name.

114
00:07:27,520 --> 00:07:31,140
So if I'm going to set up my name back up.

115
00:07:31,720 --> 00:07:34,240
And in this case, I'm going to go back with John.

116
00:07:34,480 --> 00:07:36,220
You'll notice that now I only have.

117
00:07:36,610 --> 00:07:41,560
Please provide email again if you're somewhat iffy on this code.

118
00:07:41,860 --> 00:07:46,210
You can always use this error and message.

119
00:07:46,480 --> 00:07:53,230
And lastly, let me just point out something where remember when we were setting up the user, we were

120
00:07:53,230 --> 00:07:55,540
also checking for the valid email.

121
00:07:55,810 --> 00:08:01,120
So just to reiterate in the user schema, we check for empty values.

122
00:08:01,540 --> 00:08:09,850
We check for unique email, which we are going to cover in the next video and also we check for valid

123
00:08:09,850 --> 00:08:10,300
email.

124
00:08:10,630 --> 00:08:15,850
And what's really nifty that it actually gets handled with our validation error as well.

125
00:08:16,180 --> 00:08:23,920
And if you want to start out, go to your postman and try to set up the user with some kind of invalid

126
00:08:23,920 --> 00:08:24,310
email.

127
00:08:24,700 --> 00:08:28,270
So in this case, I'm going with email and then one at one.

128
00:08:28,630 --> 00:08:33,820
And what you'll notice when you send back is the message, as well as the status code.

129
00:08:34,120 --> 00:08:39,580
And in the message, it clearly says please provide valid email, which is again, super, super cool

130
00:08:39,850 --> 00:08:42,850
because we don't have to handle that separately.

131
00:08:43,000 --> 00:08:49,030
So not only we're handling missing fields, we're also handling a valid email.

