IntentArchitect / Support

A repository dedicated to handling issues and support queries
3 stars 0 forks source link

FluentValidation - Add support for cascade options #76

Closed shainegordon closed 7 months ago

shainegordon commented 9 months ago

What problem are you trying to solve?

Imagine the following validation has been setup

RuleFor(v => v.Email)
                .NotNull()
                .NotEmpty()
                .EmailAddress()
                .MustAsync(ValidateEmailAsync);
...

private async Task<bool> ValidateEmailAsync(
            CreatePersonCommand command,
            string value,
            CancellationToken cancellationToken)
{
     var exists = await _personRepository.AnyAsync(x => x.Email == value, cancellationToken);
     return !exists;
}

In this case, we do not want to run the Must code, if the email is not a valid email. This is just an unnecessary operation.

FluentValidation supports this feature, using the .Cascade method.

RuleFor(v => v.Email)
                .Cascade(CascadeMode.Stop)
                .NotNull()
                .NotEmpty()
                .EmailAddress()
                .MustAsync(ValidateEmailAsync);

Describe the solution you'd like

Have an optional setting in the Validations properties of service designer, where a user can tick this option, and if it is enabled, it will add the .Cascade(CascadeMode.Stop) call

JonathanLydall commented 9 months ago

Thanks Shaine, this makes good sense, we'll certainly look into adding this option. I'll update this issue as we have more feedback.

joelsteventurner commented 7 months ago

Hi @shainegordon

We have put out a pre-release with this feature. Intent.Application.MediatR.FluentValidation v 4.4.4-pre.2

Give it a try and let me know if you need any additional support around this issue.

shainegordon commented 7 months ago

Hi @shainegordon

We have put out a pre-release with this feature. Intent.Application.MediatR.FluentValidation v 4.4.4-pre.2

Give it a try and let me know if you need any additional support around this issue.

Awesome, works perfectly