Fast API Tutorial, Part 5: Query Parameters and String Validation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to part 5 of our fast api tutorial in this video we're going to quickly touch on uh string validation in query parameters so now if we look this is our app which is running down here here we have the swagger documentation open right now let's so this is the query parameter if we recall correctly which we do i do at least let's go in here let's add another method or another route we'll do app.get items and we'll say this is async def just read items now what we're going to do is we're going to say this query is as we're used to string or none equals none this time we're going to say results equals a let's set up a key value store items item id foo item id bar now we will say if q results dot update q with q and let's return q okay so just as a refresher this string or none is the same thing as saying optional string if we were to import optional from typing if we imported typing.optional we're not going to do that because i'm again i'm using 3.10 for python but if you're using anything less than that then you'll want to use that optional that optional parameter so now what this does if we go in here and we refresh the prey page we see this and we're good this is fubar we get exactly what we think i know we want return results apologies there we go now we get what we expected to get okay perfect we're good now let's say we want to add some sort of validation though let's say we want to set the limit of our query parameter to be i don't know say 10 characters this is six let's say we don't want fubar foobar to be a an available query string well there's no way we can really do that right here but what we can do is we can import and we can use this query object that we get from fast api so we go into here from query and now if we look at this in pi chart i'm going to do control b and it's going to bring me to the declaration you can see the first parameters are default which is going to be any type and then we have all of these keyword arguments that we can pass in we're going to touch on some of those in just a little bit but here let's set our default to be none which is this is the exact same thing as what we had just a few minutes ago so this equals none is the exact same thing as this but now what this will allow us to do is we can set something like max length equals 10. and now we have declared this and we get this error when we trade notice i didn't refresh the page so this this fast api app has um has no real way at least in the documentation here of knowing if this is valid or not i'm going to have to refresh the page but what we see is on the back end at the very least we can see ensure this value has at most 10 characters now let me refresh the page try it out we're going to do fubar foobar and now fast api the swagger documentation throws this error if we go into here and we the request with q equals fubar fubar you can see we get that same error that we got before this is this is the more valuable thing and sure this value has at most 10 characters i say it's the most valuable thing because if we're building a client-side application this is just not returning anything this doesn't give us any information it's helpful from a you know a documentation standpoint on the back end but if you're building a client-side application you need to be able to read you know this sort of error documentation okay let's add in another one let's add in a min length min length equals three so now if we try and type fo we can see ensure this value has at least three characters okay again we're going to go into and i'm sorry for the zooming in and zooming out um it just it it's because i'm using one browser window maybe i should use more than one now remember we didn't refresh the page yet so this should still at least send the request but we're going to get this error response that we expect now if we refresh the page the swagger documentation here has updated we have to try it out again we type in fo and now we get that that behavior that we anticipate let's see there are there are some other things we can do we can add in regular expressions which i am absolutely terrible with but let's assume that we want to have some sort of fixed query i don't know why i would want to do this i can't think of a reason i'm sure it's there's a valid reason maybe it's the sort of thing where you want it it's almost like a query parent a query string version of a path parameter if you will but what this is saying is this is the only thing that will be allowed as our query the word fix query let's go in here and refresh the page and let's try this out so first things first we can set it to none and we're good but if we try and set this to anything so this is at least three characters it's at least 10 it's less than 10 characters but the regular expression does not match the words fixed query fixed query this will now work but if it's anything other than that it will not work because it doesn't match exactly this there are other you know you if you want it to be a date and you don't want to use the built-in date format or you want it to be you know formatted a certain way there are ways that you can use regular expressions um in here i i wouldn't i mean i'm terrible at it i don't see myself ever using it but it is it is an option what we can also do if we get rid of this we can set a default value so let's say we want the default to be something like fixed query and let's get rid of this none because we're declaring a default value a non-null default value so if we don't pass anything in if we go into here and we don't pass in any query you can see we're going to get the query result of fixed query we get that back we can pass anything else q equals hello and we're going to get hello but if we don't pass in any sort of query whatsoever we get back this default parameter i mean it's a python default parameter it's exactly what we anticipate okay the next thing that we want to do is we want to include a required query string so this has a default parameter so i mean this isn't required but it is this is a way we can we can set something so let's go into here and make this none and let's set this string or none equals that so this is an optional parameter as we're going to see okay if we don't pass anything in nothing happens we're good but as soon as we get rid of all of this we execute we're going to notice the error saying field required let's refresh our page and now we can see here this is a required query parameter we have to pass something in if we don't pass anything in here we're going to get this field required error again so this is good if you want to have a required query string this is how you do it but how do you how do you do it without having a default so we want to have a min length equals 3 max length equals 10 but we don't want this to be none we don't want this to be fixed query because this is again it's it's not requiring it it's by default setting it to something else well the way you can get around this is by using these ellipses here so this will this will allow us to say this is required we don't have a default value but it has to be something now if we go back in here and we refresh our page we can see again this is a required query but now we're able to actually include some validation on it okay so that's how you would add in a required parameter with that that validation i'm going to go ahead and take this out for now we're going to say none uh string or none so now what we're going to do is we're going to add in the possibility of multiple values okay so instead of just a string here let's say we want to pass in items q equals a and q equals b and q equals c and q equals d well this is just going to return q equals d because it's assuming it's one thing it's going to read through all of these and it's going to get oh hey q equals d but if we want to allow for multiple options it's very very simple to do all you need to do is say that this is going to be a list of strings that's it what this will do is this will tell fast api that we're going that we could be passing in multiple parameters we don't have to pass anything in because again this is not it's or none but now if we refresh this page we can see we get q is now a b c and d okay so you can pass that in as as a list by adding in as many of those query parameters as you need as long as you're declaring this list um similarly to how we saw before we can we can set a default parameter for this um instead of it being just a string though this would be for example foo and bar and now if we go back in here and we get rid of that and we hit enter you can see our query by default is going to be foo and bar okay let's take a look to see what it looks like in the swagger documentation just so you can kind of get an idea so you can see here we've got these string items we can get rid of these we add a string item string string one string two oh i thought i was being so clever string three and so on and so forth we hit execute and we get all of these values for our our query strings okay um we can add in um let's see let's see let's see let's touch on adding in metadata let's do that so we're going to go back to our good old string or none it's a query of none let's say min length equals 3 max length equals 10. now let's add in a title title equals sample query string let's hit save just so it reformats a little bit um let's take a look let's see what else we want to add let's add in a description um description equals this is a sample query string let's refresh the page and see what happens you can see here we have this is a sample query string if we go down into our schema here no it's not there i forget where it's supposed to be but it's not a big deal okay so here we have our description um if we want to add in let's see we can make it deprecated if we want to deprecated equals true and we refresh our page here you can see here it gives us the warning that it's deprecated but it still allows it to show up in case you're using an older version or something like that i don't want to leave that there um let's see let's see let's see what else do we want to add in um let's say we want to we want to call some we want to call it something besides q let's say we want to call it item query well this is not going to work so you notice how a lot of times like a slug field for example in a um in a blog will have um you know something like items um [Music] title equals hello world something like that i forgot the d um well so we can actually say post title equals hello world so this is a valid url parameter but python doesn't like this python can't handle a minus character you know it's a snake cased language so we're going to leave this as a queue just as a point here but now what we're going to do is we're going to say alias equals and then let's call it item query so now what will happen if we go into here this is a sample query string item query is this value right here okay that's all fine and good but let's take a look to see what happens so now if we type in q equals foobar we don't get anything and that's because this is no longer q sure it's declared as q here but for the purpose of the url itself this is now no longer q what we end up doing now is we say item query equals fubar and now it shows up okay so that that alias here allows you to replace that single variable with something that's a little more i don't want to say modern but you get the idea um one last thing let's uh let's see let's see let's see what i want to touch on let's see about hiding something so let's do [Music] uh let's leave this is q we'll get rid of our results oh you know what i'll do i'm gonna make this a separate one app.get items hidden because this is hidden query so we're going to declare this as hidden query is a string or none equals query none but now we're going to say include in schema equals false so now if hidden query then return hidden query hidden query otherwise return hidden query is not found okay now let's go back into here refresh our page we don't need read items right now we're going to look at items hidden query so now you notice there is no actual parameter that shows up here we can hit try it out and we hit execute and we get sample query param uh what am i missing here what am i missing oh duh hidden query route sorry i was so what was going on this is actually kind of stupid um so i was actually naming um my my method here the same thing as my parameter so that was uh kind of a dumb thing to do okay so now let's try again we go in here execute and this is still not working field is required i'm missing something okay i found it what i am missing is i actually have items slash item id up here i have it up here somewhere so this is assuming that i'm i should be passing in a sample query param and i should not so we're just going to say items underscore hidden is what we'll call it i'll refresh this page let's try it one last time execute hidden query is not found if i do items underscore hidden and i do hidden query as fubar we get the actual hidden query okay that was a lot and this tells you you should organize your methods and routes a bit better so that that sort of thing doesn't happen um okay i think that's it for now it's a long video already um in the next in the next tutorial we are going to talk about numeric validation in path parameters very very similar concept but let's say we want this is going to be some sort of integer or float and we want to you know validate that value you know greater than less than that sort of thing so yeah so until next time i will see you later
Info
Channel: JVP Design
Views: 13,891
Rating: undefined out of 5
Keywords:
Id: OkrDvk_qQ6M
Channel Id: undefined
Length: 19min 16sec (1156 seconds)
Published: Mon May 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.