Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Schema definitions and validation
Lecture: Introduction to data validation
0:01 Whenever we send an invalid document to our API
0:04 like this one for example, where first name is supposed to be a string
0:08 but I am sending a number,
0:10 what we get back from the API is an unprocessable entity response,
0:14 and payload contains some information
0:18 about what went wrong with our document.
0:21 So here we have a status, key, and of course, it is an error,
0:25 we have an issues key where every key is a field
0:29 and then we have the errors for the field,
0:32 in this case, there is only one error so this is a string,
0:35 but if we had more than one error it would be a list.
0:39 And then we have the error where we get
0:41 a human readable explanation of what happened.
0:45 Now, this all works because Eve comes with
0:47 powerful builtin data validation features,
0:50 whenever we define an endpoint,
0:52 we also define a schema for the data coming into the endpoint,
0:56 so if the validation rules are met when the document comes in,
0:59 then the document is accepted and stored onto the database.
1:03 If any of the rules is not met, then the document is rejected.
1:09 We already defined the validation schema back
1:12 when we were building our first app,
1:15 let's go back to our code editor, here we see that we defined the two fields,
1:19 first time and last time and both were of type string,
1:23 so whenever we send a number
1:25 or anything else which isn't a string, we get an error back.
1:28 Now, in fact, you have a lot more rules you can set for your fields,
1:33 and in the following lectures, we're going to learn more about them
1:37 and how we can fine tune our document rules
1:41 to make sure that documents coming into the database
1:44 exactly match our use case.