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.

Enable interception of model type used to fetch validators (for proxy type scenarios)

See original GitHub issue

EditContextFluentValidationExtensions.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:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanMarcottecommented, Jun 18, 2021

Documentation added to the existing PR

0reactions
Lierocommented, Oct 1, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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