Could not infer property name using generic list with the newest FluentValidation version.
See original GitHub issueHi,
I just upgrade our fluent validation version to be able to use new features (which are awesome). However, I’m facing an issue with existing validations we have in our system.
We are not using a conventional model in our current framework. We have a global class Entity
that defines an entity and the global property. This class contains a list of children List<Entity>
which can contains multiple type of entities. Because of this, we had to hack the way fluentValidation works and use the OfType
on our global list to pass the right details to our validator. I wrote a sample of code below to demonstrate how we did it.
This was previously working and is not working anymore. The error that we have now is the following one:
Could not infer property name for expression: p => new <>f__AnonymousType0`1(F2s = p.Foos.OfType()).F2s. Please explicitly specify a property name by calling OverridePropertyName as part of the rule chain.
// Abstract Parent
public abstract class MasterFoo
{
public IEnumerable<MasterFoo> Foos {get;set;}
}
// Parent Class
public class F1 : MasterFoo
{
public IEnumerable<F2> F2s => Details.OfType<F2>();
}
// Child Class
public class F2 : MasterFoo{...}
public class F3 : MasterFoo{...}
// Parent that contains 2 type of childs.
F1 parent = new F1();
F2 child1 = new F2();
F3 child2 = new F3();
parent.AddChild(child1)
parent.AddChild(child2)
// Parent validator
public class F1Validator : AbstractValidator<F1>
{
public F1Validator
{
// Does work
RuleFor(p => p.F2s).SetCollectionValidator(new F2Validator());
// Does not work (Inferring issue)
RuleFor(p => new { F2s = p.Foos.OfType<F2>() }.F2s).SetCollectionValidator(new F2Validator());
// Does not work (Inferring issue)
RuleFor(p => p.Foos.OfType<F2>()).SetCollectionValidator(new F2Validator());
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Actually I just tried this and it seems to work OK. You can set something like this in your application’s startup routine:
Hi,
This is the expected behaviour. By default, a call to
RuleFor
expects a lambda expression that defines a simple property accessor. If you’re doing anything more complex inside a call to RuleFor (including anything that performs method calls toOfType
, or any other method call), then FluentValidation won’t automatically infer the property name, so you need to tell it yourself the name of the property with which the expression is associated. The error message does actually explain how to do this pretty clearly:Could not infer property name for expression: p => new <>f__AnonymousType0`1(F2s = p.Foos.OfType()).F2s. Please explicitly specify a property name by calling OverridePropertyName as part of the rule chain.
So just do as it suggests and add a call to OverridePropertyName:
This may not have been necessary in some older version of FluentValidation, but since expression caching was introduced in 7.x (for performance reasons) this is a requirement if you are not using simple property accessors.
Hope that helps