We Are Getting New Validation Types in .NET 8!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video we're going to take a look at a few brand new data annotation attributes adding.net 8. now data annotations are still heavily used in both.net and asp.net core mainly for validation purposes and in this video we're going to see what exactly did we get and how you can use it in your obligations if you like to have content and you want to see more make sure you subscribe during the sub notification Bell and for more training check out my courses on domtrain.com okay let me show you what I have here and like I said data annotations are mainly used for model validation so I'm going to use a web API which has that validation logic built in to its incoming models and the first thing I'm going to do is add my first endpoint in this controller so this in point is a post endpoint construct and it has this struct example body it just has a good and ID and I want this good to be a required property so when someone makes a post request I want that ID to exist so you might say oh I'm just gonna use required and here is a bit of a problem with this if I just run it and I go to postman if I try to call this endpoint with an empty Json body this will still be accepted because good is struck so its default value is just an empty good so the required data annotation doesn't have enough context to understand what required really means for that struct now you can say yes Nick but you should just make this nullable and if you do make it nullable and this is required then what's going to happen is validation will kick in and even though that's true why should I have to make this good nullable if it is supposed to be required so this is where this new option in the required output comes in disallow all default values so if I set this to true now and this is a non-nullable good and I try to call that endpoint again with an empty request body then I'm getting the same validation error because even though it will default to its empty value its empty value in this case is not acceptable if I just go ahead head and I pass a proper value so I say generate a grid over here Postman and I pass it down then that is acceptable so this is a new thing in dotnet 8. the next data notation has to do with ranges so now let's say I have this range endpoint with this range example body and I want to accept a value in this age within a range so I want to say from zero to three years old like these board games do so I can say range 0 all the way to three but the interesting thing about this is that if I actually go and I lower the version of this project which is now.net 8 to.net 7 this won't break because this was possible so what changed here well what changed is that now you can actually have exclusive bounds so you can say minimum is exclusive so basically do not accept zero start from one or maximum is exclusive now if I do that the only allowed values would be be at one and two so let's just quickly run this just to showcase the feature I'm gonna go here I have this range endpoint three as you can see it's not acceptable and the age must be zero exclusive and three exclusive this means that two is acceptable one is acceptable but zero is not acceptable all three just a small update small change but now you have this option the next annotation has to do with length so let's say I have this length example body which has a collection of names and let's say that I only accept from one all the way up to three what can I do to limit it well traditionally you'd have to have a mean length of one and then you'd have to probably chain or have a second annotation of max length three and if I have that then I go ahead and I run it then as you can see if I try to create it using this array Nick Mike and John then that's acceptable the moment I add I don't know Tim then this is not acceptable it says the maximum length is three and if I try try to just remove everyone have an empty collection this is also not acceptable so you need from one to three what we got now in dotnet 8 is you can actually remove this and you can simply have length and you can have from one all the way to three consolidating those two annotations in one and if I do that I have the exact same experience if I have four not acceptable and it actually has a better message telling you that the minimum length is one the maximum is three so if I go and I say nothing then it's not acceptable but if I go back and I just have three people here then that works absolutely fine very nice addition just converting something that you could do before in a single attribute the next one which is pretty handy actually goes as follows let's say I have this karenessing endpoint with this values example object and I have a currencies string and I only won't accept three currencies so GBP euros and US dollars so I'd have to do something fancy beforehand but now all I have to do is say allowed value and if I pass that down as a parameters array I can say GBP euros and USD and if I do that and I run my API when I can go to that endpoint if I try to do this with empty this isn't acceptable it does not equal anything in that attribute but if I say GBP then that is accepted if I try to do Australian dollars then that's also not accepted because it is not in that collection now that is only half of the feature what we also got is the denied values so if you have special preferences of things that you don't want to have someone submit I don't know I'm just going to pick a couple of them at random then if you have that and someone tries to use them guess what it actually is going to be denied so this is not acceptable but anything else is that can also be very handy very very nice and I'm going to wrap this up with a data annotation that is the most questionable in terms of usability but I guess they added it so now we have this base60 for example and we have this message property over here one but if I just wanted this to be a base64 thing well traditionally you might have to use something like regex2 limited but now all you have to do is say base64 and that is it now you have to be very careful with this because you might misunderstand it this does not mean that this will validate that something is a base64 string that necessarily gets decoded into something in a valid way this just says that any base64 character is acceptable so if I just go with an empty string it's acceptable so it passes if I try to pass this text and some capitals here all of these characters are valid base64 strings so they are accepted the moment I introduce something that is not part of the base64 character set something like this then it's going to say hey validation failed this is not a valid base64 encoding but it doesn't mean that it's going to validate that for example this thing is something that decodes to 10 next door bytes or whatever it is just the characters and that is it how much you're gonna use it I don't know but now you have it now it is there in my opinion these are loud ones very handy length consolidation is always good range exclusive how often do you use exclusive I don't know inclusive a thing makes more sense for me but this one over here this allowed all different values it's very very handy now I want to know from you in the comments are you using data annotations and are you mixing them up with something like fluent validation or have you completely replaced them with fluent validation leave a comment down below and let me know well that's all I had for you boys video thank you very much for watching guys jump next to my patreons for making this videos possible if you want to support that's really gonna find the link description down below leave a like if you like this video subscribe more to like listening the Bell as well and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 36,121
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 7, .net 8, dataannotations, new data annotations, data annotations, data annotation validation, data annotation validation in web api, We Are Getting New DataAnnotations in .NET 8!, We Are Getting New Validation Types in .NET 8!
Id: 2VxFVRAOdZk
Channel Id: undefined
Length: 8min 0sec (480 seconds)
Published: Thu May 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.