Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Minor FluentValidation integration improvements #35

Closed
JeremySkinner opened this issue Aug 18, 2020 · 1 comment
Closed

Minor FluentValidation integration improvements #35

JeremySkinner opened this issue Aug 18, 2020 · 1 comment

Comments

@JeremySkinner
Copy link

JeremySkinner commented Aug 18, 2020

Hi @ryanelian,

I was looking through your library and noticed a couple of minor things that you may want to consider for better integration with FluentValidation.

  • If you wanted to support InjectValidator, then you could pass the service provider into the ValidationContext
  • FluentValidator.CreateValidationContext could be simplified so you don't have to use reflection.
private IValidationContext CreateValidationContext(object model, IValidatorSelector validatorSelector = null)
{
	if (validatorSelector == null) 
	{
		// No selector specified - use the default.
		validatorSelector = ValidatorOptions.Global.ValidatorSelectors.DefaultValidatorSelectorFactory()
	}

        // Don't need to use reflection to construct the context. 
        // If you create it as a ValidationContext<object> instead of a ValidationContext<T> then FluentValidation will perform the conversion internally, assuming the types are compatible. 
	var context = new ValidationContext<object>(model, new PropertyChain(), validatorSelector);
        // InjectValidator looks for a service provider inside the ValidationContext with this key. 
	context.RootContextData["_FV_ServiceProvider"] = ServiceProvider;
	return context;
}

Hope that's useful

@ryanelian
Copy link
Owner

Thank you for the guidance!

I will implement your suggestion post-haste.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants