1 00:00:00,600 --> 00:00:03,835 Let's begin setting up validations and constraints for 2 00:00:03,835 --> 00:00:08,224 the user model to prevent invalid data from being entered into the database. 3 00:00:08,224 --> 00:00:11,026 As I demonstrated in the previous video, 4 00:00:11,026 --> 00:00:16,159 null by default is an allowed value for any attribute or column of a model. 5 00:00:16,159 --> 00:00:21,089 Sending an empty post request or even setting a property explicitly to 6 00:00:21,089 --> 00:00:26,021 null allows User.create() to be called here in the post users route and 7 00:00:26,021 --> 00:00:28,797 successfully create a new user entry. 8 00:00:28,797 --> 00:00:30,432 We don't want that. 9 00:00:30,432 --> 00:00:34,688 First, we'll make sure that a null value is not allowed for name, 10 00:00:34,688 --> 00:00:39,332 email, birthday, and password using the Sequelize allow null check. 11 00:00:39,332 --> 00:00:42,001 If any of these attributes are null, 12 00:00:42,001 --> 00:00:47,436 the new user entry will not be stored and a validation error will be thrown. 13 00:00:47,436 --> 00:00:51,389 Inside the name attributes object, 14 00:00:51,389 --> 00:00:56,879 I'll add the allowNull option and set it to false. 15 00:00:56,879 --> 00:01:03,665 I'll do the same for email, then birthday, and finally password. 16 00:01:11,190 --> 00:01:16,142 Over in Postman, I'll try to send a post request to the API 17 00:01:16,142 --> 00:01:19,581 users routes with missing attributes or 18 00:01:19,581 --> 00:01:23,840 fields in the body, an empty object for example. 19 00:01:23,840 --> 00:01:29,109 Notice how I receive a 400 bad request HTTP status code back from the server. 20 00:01:29,109 --> 00:01:33,287 And the response body displays the validation error messages thrown by 21 00:01:33,287 --> 00:01:37,827 sequelize, preventing unreliable data from being stored in the database. 22 00:01:37,827 --> 00:01:42,825 So here in the post users route handler function in routes.js, 23 00:01:42,825 --> 00:01:48,480 the error is thrown in the try block, user.create does not get called. 24 00:01:48,480 --> 00:01:52,816 So the statements in the catch block execute instead. 25 00:01:52,816 --> 00:01:56,616 I've also added a console.log statement here in the catch block, 26 00:01:56,616 --> 00:01:58,559 where I'm logging error.name. 27 00:01:58,559 --> 00:02:01,674 So we're able to see the error being thrown. 28 00:02:01,674 --> 00:02:08,903 Notice in the terminal that the error log is a sequelize validation error. 29 00:02:08,903 --> 00:02:12,582 As you've noticed from working with sequelize in previous lessons, 30 00:02:12,582 --> 00:02:16,511 the format of its errors contains more information than what's typically 31 00:02:16,511 --> 00:02:18,399 necessary to return to the client. 32 00:02:18,399 --> 00:02:19,996 So for this workshop, 33 00:02:19,996 --> 00:02:25,126 I have also transformed errors to a more straightforward format by first 34 00:02:25,126 --> 00:02:30,170 checking if the error caught by catch is a sequelize validation error or 35 00:02:30,170 --> 00:02:35,490 a sequelize unique constraint error, which I'll talk more about later. 36 00:02:35,490 --> 00:02:41,549 Then I'm iterating over the errors array that's in the sequelize error 37 00:02:41,549 --> 00:02:46,714 passed to the catch block to access only the error messages and 38 00:02:46,714 --> 00:02:50,687 sending the error messages back to the client, 39 00:02:50,687 --> 00:02:54,085 as you can see here in the response body. 40 00:02:54,085 --> 00:03:00,107 Else any other error will be thrown here and caught up here and 41 00:03:00,107 --> 00:03:05,200 the asyncHandler function that wraps each route. 42 00:03:05,200 --> 00:03:07,177 More about this handler in the teachers notes.