Enable interception of model type used to fetch validators (for proxy type scenarios)
See original GitHub issueEditContextFluentValidationExtensions.GetValidatorForModel
uses model.GetType()
to determine what validator to fetch from the service provider. The majority of my view models are proxied at runtime and so model.GetType()
will return the runtime-generated proxy type, not the underlying compile-time type being proxied. The result is that validators will sometimes not be found.
My proposal is to add a new parameter to EditContextFluentValidationExtensions.AddFluentValidation
(in a non-breaking fashion).
public delegate Type InterceptEditContextModelTypeDelegate(object model);
public static EditContext AddFluentValidation(this EditContext editContext, IServiceProvider serviceProvider, bool disableAssemblyScanning, IValidator validator, FluentValidationValidator fluentValidationValidator)
=> editContext.AddFluentValidation(serviceProvider, disableAssemblyScanning, validator, fluentValidationValidator, model => model.GetType());
public static EditContext AddFluentValidation(this EditContext editContext, IServiceProvider serviceProvider, bool disableAssemblyScanning, IValidator validator, FluentValidationValidator fluentValidationValidator, InterceptEditContextModelTypeDelegate modelTypeInterceptor)
{
...
}
private static IValidator GetValidatorForModel(IServiceProvider serviceProvider, object model, bool disableAssemblyScanning, InterceptEditContextModelTypeDelegate modelTypeInterceptor)
{
var modelType = modelTypeInterceptor.Invoke(model);
...
}
I can submit a PR for this later.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Proxy - JavaScript - MDN Web Docs
The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for that object.
Read more >Build an HTTPS-intercepting JavaScript proxy in 30 ...
Automatically validate and debug HTTP interactions across an entire system. It turns out setting this up is super quick & easy to do....
Read more >How JavaScript's Proxy Object Works
In this tutorial, you are going to learn what a proxy object is, along with its limitations. We will also look into some...
Read more >Cypress cy.intercept Problems
Let's say the application is making GET /todos call to load its data. We might write a test like this to spy on...
Read more >Chapter 6. Aspect Oriented Programming with Spring
To use @AspectJ aspects in a Spring configuration you need to enable Spring support for configuring Spring AOP based on @AspectJ aspects, and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Documentation added to the existing PR
Actually, this scenario would be very well covered by #88 - ValidatorFactory callback which returns directly IValidator instance.
Advantage of ValidatorFactory is that it cover much more scenarios. For example you might used different validators for the same model type, you can use your own validator discovery logic, etc…
In fact, existing AssemblyScanning and ServiceProvider activation could be refactored as a ValidatorFactory callbacks.