Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Schema definitions and validation
Lecture: Introduction to data validation

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Whenever we send an invalid document to our API like this one for example, where first name is supposed to be a string but I am sending a number,
0:11 what we get back from the API is an unprocessable entity response, and payload contains some information about what went wrong with our document.
0:22 So here we have a status, key, and of course, it is an error, we have an issues key where every key is a field
0:30 and then we have the errors for the field, in this case, there is only one error so this is a string,
0:36 but if we had more than one error it would be a list. And then we have the error where we get a human readable explanation of what happened.
0:46 Now, this all works because Eve comes with powerful builtin data validation features, whenever we define an endpoint,
0:53 we also define a schema for the data coming into the endpoint, so if the validation rules are met when the document comes in,
1:00 then the document is accepted and stored onto the database. If any of the rules is not met, then the document is rejected.
1:10 We already defined the validation schema back when we were building our first app,
1:16 let's go back to our code editor, here we see that we defined the two fields, first time and last time and both were of type string,
1:24 so whenever we send a number or anything else which isn't a string, we get an error back.
1:29 Now, in fact, you have a lot more rules you can set for your fields, and in the following lectures, we're going to learn more about them
1:38 and how we can fine tune our document rules to make sure that documents coming into the database exactly match our use case.


Talk Python's Mastodon Michael Kennedy's Mastodon