RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Validation with view models
Lecture: Concept: Adding validation to APIs

Login or purchase this course to watch this video and the rest of the course contents.
0:01 It's really important to have validation for our methods you should never trust input that's coming off the internet
0:09 not for security purposes or validation purposes. So here you can see that we've added some validation to this particular method
0:16 and I realize actually it was too simple in my little demo I just did,
0:19 actually I forgot to return an error in the case which things weren't good right, so there's a little bit more we'd have to add to round that thing up
0:27 but you can get the idea, we're not going to stick with it anyway, so it doesn't matter.
0:33 If we look at this code, notice there's one thing we're trying to do take the car and save it to the database,
0:41 there's a little bit of work to juggle to get the car and the id but this is basically what we want to do,
0:46 how clear when we go back is that that's the key thing we're doing here, that's the essential point, I don't think it's not terribly hard
0:54 but it's certainly not obvious, right, not as obvious as it should be, we want this to stand out more, and we want the validation
1:00 to be some more separate so we can test it more easily and not necessarily go mock out a bunch of things to be able to do that,
1:06 so if we look at this, we'll see the majority of the code we've written is actually validation, so all that is validation
1:13 and a little bit more of this is actually just to take what was submitted and convert it into the car as well, right
1:19 so this stuff right here is taking the raw data and pulling it back into what we're actually looking for.
1:26 So we're going to see that we can move this stuff here all the colored stuff that's not green, we can move that somewhere else
1:33 in something like I said, testable, readable, more clear single responsibility principle type things where its job is to parse and validate the data.
1:43 And then we hand it off to this function, like here's the car they wanted to update,
1:49 it's valid, or it's not valid, what you want to do with that, right? So that's what we're going to do next with this concept of view models.


Talk Python's Mastodon Michael Kennedy's Mastodon