Model Validation | Validation Attributes vs. FluentValidation | .NET 6

Ғылым және технология

Become a Patreon & get the source code: / amantinband
In this video, we'll learn how to use the built-in validation attributes for model validation and also how we can create custom validation attributes to match our needs.
We'll then cover some of the reasons why we might prefer using the FluentValidation library instead.
In the FluentValidation library, we'll cover the following:
0. FluentValidation vs FluentValidation.AspNetCore
1. FluentValidation built-in validators
2. FluentValidation custom validators
3. FluentValidation and Dependency Injection
4. FluentValidation & nested objects
5. FluentValidation & including rules from other validators
6. FluentValidation's built-in Message Placeholders
7. FluentValidation & Custom Placeholders
Give FluentValidation a ⭐:
github.com/FluentValidation/F...
#dotnet
00:00. The project we'll be using as an example
02:15. Built-in Validation Attributes
03:05. Returning a ValidationProblem when the ModelState isn't valid
04:00. ApiController Attribute - Automatic 400 responses & Automatic binding
05:00. Custom Validation Attributes
06:14. Custom Validation Attributes: Custom Error Message
07:12. Validation Attributes vs. FluentValidation
08:31. Adding the FluentValidation Nuget package
08:41. Implementing our first Validator
09:38. Registering the Validator using the IValidator interface
10:06. FluentValidation Manual Validation
11:52. Adding the FluentValidation.AspNetCore Nuget package
12:06. Registering the Validator using the AddValidatorsFromAssemblyContaining extension method
12:56. FluentValidation Automatic Validation (AddFluentValidation extension method)
13:32. FluentValidation Automatic Validation disclaimer
14:20. FluentValidation Built-in validators (RuleFor, Must, NotNull, NotEmpty, CreditCard, Match)
15:05. Validators & Dependency Injection
16:05. FluentValidation RuleForEach & WithMessage
17:11. FluentValidation Custom Validator (IRuleBuilderOptions, IRuleBuilder)
19:55. Custom Validator & Custom error message
20:20. Built-in Placeholders (PropertyName, PropertyValue)
21:06. Custom Placeholders (ValidationContext, MessageFormatter)
22:33. Updating the CreateBreakfastRequest to have a nested object
23:50. Implementing the BreakfastDetailsValidator
23:50. Implementing the BreakfastDetailsValidator
24:40. Setting an external validator on nested objects (SetValidator)
25:20. Splitting rules across multiple files (Include)
26:51. Next video sneak peek

