Hi, I maintain the FluentValidation project which contains integration components with AspNetCore.
Unfortunately 2.1 preview 1 breaks this integration. In previous versions, it was possible to implement a custom IObjectModelValidator. This worked as the entry point for the validation process where I could then customize the validation behaviour by providing a custom ValidationVisitor (as custom ModelValidatorProviders do not provide enough extensibility).
In the 2.1-preview1 release it seems that the IObjectModelValidator is never resolved from the service provider anymore, and instead validation is kicked off by the ParameterBinder class instead. Unfortunately the EnforceBindRequiredAndValidate method is private, so I can't just override this method to supply my own ValidationVisitor, and instead have to override the BindModelAsync method and duplicate all the code in here.
1) Please can ParameterBinder.EnforceBindRequiredAndValidate be made virtual? (Or alternatively, introduce a new protected method called CreateAndInvokeValidationVisitor or something like that which is then called by ParameterBinder.EnforceBindRequiredAndValidate)
2) Is IObjectModelValidator not supposed to be used anymore? If so, should it be marked as obsolete?
3) Is there any documentation on when the new ModelMetadataKind.Parameter is used vs ModelMetadataKind.Type?
Also could you provide some background information on this change? (Or point me to a previous discussion if there was one that I missed)
Thanks
//cc @rynowak
Thanks for contacting us. One of our team members will look into it and get back to you.
@dougbu, can you please look into this? Thanks!
@JeremySkinner this is indeed a significant breaking change. The IObjectModelValidator implementation will still be used in methods like ControllerBase.TryUpdateModelAsync(...) or when a ParameterBinder is manually created (using an [Obsolete] constructor).
We'll likely create an abstract implementation of IObjectModelValidator, add new methods to it, and restore the interface's use in ParameterBinder but special-case the abstract class. For fluent validation, you'll be able to choose between implementing the interface (which will lose support for ValidationAttributes on top-level models e.g. parameters) or subclassing the class.
Clearing my assignment to bring this to @mkArtakMSFT and @danroth27's attention. I suggest we correct this issue in Preview2.
Thanks, that鈥檚 very helpful to know. For now I鈥檒l maintain both a subclass of ParameterBinder and the IObjectModelValidator implementation.
I鈥檒l look forward to seeing the updated api.
Thanks @dougbu!
@kichalla, can you please look into this when you'll be back? Thanks!
@JeremySkinner Any chance for a preview package including the (temporary?) fix?
@BladeWise Not for preview1 - will be aiming for preview2. Please follow this issue if you want to track progress. https://github.com/JeremySkinner/FluentValidation/issues/698