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

ChildRules with class hierarchies broken #2165

Closed
runehalfdan opened this issue Oct 11, 2023 · 3 comments
Closed

ChildRules with class hierarchies broken #2165

runehalfdan opened this issue Oct 11, 2023 · 3 comments
Milestone

Comments

@runehalfdan
Copy link

FluentValidation version

11.7.1

ASP.NET version

No response

Summary

#2097 (Fix for multi-level child-rules) broke our validation code. We get exception Unable to cast object of type FluentValidation.Internal.RuleBuilder2[Root, Bar] to type FluentValidation.Internal.RuleBuilder2[Root, Base] when setting up validation in sample below.

It worked with 11.5.1, but broke when we upgraded to 11.7.1

Steps to Reproduce

using FluentValidation;

public class Root
{
    public Bar Data {get; set;} = null;
}

public class Base 
{
    public int Value {get; set;}
}

public class Bar : Base
{
    public int BarValue {get; set;}
}

public class RootValidator : AbstractionValidator<Root>
{
    public RootValidator()
    {
        RuleFor(x => x).ChildRules(RootRules);
    }

    public static Action<InlineValidator<Base>> BaseRules()
    {
        return rules => {
            rules.RuleFor(x => x.Value).NotEqual(-1);
        };
    }

    public static Action<InlineValidator<Root>> RootRules()
    {
        return rules => {
            rules.RuleFor(x => x.Data).ChildRules(BaseRules());
        };
    }
}

public static class Program
{
    public static void Main()
    {
        var validator = new RootValidator();
        var result = validator.Valdiate(new Root());
    }
}
@runehalfdan
Copy link
Author

We have worked around it for now, by being more explicit about types in the various rule-builders (either just using the most specific type or using constrained generics). But is very troublesome that this causes runtime exceptions instead compile-time errors.

@JeremySkinner
Copy link
Member

Fixed in a437761 which will be part of the 11.8 release.

@JeremySkinner JeremySkinner added this to the 11.8 milestone Oct 11, 2023
@JeremySkinner
Copy link
Member

11.8 is released with this change

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2023
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