ASP.NET Core Web API .NET 8 2024 - 17. Data Validation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so whenever you fill out a form on the internet you probably fill it out with bogus addresses you put fake addresses down you put the wrong number down and this is very common and this is the reason that we have this thing called Data validation data validation is going to make sure that the people who are filling out the forms the people who are submitting data to your API are doing so in a correct format and data validation comes in many forms but I'm going to just cover the first two most common forms the first is going to be whenever you actually submit the URL you can perform data validation in the form of URL constraints we'll talk about this more here in a second the most common and the most robust is going to be data validation for your Json and what's going to happen is that whenever you submit a URL and whenever you submit an actual piece of Json to a controller to net core what's going to happen happen is that this is going to be a string and once it passes over here it's going to rip off all of the numbers and all the things that are identifiers turn them into integers and this is almost a form of validation in itself because if you submit something that's not supposed to be an integer is going to give you an err the second and by far the most common is whenever you're submitting Json it's going to turn it into a whole entire object like it's going to take a huge string just like the a huge you know blob of Json and it's going to actually turn it into an object and that's actually pretty amazing but let's go into a little bit more depth so simple types whenever you submit the actual URL whenever you submit an ID through the HTTP get annotation just like this it's going to actually turn it into an INT and let's just say you did not submit a one or you submitted maybe a string right here this is going to catch it this is actually going to say this is not not an INT this is actually a string and this is for simple types for complex types as I mentioned before you're going to submit a whole entire piece of Json and we are going to put our data annotations within the actual dto but it's kind of complicated so let's go ahead let's jump over into VSS code and let's do some coding okay so the First Data validation that we are going to do is we're going to do URL constraints sometimes called route constraints and the first one is going to be an INT so we'll have an ID we'll have an INT we'll have a stock ID and we'll have an INT right here then we'll have an ID going to do an INT let's see here ex moved it up looking good and the last ID is going to be an INT and that's going to be on our delete once again route constraints are very easy so let's go to our stock and let's let's do the exact same thing we're going to go into here and this is just a form of type checking that all this is going to do is make sure that it isn't in when it's initially passed in and if you do pass in a string it's just going to give you a 404 error so go here going to do int do ID is equal to int and that looks good so let's go ahead Let's test it to make sure we're going to go net watchrun okay so what I'm going to do is I am going to go into one of the comment IDs and I'm just going to test it really quick I don't think we need to test all of them and another thing too is that you can't exactly test it right here so what you have to do is you have to have it's almost like a workaround so what I'm going to do is just copy and uh paste this directly into the URL and I'll just type in string and what's going to happen is that it's just going to say it can't can't be found so that is uh our for that is a very simple form of data validation and that's route constraints now we want to move on to comp more complex forms of validation but very simple these are not very complicated forms of validation and what we're going to do is we're going to go inside of our dto and we're going to make sure that we have uh what are called Data validation annotations that's very big word on top of our actual properties within our dto I would highly recommend never putting data validation directly in the model if you put data validation directly in the model it's going to apply it globally and you typically don't want that so this is another reason that we create dto is so that we can add our uh data validation in here so I'm going to go required and it's going to go ahead it's going to bring it in um through the component model data annotations namespace and then we're going to go up here we're going to go Min length and we just have to type out a couple of these and after this we can copy and paste the rest feel free to set these to whatever you like but I'm going to make sure that it is a minimum length of five and I'm going say the error message is equal to we'll say title must be five characters yeah I think that that is what we want so title is five characters just like this same thing we're just going to go uh down one more and we're going to make it max length so we're we're going to say max length is equal to 280 and I think I just typed in Max characters for like a Twitter comment or something and that's how I obtain these so going to go here title cannot be over so we'll say title cannot be over 280 characters characters just like that so the next thing I'm going to do is just go over here and I'm going to copy and paste this down there's no reason to typ go ahead and type all this down and then just change this to content must be minimum five characters and see content must be over to 280 characters and then we can just do the same exact thing for the update so we'll do update comment request D and we can just copy and paste them uh over so first one title just go into here we'll do the same exact thing why did it do that so go over here go down say title and we need to go ahead bring this in so control dot then we're going to do the same exact thing okay now what we're going to do is we're going to do all of our data annotations for the create stock request utto so we're going to go into here create stock request and we have the symbol company name purchase we have the dividend and same thing as before we're going to say required go ahead bring this in then we're going to go uh max length we'll say max length is equal to 10 and it's going to be airor message and because it's required it's going to make sure that we only submit one and I I'm I'm almost 100% positive that there are uh stock symbols with just one but there's not over over 10 but I'm not I'm not 100% sure on the 10 but I am like a I am positive that there are one one letter uh stock symbols okay so we're going to do the exact same thing with the company name and say company name cannot be over 10 characters and we'll do the purchase price so we're going to say we're going to make this required then we're going to give this a range and the range is going to be one to one trillion six so one two three one two three there's a million two three and we'll just add one more on there just for good measure all right so last div it's going to be in between 0.001 and 100 so we're going to go into here and we're going to have a range of 0.001 going to make it Max 100 although I almost guarantee you although I almost guarantee you that there's probably not a dividend that's going to be over 100 all right so required and we're going to go into here so max length is equal to 10 air message it's equal to Industry cannot be over 10 okay and then we're going to have the market cap and the market cap is going to be one between 1 and 5 trillion so range equal to one and we're going to say one two 3 one two 3 one two 3 looking good all right now we need to do the update stock and the update stock is going to be almost the exact same so what I'm going to do is bring this over here and I'm going to say update stock request and just go over so update stock request bring this over here bring this over here so I can see it and we'll just go ahead copy and paste all these over so go here I'm going to go paste that make sure to control dot then we're going to do company name so go down here then going go purchase I'm going go last div so go here I'm going go industry so just go ahead copy and paste that down there then market cap so go ahead copy and paste this looking good so even though we have all of these data annotations inside of our dto nothing is going to happen till we tell Donnet to actually perform validation and luckily for us we have abstractions on top of abstractions and all we have to do is add what's called Model state so model State and watch what happens all you need to do is add this and and we could add more complex forms of validation but this is a pretty simple API and you can get a lot done with model State you could like I said there are more comp you could do Global State validation but with just this with just this two lines of code right here we perform all of this validation and all that's going to happen is that if our model is not right and we did not submit the right type of validation or let's just say we didn't add the type type of symbol that we wanted or needed it's going to spit out an error in the form of a model State error where does this model State even come from well what where it's coming from is it's inheriting from controller base controller base is giving us this object and each time that this actual controller executes it's going to give us a brand new model State object and if our model state is not valid it's going to trigger a bad request so and just watch how easily we can add model state to all of this right here so we're going to go model state right here and technically we don't even really need uh model state for get all we don't really need model state for get by ID but I think it's good just to add it in case we add it later and we forget or we add some type of complex Json or complex validation or simple validation later and we forget it'll automatically be there and it's just really easy to add so I'm just going to go through here and go ahead add all these all the way down so go down here do the exact same thing and all we have to do is copy and paste this into our controller as well too so go here go down going to do the get by ID do post and I'm going to separate these apart these look this doesn't look this is all jumbled together I don't think it looks that great okay so we're going to do same exact thing go here and now we have delete and it's probably a good idea that we test all the end points this is probably going to take a little bit of time so I'm just going to go through and test all of these to make sure that they're working so I'm going to go into here go ahead just try it out it's looking good so we'll test for we'll test to see if we get apple back looking great so we have we'll do the comment ID and we're going to do let's just say apple and we'll just add Apple two we're going to add the Apple two to uh to the title all right so all right I'm getting a little cared away here so Apple 2 was the best computer oh my gosh I'm having way too much fun and we're looking good let's let's go ahead and make sure here Apple 2 was the best computer and let's go ahead delete it comment does not exist or it's actually number four okay looking good okay so let's go ahead down here let's go get a stock Let's test our actual post so we got an ID 21 and we're going to go into here add an extra title string content string to our actual uh Tesla stock again and everything's looking good to go next thing we want to do is we're going to test this and we're going to just go down here and okay yep so title field is required and let's also do a put as well too and let's just get a random comment so we'll get five we'll just try to edit number five go into here so we'll say five and go here go here yep looking good so now we're going to do the stock and I'm just going to go ahead and refresh this so get stock looking good we're going to go down here we're going to do the post so we're going to go ahead try this out string string string and just going to build this with bogus information so make sure that all these are looking good okay symbol field is required field PL div looking great then we're going to uh make sure that our ID so let's see let's go ahead test we go try we'll get we'll try to get 22 make sure that it works okay working as expected we'll try to edit Microsoft up here so 22 going to try and do the exact same thing just input bogus information so zero looking good okay okay then let's do the very last delete so let's do the delete we're going to go down and let's just delete 1018 because we don't want the string one so go 108 and looking good we now have success hope that you guys like this if you did make sure to smash that like button make sure to smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 4,871
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: 1i_WE4aGVLU
Channel Id: undefined
Length: 17min 50sec (1070 seconds)
Published: Thu Jan 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.