IntentArchitect / Support

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

Custom Fluent Validation Support #93

Closed JohannesMogashoa closed 3 months ago

JohannesMogashoa commented 3 months ago

Ask a question

Hi there,

I am currently trying to introduce custom fluent validations to my command properties. Example I would like to ensure that the Name property must be unique.

With fluent validation, the custom validators you can chain a custom message. Unfortunately I do not see an option that allows me to add a message.

I have tried doing the below but each time I run the software factory, it removes the WithMessage from the chain.

image

Is there a way in which I can keep this functionality of chaining, WithMessage? It would be nice as well to have an option in the fluent validation settings to introduce a custom message when using custom validators like Must.

joelsteventurner commented 3 months ago

Hi @JohannesMogashoa

Unfortunately you can't put the // IntentIgnore on a method chain like that. If you put the // IntentIgnore on the full statement it should work i.e.


    // IntentIgnore 
    RuleFor(v => v.Name)
        ...

Another note if you use the CustomValidation option as opposed to the 'Must' That would look similar to this


        private void ConfigureValidationRules(IValidatorProvider provider)
        {
            RuleFor(v => v.Addresses)
                .NotNull()
                .CustomAsync(ValidateAddressesAsync)
                .ForEach(x => x.SetValidator(provider.GetValidator<CreateCustomerCommandAddressesDto>()!));
        }

        [IntentManaged(Mode.Fully, Body = Mode.Ignore)]
        private async Task ValidateAddressesAsync(
            IEnumerable<CreateCustomerCommandAddressesDto> value,
            ValidationContext<CreateCustomerCommand> validationContext,
            CancellationToken cancellationToken)
        {
            //Validation
            if (!value.Any(a => a.AddressType == Domain.AddressType.Delivery))
            {
               //Custom Error Message
                validationContext.AddFailure("Require a delivery address");
            }
        }

Let me know if this solves the issue.

JohannesMogashoa commented 3 months ago

Hi @joelsteventurner

Yes absolutely, it works with the custom validation option.

joelsteventurner commented 3 months ago

Hi @JohannesMogashoa

This would be a great question for us to have on our public support repository, so any Intent Architect users with a similar question might find this answer. Would you mind if I move this this question to our public support repository?

JohannesMogashoa commented 3 months ago

Hi @joelsteventurner

Yes absolutely, I don't mind.