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.

ParentContext is not accessible unless casting ValidationContext<T> to IValidationContext

See original GitHub issue

FluentValidation version

10.0.0

ASP.NET version

.NET 5

Summary

The example given in https://docs.fluentvalidation.net/en/latest/advanced.html#root-context-data does not seem to work. I have a possible workaround but I am not sure if it is appropriate or will even work.

Using the example in the link above results in:

image

Exact error message: error CS1061: 'ValidationContext<MyViewModel>' does not contain a definition for 'ParentContext' and no accessible extension method 'ParentContext' accepting a first argument of type 'ValidationContext<MyViewModel>' could be found (are you missing a using directive or an assembly reference?)

Possible workaround(?)

RuleFor(x => x.Surname).Custom((x, context) => {
    if ((context as IValidationContext).ParentContext.RootContextData.ContainsKey("MyCustomData"))
    {
        context.AddFailure("My error message");
    }
});

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Apr 6, 2021

Thanks, I’ve updated the docs, they should publish momentarily.

1reaction
JeremySkinnercommented, Apr 6, 2021

Yep, it’s the same instance used everywhere. It’s intended to be a way to share data that you can access anywhere in the validation process.

Should ParentContext be accessible at all in V10 now?

No, you shouldn’t need to use it. In 9.x you had to use context.ParentContext as context is a PropertyValidatorContext, and ParentContext is a ValidationContext<T> but in 10.0 the ValidationContext<T> Is passed in directly and we don’t use PropertyValidatorContext anymore.

There’s a section in the upgrade guide that mentions that various places now take a ValidationContext instead of a PropertyValidatorContext, I’ll try and make it clearer that this also means you don’t need to access ParentContext anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using IActionContextValidatorInterceptor to examine/ ...
I want to validate/display my own message for type conversion errors via FV and not have MVC providing any type conversion errors so...
Read more >
I am having a problem to use to object with Fluentvalidation
You can resolve it by providing the IValidationContext instance. var context = new ValidationContext<object>(model); var result = validator.
Read more >
[Solved] C# --fluentvalidation | system.invalidcastexception
InvalidCastException : 'Unable to cast object of type 'DevFramework.Northwind.Entities.Concrete.Product' to type 'FluentValidation.
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