question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidOperationException when extending WithState to accept TProperty parameter

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Apr 7, 2017

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?

0reactions
ghostcommented, Apr 11, 2017

I appreciate the information. I will close this issue as no further information on this topic is required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript extend parameter type - reactjs
1 Answer. The generic type parameter should be on the interface and the component, not the function. If you have a generic function...
Read more >
Spawning - Photon Fusion
On the server, in hosted or client/server mode, ownership (State Authority) of the spawned object is assigned to the server, and the object...
Read more >
Changelog | Input System | 1.5.1
Fixed an InvalidOperationException when using Hold interaction, and by extension any interaction that changes to performed state after a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found