Validators and Custom Error Messages4:52 with Guil Hernandez
Sequelize offers several built-in validators that allow you to specify validations for each attribute of the model, as well as custom error messages.
We're getting the sequelized validation error messages back from Sequelize. 0:00 And this response, we're seeing the default error messages provided for 0:04 each validation error item. 0:09 Let's instead display clear and more useful custom error messages. 0:11 Sequalize offers several built-in validators that allow you to specify 0:16 validations for each attribute of the model as well as custom error messages. 0:20 To begin using them, add a validate object inside a models attribute object like so. 0:25 The notNull validator allows you to customize the allowNull false error 0:34 message. 0:39 Inside this new object, I'll add the message property and 0:40 set it to my custom error message. 0:44 A name is required. 0:47 I'll add a similar custom error message for email. 0:53 And birthday, and password. 1:10 I'll test these updates over in Postman by once again sending a POST 1:25 request with an empty object. 1:29 Now the custom error messages display in the response body. 1:33 Keep in mind that the notNull validator is 1:37 only allowed with allowingNull set to false. 1:43 So when using notNull, 1:46 you must also specify allowNull false in the model attribute's object. 1:48 Since the name, email and password values are string data types and 1:55 birthday is date only, we're currently able to set their value to an empty or 1:59 blank string and it will be successfully validated. 2:04 Which means that the user's name, email or 2:07 even password would be missing from the data. 2:10 So next I'll use the notEmpty validator to ensure that users cannot 2:13 create a new account if they submit empty values or fields. 2:18 Starting with the name attribute, I'll set a custom error message when 2:23 validation fails by setting notEmpty to an object containing a message 2:28 property set to the custom message, Please provide a name. 2:33 I'll do the same for birthday. 2:43 Then password. 2:51 For the email attribute, 3:04 I'll use the isEmail validator to check the email format. 3:06 If the submitted email format is not for 3:11 example, email@example.com Sequelize will throw a validation error. 3:14 I'll add the message property to display a custom error message, 3:19 Please provide a valid email address. 3:23 All right now I'm ready to test my updates in Postman. 3:30 First, I'll test the post request by setting the name, 3:34 birthday and password values in the body to an empty string. 3:37 Then set the email value to an invalid format, like guil.com. 3:41 Click Send and receive a 400 Bad Request status. 3:49 Notice the errors arrays and the response containing the custom error messages. 3:53 Next, I'll set each property to a valid value. 4:00 Click Send and now I receive a 201 Created status, 4:06 which means that the user entry was successfully created and 4:10 stored in the database as you see here in the response. 4:14 Finally I'll send a get request to the API users route. 4:17 Remember that this route here in routes.js, 4:22 the handler calls User.findAll to retrieve all entries from the user's table. 4:26 I receive a 200 OK status. 4:34 And in the response body, I see an array with the new user object. 4:37 It has an id of 1, along with the createdAt and updatedAt timestamps. 4:41 Sequelize adds to every model by default. 4:47 Good. 4:50
You need to sign up for Treehouse in order to download course files.Sign up