-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
FluentValidation version
10.0.4
ASP.NET version
.NET Core 3.1
Summary
I have a rulebuilder for a IList typed property with three rules: notEmpty, foreach and mustAsync. The problem is that the foreach rule returns a rulebuilder with out type 'IEnumerable' but the next rule seems to expect the original property type IList since there is an invalid cast exception from ...IList.. to ..IEnumerable..
This rule did not give any exception in FluentValidation version 9.3.0.
Steps to Reproduce
The code
Dto to validate
public class SaveRegistrationRequestForHardwareUnit : CommandWithTransactionToggle<SuccessOrFailureDto>
{
//other props
public IList<KeyValuePair<Guid,string>> HardwareUnitRoles { get; set; }
}
Validator
public class SaveRegistrationRequestForHardwareUnitValidator : AbstractValidator<SaveRegistrationRequestForHardwareUnit>
{
private const string ValidRoleConfigMessage = "ValidRoleConfigMessage";
public SaveRegistrationRequestForHardwareUnitValidator(IValidator<UserDto> userDtoValidator)
{
//Other rules
RuleFor(r => r.HardwareUnitRoles).Cascade(CascadeMode.Stop)
.NotEmpty()
.ForEach(r => r
.Must(kv => HardwareUnitUserRoles.IsValid(kv.Value))
.WithMessage(m => Resources.ValidationMessages.hardwareunit_role_notvalid)
.Must(kv => kv.Value != HardwareUnitUserRoles.Owner)
.WithMessage(m => Resources.ValidationMessages.registrationrequest_hardwareUnitRole_notallowed)
)
.MustAsync((roles, token) => ValidRoleConfigAsync(roles))//GIVES EXCEPTION
.WithMessage(dto => ValidationErrorMessages.GetErrorMessage(ValidRoleConfigMessage));
}
private async Task<bool> ValidRoleConfigAsync(IEnumerable<KeyValuePair<Guid, string>> hardwareUnitRoles)
{
//logic
}
}
Exception:
System.InvalidCastException: Unable to cast object
of type 'FluentValidation.Internal.RuleBuilder`2[Foo.Commands.Registration.SaveRegistrationRequestForHardwareUnit,System.Collections.Generic.IList`1[System.Collections.Generic.KeyValuePair`2[System.Guid,System.String]]]'
to type 'FluentValidation.Internal.RuleBuilder`2[Foo.Commands.Registration.SaveRegistrationRequestForHardwareUnit,System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Guid,System.String]]]'.