Пікірлер: 68

  • @MilanJovanovicTech
    @MilanJovanovicTech2 жыл бұрын

    Such a great introduction to the FluentValidation library. I loved that you showed that DI is supported inside of validators. Great job!

  • @joshmeyers2034
    @joshmeyers20342 жыл бұрын

    Love your videos! Every time you say "Yes" whenever the copilot autofill's syntax is really satisfying. Co-pilot is really great

  • @atifsaeedkhan9207

    @atifsaeedkhan9207

    2 жыл бұрын

    Yes! :D

  • @iliyan-kulishev
    @iliyan-kulishev2 жыл бұрын

    I've been using FluentValidation for some time now and was stunned by what I saw you doing with this custom validator. Great video, thanks !

  • @faisalalhoqani6151
    @faisalalhoqani6151 Жыл бұрын

    Thank you dear for your detailed, quick, and straightforward demonstration.

  • @wvanlosser
    @wvanlosser2 жыл бұрын

    Thanks for this great introduction into the FluentValidation library.

  • @DarkMetroid777
    @DarkMetroid777 Жыл бұрын

    Great video. Been diving into a dotnet project at work and needed some refreshers over this.

  • @DanielTames
    @DanielTames2 жыл бұрын

    you are the best .net technology teacher I have ever seen.

  • @amantinband

    @amantinband

    2 жыл бұрын

    Thanks Daniel! 🫶🏼

  • @haskell3702
    @haskell3702 Жыл бұрын

    This video has everything I was looking for, incredible, thank you!

  • @branislavpetrovic7486
    @branislavpetrovic74862 жыл бұрын

    Great overview of FluentValidation package, I realy like last parts about custom validators. Thanks!

  • @danielschmitt5750
    @danielschmitt57502 жыл бұрын

    Hey Amichai :) I have been subscribed to your channel since your first video came out and I though it was about time to leave a comment. I really enjoy watching your videos. They are informative and at the same time fun to watch (at least for someone interested in coding and software archtiecture). At work we already use a lot of the technologies and patterns you introduced in your last videos. Nevertheless, I still learned one or two new things or got a different perspective on things. Thank you for that.

  • @amantinband

    @amantinband

    Жыл бұрын

    Wow, that's awesome to hear. It's rare to have an opportunity to impact on such a scale, one of the reasons I created this channel. Thanks, Daniel!

  • @ugursesen7629
    @ugursesen76292 жыл бұрын

    excellent tutorial as always, you are awesome

  • @alexwexov4298
    @alexwexov42982 жыл бұрын

    As always, great !

  • @silvertek
    @silvertek2 жыл бұрын

    Great pacing and information, keep up the great work. My only complaint is I cant get videos fast enough! :D

  • @dicusardenis922
    @dicusardenis9222 жыл бұрын

    Thanks for the great content, it really inspires me to improve myself to become a better developer. Your are doing great, keep going 👍

  • @amantinband

    @amantinband

    Жыл бұрын

    Best comment I could get. Thanks, Denis!

  • @hasan_shans
    @hasan_shans2 ай бұрын

    Great video! Thanks!

  • @ugochukwuumerie6378
    @ugochukwuumerie63782 жыл бұрын

    Such a great content. Keep it up. Yes! :)

  • @reynaldorodriguez7161
    @reynaldorodriguez71612 жыл бұрын

    Loving your videos

  • @opachshwee5589
    @opachshwee55892 жыл бұрын

    Bro your channel is small but you are making some of the best .NET 6 content already. Keep up the good work.

  • @amantinband

    @amantinband

    Жыл бұрын

    Thanks 🫶🏼

  • @Ran_Gsharim
    @Ran_Gsharim Жыл бұрын

    Amichai you are the best! ❤️

  • @RakeshChintalacheruvu
    @RakeshChintalacheruvu Жыл бұрын

    Excellent!

  • @keyable
    @keyable Жыл бұрын

    Nice video! Could you make more detailed video about Attributes and how to use them for validation? Also making Custome Attribute validators?

  • @ugursesen7629
    @ugursesen76292 жыл бұрын

    looking forward to your new videos.

  • @bookuha
    @bookuha Жыл бұрын

    Thank you!

  • @evilTano
    @evilTano2 жыл бұрын

    Hi, great video as always. But I have doubts about the rules for the creation of Breakfast. I'm using a "DDD approach", so Breakfast is an Entity that has a static method "Create()" that works as Factory Method. In your example, you put the "sunrise" rules inside the DateTimeValidators and not inside the "Create()" method of Breakfast class. Is this a good approach? I have to put all the creation rules inside my “BuberBreakfast.Contracts” project and Validators or should I put the creation rules inside the Entity Breakfast? In short, the creation logic of an entity must be put inside our contracts or inside the Entities? Thanks!

  • @amantinband

    @amantinband

    2 жыл бұрын

    Hey Gaetano, that's an awesome question. I spent a lot of time thinking about it myself, questioning some of the recommendations, reading several blogs, and checking how some solutions behave at scale within Microsoft projects. For large projects with several teams/team members - I would recommend having the application layer validate the command/query, and the domain layer validate the object in the Create method, as you said. Will you have multiple duplicate validations? Yes. But allowing the domain models to be in an invalid state because it was already validated in the application/presentation layer assumes only the client provides invalid values when in actuality, we should also be defending our domain models from developers. Of course, the behavior on failure is different in each layer. Invalid request details? No problem - 400. Invalid values when trying to create a domain model? That's probably an internal error

  • @ankitpanwar1494
    @ankitpanwar149423 күн бұрын

    Did you know how we convert fluent validation responses to custom class responses while using auto validation feature?

  • @akalankagajasinghe5239
    @akalankagajasinghe5239 Жыл бұрын

    Fantastic playlist. Can this use for domain entity validation ?

  • @atifsaeedkhan9207
    @atifsaeedkhan92072 жыл бұрын

    Excellent one

  • @atifsaeedkhan9207

    @atifsaeedkhan9207

    2 жыл бұрын

    I have become a big fan of urs now. If you noticed I recently joined and start learning from ur videos. Is there any new video coming about Clean Architecture series where you already have I guess 7 videos.

  • @androidsavior
    @androidsavior4 ай бұрын

    what is the name of the screen recorder you're using to do marks during recording ?

  • @khaledsaleh286
    @khaledsaleh286 Жыл бұрын

    I liked this "Yes" to the copilot autofill :D. I didn't get the idea behind the IAssemblyMarker, can you please elaborate more?

  • @HueHueHue23
    @HueHueHue23 Жыл бұрын

    Hey! What's the shell and theme you're using in your integrated terminal?

  • @musazulu7013
    @musazulu70132 жыл бұрын

    i have to say i love your videos, i always check to see if you have uploaded new content, are you going to build the front-end as well or it's just going to be a service ? Greate Video by the way!

  • @amantinband

    @amantinband

    2 жыл бұрын

    Haha thanks, Musa! I'm not planning any from-end work as part of this series. This can be a fun .NET MAUI series for when I feel I know the topics well

  • @56kgofawesome
    @56kgofawesome2 жыл бұрын

    Great video! Nice structured and explained. What extension or customization are you using for your terminal? The color state of the branch and path looks gorgeous.

  • @amantinband

    @amantinband

    2 жыл бұрын

    Thanks, Humberto! I followed Scott Hanselman's tutorial. You can find it via "Scott Hanselman windows terminal" on YT or on his blog.

  • @alaamohammed3491
    @alaamohammed3491 Жыл бұрын

    if I did have the same rule for multiple proprieties (with only different parameters like max Length), Is there a way I could make a delegate or any think like this that could point at the same rule. And use it with different proprieties and there corresponding parameters. As it will be easier to make everything consistent, in case I wanted to change anything in the rule itself. Specially, that I have many Models and Dto that use the same rule. Thanks.

  • @fakhrulhilal
    @fakhrulhilal Жыл бұрын

    I love your videos, it's very straightforward. Can you share your setup when recording (VScode, screen recorder, etc) in the future?

  • @amantinband

    @amantinband

    Жыл бұрын

    Thanks, I'll probably do something like that in the future. For now, if you're interested, I have a video all about vscode for .NET: kzread.info/dash/bejne/n2182NV7YZDOcdI.html For recording the screen, I use OBS Studio.

  • @DrunkyDude97
    @DrunkyDude9711 ай бұрын

    How can we use fluent validation to validate the DTO like the behavior of Datanotations? Because when the validation happen later on, some of the null fields might have taken their default values by the framework. ex: int --> 0 Which would miss my validations. So how can we validate beforehand?

  • @androidsavior
    @androidsavior4 ай бұрын

    What is the tool that suggests the code to you ?

  • @jimkoro664
    @jimkoro664 Жыл бұрын

    Fantastic playlist/work! Thank you! How would you approach fluent validation in a project with application services (without mediatr). Automatic or manual validation -> which means no pipeline behavior, but instead doing the work manually for each action?

  • @amantinband

    @amantinband

    Жыл бұрын

    Thanks, Jim! I'd use automatic validation. If async validations are needed, I'd see if it's possible to implement some middleware/filter to do the work. Never looked into it though

  • @LouisWaweru
    @LouisWaweru2 жыл бұрын

    Oh, you switched to dark mode. Thank you, from all of us night owls. hoot🦉🌠

  • @amantinband

    @amantinband

    Жыл бұрын

    Haha yeah it bothered me for a while 😂

  • @g0keyn
    @g0keyn7 ай бұрын

    Which extentions do you use in vs code? I realy need this info.

  • @kinax2
    @kinax2 Жыл бұрын

    Thank for the video but AddFluentValidation is deprecated and is not working with Minimal API. Please can make a newer video with Minimal API and show us how to validate a request without using [FromServices] IValidator

  • @badr6896
    @badr68969 ай бұрын

    how to return custom response ?

  • @LindenMikus
    @LindenMikus2 жыл бұрын

    great vid, which software do you use to draw those arrows on screen?

  • @amantinband

    @amantinband

    Жыл бұрын

    Thanks, Linden. I use ZoomIt

  • @AlexRadevichPlus
    @AlexRadevichPlus2 жыл бұрын

    Will you proceed with the CQRS version of this application? Just want to see FluentValidation + MediatR Behaviors work together.

  • @amantinband

    @amantinband

    Жыл бұрын

    kzread.info/dash/bejne/eIyElbKKYJbMkZM.html 😉

  • @iliyan-kulishev
    @iliyan-kulishev2 жыл бұрын

    17:13 Does it make much difference that you define NotBeEmpty() as static?

  • @amantinband

    @amantinband

    Жыл бұрын

    Nope 🙂

  • @kamarchand
    @kamarchand2 жыл бұрын

    Where do you get your Idatetimeprovider from? thanks

  • @amantinband

    @amantinband

    2 жыл бұрын

    It's a simple wrapper around the built in DateTime

  • @rezarezash
    @rezarezash Жыл бұрын

    Should not this be a continuation of BubberDinner?

  • @dayv2005
    @dayv20052 жыл бұрын

    I misheard. Is it recommended to use manual or automatic validation?

  • @amantinband

    @amantinband

    Жыл бұрын

    It is recommended to use Manual validation

  • @sunefred
    @sunefred2 жыл бұрын

    Great library and greate demo. I am a bit dissapointed however that you put the validation in the controller layer and did not keep your structure, with domain and application layers, from previous chapters. If anything, validation is the core of domain functionality.

  • @amantinband

    @amantinband

    2 жыл бұрын

    26:51 😉. This video was an introduction to FluentValidation. In the next video we'll integrate it with our Buber Dinner application as part of the application layer. We'll also discuss and the difference between application layer validations and domain layer validations.

  • @sunefred

    @sunefred

    2 жыл бұрын

    @@amantinband Cool, looking forward to it!

  • @patrykk.4630
    @patrykk.46302 жыл бұрын

    Wow, pretty detailed explanation about this library. Do we use it? Of course 🙂 but more for a simple validations like 'greaterThaZero', 'notEmpty', etc. and several custom validators like '...doesExist'

Келесі