Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
RyanMarcotte opened this issue Jun 17, 2021 · 5 comments
Labels
Feature New feature that will be added to the project
Milestone

Comments

@RyanMarcotte
Copy link

RyanMarcotte commented Jun 17, 2021

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.

@chrissainty
Copy link
Member

Hi Ryan, thanks for raising this. It seems like a good suggestion. I've just had a quick look at your PR as well and it looks good I think we can get this feature in.

@chrissainty chrissainty added Feature New feature that will be added to the project and removed Feature Request Request to add a new feature Triage Issue needs to be triaged labels Jun 18, 2021
@RyanMarcotte
Copy link
Author

Awesome! I can add some documentation to the README. Would you prefer that be done as part of the PR or after a new version of the package is released?

@chrissainty
Copy link
Member

That would be fantastic! Could you add it as part of the existing PR.

@RyanMarcotte
Copy link
Author

RyanMarcotte commented Jun 18, 2021

Documentation added to the existing PR

@Liero
Copy link

Liero commented 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.

@chrissainty chrissainty added this to the vNext milestone Jan 21, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Feature New feature that will be added to the project
Projects
None yet
Development

No branches or pull requests

3 participants