I only use these annotations to generate the Swagger file. I turn off actual validation, because I want to customize the error response. And its pretty hard to do that with the built-in validation. Additionally, not all validations we need to do can be done via these annotations. So you always end-up with additional validation logic anyways.
The updated Required attribute is by far the most useful. I've always hated having to mark my value types as nullable in order to mark them as required... (assuming that I'm not using FluentValidation)
MinimumIsExclusive and MaximumIsExclusive is great for decimal ranges that are exclusive (e.g. trying to constrain to 0.001 to 0.999). When you use an integer value, yeah, definitely not valuable.
Why not use the `required` keyword on the property instead of `DisallowAllDefaultValues`? It works for when you want to allow the default value too (like `0` for an int). I know that that will be like using of the `JsonRequiredAttribute` and the JSON serialization will fail, but it's still better IMHO.
I noticed that the RequiredAttribute in .NET 8 doesn't seem to ahve that DisallowAlldefaultValues property. Did they pull it out? Or is there some kind of NuGet package I'm missing?
2015 year, Microsoft Enterprise Library: - I'll be back 2023: i'm here! It's me - Microsoft Enterprise Library! Great news. I use alot this attributes with custom realization, except base64. Now i'll can migrate this to sdk implementation. Required attribute - this best update. Much problems with this attribute has before.
I like data annotations better than fluent validation but sometimes, when you need have multiple validations on the same model, fluent validations can be nice.
Why would I have to use Base64String if I'm going to later check if it can be decoded or not myself? In this way it seems useless. If they're gonna stick to their idea, at least they could add a property to let us choose full check in Base64String.
I have being thinking about fluent Validation for months and I don't why but I haven'tt made any move yet🙂. I'm just stuck with Data anatations while throwing in some custom/Regex ones when needed😊
Now, which method of validation is more efficient, overall, data annotations or any of the fluent validation patterns? Personally, for MVC, I like data annotations, but web API, I prefer fluent validations.
Du you also prefer fluent model configuration over attributes? Attributes like the RequiredAttribute serve two purposes model configuration and validation. the validation part can be replaced by FluentValidation. How much sense does it make to leverage the fluent api to define the model instead of attributes?
I personally feel like FluentValidation is better, because it allows a better approach to change and configure rules, instead of hard coding via Attributes and needing to recompile.
The DisallowAllDefaultValues kind of helps in the case of guids, but it doesn't really work for ints, where 0 would often be a valid value. Then we still have to make it nullable.
Comon devs. lets stop being lazy and just create our own validation attribute classes and middlewares... there are AsyncFilter actions too.... xD Great vid
I really hate DataAnnotations. Using FluentValidation is much more readable, flexible and restrict. Thanks for the video but I will never use them for sure In FluentValidation I can register them in DI and inject IStringLocalizer for different messages. The Messages here are prewritten
I only use these annotations to generate the Swagger file. I turn off actual validation, because I want to customize the error response. And its pretty hard to do that with the built-in validation. Additionally, not all validations we need to do can be done via these annotations. So you always end-up with additional validation logic anyways.
FluentValidations all the way. It reads nice, is functional, and you can embed logic much more easily than DataAnnotations.
Yes, into Validator we can also inject dependency. To attribute no.
The updated Required attribute is by far the most useful. I've always hated having to mark my value types as nullable in order to mark them as required... (assuming that I'm not using FluentValidation)
MinimumIsExclusive and MaximumIsExclusive is great for decimal ranges that are exclusive (e.g. trying to constrain to 0.001 to 0.999). When you use an integer value, yeah, definitely not valuable.
it was awesome seeing you today at NDC, you had my favorite presentation of the day 😀
I use both. Annotations are useful for generating swagger files with Swashbuckle.
The required attribute is nice. For base64, just make the property type `byte[]` The model binder should do "the right thing" and base64 encode it.
I was looking how to validate string. Found it here. Thanks
Why not use the `required` keyword on the property instead of `DisallowAllDefaultValues`? It works for when you want to allow the default value too (like `0` for an int). I know that that will be like using of the `JsonRequiredAttribute` and the JSON serialization will fail, but it's still better IMHO.
I noticed that the RequiredAttribute in .NET 8 doesn't seem to ahve that DisallowAlldefaultValues property. Did they pull it out? Or is there some kind of NuGet package I'm missing?
Great stuff. Need to read the whole doc of this.
I would have at expected a base64 validation to at least also check the length and if it's padded correctly.
I needed to implement the DisallowAllDefaultValues behavior last week. in .Net 6
Glad I'll have it available when I upgrade to .Net 8😃
2015 year, Microsoft Enterprise Library: - I'll be back
2023: i'm here! It's me - Microsoft Enterprise Library!
Great news. I use alot this attributes with custom realization, except base64. Now i'll can migrate this to sdk implementation.
Required attribute - this best update. Much problems with this attribute has before.
In complex project, i prefer using fluent validation on commands with CQRS and the contracts (DTOs) stays neutral without validation
Mostly I would just use fluent validations.
I like data annotations better than fluent validation but sometimes, when you need have multiple validations on the same model, fluent validations can be nice.
Blaze it. That's a missed opportunity, Nick.
I should have Rick rolled
I checked that too 😂
Why would I have to use Base64String if I'm going to later check if it can be decoded or not myself? In this way it seems useless. If they're gonna stick to their idea, at least they could add a property to let us choose full check in Base64String.
I have being thinking about fluent Validation for months and I don't why but I haven'tt made any move yet🙂. I'm just stuck with Data anatations while throwing in some custom/Regex ones when needed😊
Now, which method of validation is more efficient, overall, data annotations or any of the fluent validation patterns? Personally, for MVC, I like data annotations, but web API, I prefer fluent validations.
I personally prefer FluentValidation for everything.
Du you also prefer fluent model configuration over attributes? Attributes like the RequiredAttribute serve two purposes model configuration and validation. the validation part can be replaced by FluentValidation. How much sense does it make to leverage the fluent api to define the model instead of attributes?
New video suggestion: the new TimeProvider type
I already made that video
I prefer to write complex error handling with annotations but they have limitations. Then i force to use fluentvalidation
Are these annotations something that you see in for example a swagger documentation?
Could disallowing zeroed out guids cause problems if one of them is actually generated? Or is all zeroes not possible to generate?
I personally feel like FluentValidation is better, because it allows a better approach to change and configure rules, instead of hard coding via Attributes and needing to recompile.
FluentValidation is just too strong, no chance.
I remember when i first used MustAsync in fluent validation, i was mind blown lol
Does this apply to use cases not being ASP or why is it a ".NET 8" feature, according to the title?
DisallowAllDefaultValues does NOT exist!
dotnet runtime issue #83797
Is it possible to have FluentValidation rules to be displayed in Swagger in the same way as data annotations?
What is the behavior if I use the disallow default value in a boolean property?
Same thing as with the Guid. If it can't deserialize it (ie: for bool, that's only either True or False), then it'll return a bad request.
@@zabustifu is that the same for the enums case?
@@kein3001 I haven't tried it, but that's what I'd expect (ie: if the enum is not nullable, only actual enum values are accepted). To be confirmed.
What if the GUID is required but the default value is allowed? I need a way to express that the value must be present in the JSON.
Disallow all default values? I didn't know there were any types that had more than one default value!
Maps good with formats in OpenAPI.
i use majorly fluent validation
Good!
Are you trade forex or crypto? (just curious)
The DisallowAllDefaultValues kind of helps in the case of guids, but it doesn't really work for ints, where 0 would often be a valid value. Then we still have to make it nullable.
You can use BindRequired or JsonRequired (if that's inside JSON body)
Still no DateTime validation...
Thank you SeaSharp-Man. I am now smarter. 5 stars.
The error message exposing the underlying attribute name is just awful. Who wrote that, the coop student?
Comon devs. lets stop being lazy and just create our own validation attribute classes and middlewares... there are AsyncFilter actions too.... xD Great vid
I always replace them with fluent validation
bruh i thought that the base64 string @7:01 would be the video id for a rickroll :(
What was it? I'm too lazy to check it myself.
@@lalibi Blaze it
@@lalibi unfortunately, nothing, youtube just 404s at that url
Is the thumbnail a pun on frozen sets?
I wish I was this smart
I really hate DataAnnotations. Using FluentValidation is much more readable, flexible and restrict.
Thanks for the video but I will never use them for sure
In FluentValidation I can register them in DI and inject IStringLocalizer for different messages.
The Messages here are prewritten
For me is useless because the messages are in english only. I prefer to user FluentValidation package
Such features should be from. Net 1.1, not 8 😀
BTC is the ONLY acceptable currency. 😉
One should not deny BTC and ETH 😆
So basically some syntactical sugar.