Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config.AddFluentValidation -> Index was outside the bounds of the array. #45

Open
joost00719 opened this issue Nov 7, 2022 · 3 comments

Comments

@joost00719
Copy link

When I try to register the validators using AddFluentValidation I get an exception:

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=PeterLeslieMorris.Blazor.FluentValidation
  StackTrace:
   at PeterLeslieMorris.Blazor.Validation.ValidationConfigurationFluentValidationExtensions.ScanForValidators(IServiceCollection services, IEnumerable`1 assembliesToScan)
   at PeterLeslieMorris.Blazor.Validation.ValidationConfigurationFluentValidationExtensions.AddFluentValidation(ValidationConfiguration config, Assembly assemblyToScan, Assembly[] additionalAssembliesToScan)
   at ::redacted::.ConfigurationManagement.Blazor.Startup.<>c.<ConfigureServices>b__4_2(ValidationConfiguration config) in C:\Users\::redacted::\source\repos\Configuration Management Admin Tool\::redacted::.ConfigurationManagement.Blazor\Startup.cs:line 110

image
There's no further information about what exactly went wrong.

This exception started after these validators were added:

   public class CustomerCompleteValidator: CustomerComplexValidator
   {
      public CustomerCompleteValidator(): base()
      {
      }
   }

   public class CustomerComplexValidator: AbstractValidator<CustomerComplexDto>
   {
      public CustomerComplexValidator()
      {

         RuleFor(p => p.Name)
            .NotEmpty();

         RuleFor(p => p.Email)
            .NotEmpty()
            .EmailAddress();

         RuleFor(x => x.PhoneNumber)
            .NotEmpty();
      }
   }

I am unsure whether or not my validator is invalid, or if it's a bug in this package.

I think a fix would be either make it work, or throw a meaningful exception, so the caller knows what's up.

@joost00719
Copy link
Author

joost00719 commented Nov 7, 2022

I have rewritten my validator to not inherit from another validator, but rather add the other validator as rule.

   public class CustomerCompleteValidator: AbstractValidator<CustomerCompleteDto>
   {
      public CustomerCompleteValidator()
      {
         RuleFor(customer => customer).SetValidator(new CustomerComplexValidator());
      }
   }

This does not cause the exception. However, a proper exception message should still be implemented.

@mrpmorris
Copy link
Owner

Would you be able to send a link to an example app on github?

@joost00719
Copy link
Author

You can run this project to recreate the exception:
https://github.com/joost00719/PeterLeslie.BlazorDemo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants