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

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')' #1698

Closed
Arash-Sabet opened this issue Apr 15, 2021 · 2 comments

Comments

@Arash-Sabet
Copy link

FluentValidation version

10.0.2

ASP.NET version

.net core 3.1

Summary

The fluent validation version 9.5.4 used to be able to validate our model successfully. Upgrading to 10.0.2 results to a failure per the exception in the title of this issue.

The stack trace is per below:

at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
at System.Collections.Generic.List1.RemoveAt(Int32 index) at FluentValidation.Internal.CollectionPropertyRule2.GetValidatorsToExecute(ValidationContext1 context) in C:\Projects\FluentValidation\src\FluentValidation\Internal\CollectionPropertyRule.cs:line 335 at FluentValidation.Internal.CollectionPropertyRule2.FluentValidation.IValidationRuleInternal.Validate(ValidationContext1 context) in C:\Projects\FluentValidation\src\FluentValidation\Internal\CollectionPropertyRule.cs:line 125 at FluentValidation.AbstractValidator1.Validate(ValidationContext1 context) in C:\Projects\FluentValidation\src\FluentValidation\AbstractValidator.cs:line 92 at FluentValidation.AbstractValidator1.Validate(T instance) in C:\Projects\FluentValidation\src\FluentValidation\AbstractValidator.cs:line 62

Steps to Reproduce

The model that used to be validated successfully by FluentValidation ver 9.5.4 looks like the following code snippet:

public class MyModel
{
		public dynamic Data { set => JData = value; }
		public JObject JData{ get; private set; }
		public bool Enable{ get; set; } = false;
		public string Id{ get; set; }
                public string[] OtherIds { get; set; }
}

Version 10.0.2 fails to validate this model. The validator's code is per below:

public class MyModelValidator  : AbstractValidator<MyModel>
{
        public MyModelValidator()
        {
            RuleFor(m=> m.JData).NotNull();
            RuleForEach(m=> m.OtherIds)
                .NotEmpty()
                .MaximumLength(100)
                .When(m=> m.OtherIds?.Any() == true);
            RuleFor(m=> m.OtherIds)
                .Must(m=> m.Count() == m.Distinct().Count())
                .When(m=> m.OtherIds?.Any() == true);
        }
}
@JeremySkinner
Copy link
Member

JeremySkinner commented Apr 15, 2021

Thanks, yes this is a bug. It happens when you're using RuleForEach with multiple components in the same chain, plus a condition which fails.

I've fixed the issue and pushed out 10.0.3 to nuget. Please give it a try and let me know if you run into any further issues.

@Arash-Sabet
Copy link
Author

Thanks, @JeremySkinner for the prompt answer. The issue is now fixed. Please close this ticket.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants