InvalidOperationException when extending WithState to accept TProperty parameter
See original GitHub issueI have a case where I am using a RuleForEach along with a .WithState to loop over the rule and have Fluent Validation create a new failure (and thus a separate CustomState object) for each failure found in the loop. I need access to TProperty in the stateProvider parameter in order for my logic to work. I went ahead and created my own version of WithState with a stateProvider parameter that also has the TProperty parameter in addition to the T parameter. Below is my current implementation:
public static IRuleBuilderOptions<T, TProperty> WithState<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, Func<Tuple<T, TProperty>, object> stateProvider)
{
if (stateProvider == null)
{
throw new ArgumentNullException("A lambda expression must be passed to WithState");
}
return rule.Configure(config =>
{
config.CurrentValidator.CustomStateProvider = x => (stateProvider((Tuple<T, TProperty>)x));
});
}
When I execute a RuleForEach with this modified WithState version I get the following exception thrown: “Message”: “An error has occurred.”, “ExceptionMessage”: “Could not apply an Indexer because the property chain is empty.”, “ExceptionType”: “System.InvalidOperationException”, “StackTrace”: " at FluentValidation.Internal.PropertyChain.AddIndexer(Object indexer) in C:\projects\FluentValidation\src\FluentValidation\Internal\PropertyChain.cs:line 99
What am I doing wrong?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I suspect it’s because the RuleForEach is being called on a method rather than a property. I’m not at a computer to test it, but could you try calling OverridePropertyName on the RuleForEach passing in a temporary name and see if that makes a difference?
I appreciate the information. I will close this issue as no further information on this topic is required.