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
0:04 you should never trust input that's coming off the internet
0:08 not for security purposes or validation purposes.
0:10 So here you can see that we've added some validation to this particular method
0:15 and I realize actually it was too simple in my little demo I just did,
0:18 actually I forgot to return an error in the case which things weren't good right,
0:23 so there's a little bit more we'd have to add to round that thing up
0:26 but you can get the idea, we're not going to stick with it anyway, so it doesn't matter.
0:32 If we look at this code, notice there's one thing we're trying to do
0:36 take the car and save it to the database,
0:40 there's a little bit of work to juggle to get the car and the id
0:43 but this is basically what we want to do,
0:45 how clear when we go back is that that's the key thing we're doing here,
0:50 that's the essential point, I don't think it's not terribly hard
0:53 but it's certainly not obvious, right, not as obvious as it should be,
0:56 we want this to stand out more, and we want the validation
0:59 to be some more separate so we can test it more easily
1:01 and not necessarily go mock out a bunch of things to be able to do that,
1:05 so if we look at this, we'll see the majority of the code we've written
1:08 is actually validation, so all that is validation
1:12 and a little bit more of this is actually just to take what was submitted
1:15 and convert it into the car as well, right
1:18 so this stuff right here is taking the raw data and pulling it back
1:23 into what we're actually looking for.
1:25 So we're going to see that we can move this stuff here
1:28 all the colored stuff that's not green, we can move that somewhere else
1:32 in something like I said, testable, readable, more clear
1:36 single responsibility principle type things
1:39 where its job is to parse and validate the data.
1:42 And then we hand it off to this function,
1:45 like here's the car they wanted to update,
1:48 it's valid, or it's not valid, what you want to do with that, right?
1:50 So that's what we're going to do next with this concept of view models.