FluentValidation 9 ChildValidatorAdaptor Upgrade guide
See original GitHub issueFluentValidation 9 NET Core 3.1
I can’t get new ChildValidatorAdaptor working. Can you provide more details on how to use those generic parameters? My old method:
This is code from version 8 which works:
public class PolymorphicValidator<TInterface> : ChildValidatorAdaptor
{
readonly Dictionary<Type, IValidator> _derivedValidators = new Dictionary<Type, IValidator>();
// Need the base constructor call, even though we're just passing null.
public PolymorphicValidator() : base((IValidator)null, typeof(IValidator<TInterface>))
{
}
public PolymorphicValidator<TInterface> Add<TDerived>(IValidator<TDerived> derivedValidator) where TDerived : TInterface
{
_derivedValidators[typeof(TDerived)] = derivedValidator;
return this;
}
public override IValidator GetValidator(PropertyValidatorContext context)
{
// bail out if the current item is null
if (context.PropertyValue == null) return null;
if (_derivedValidators.TryGetValue(context.PropertyValue.GetType(), out var derivedValidator))
{
return derivedValidator;
}
return null;
}
}
`
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
9.0 Upgrade Guide — FluentValidation documentation
FluentValidation 9.0 is a major release that included several breaking changes. Please review this document before upgrading from FluentValidation 8.x to 9.
Read more >Using ChildValidatorAdaptor in version 9.x · Issue #1526
Now i have updated the library to 9.x and found the breaking changes, this led me to the documentation post of upgrade guide:...
Read more >FluentValidation changelog - Awesome .NET - LibHunt
FluentValidation 9 is a major release and includes several breaking changes. Please read the upgrade guide for full details ...
Read more >FluentValidation 9 released - Jeremy Skinner
FluentValidation 9 is now available. ... Please read the upgrade guide and the changelog for full details, but here are a few things...
Read more >PropertyValidatorContext, FluentValidation.Validators C# ...
Validators PropertyValidatorContext - 46 examples found. These are the top rated real world C# (CSharp) examples of FluentValidation.Validators.
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
It works. Thank you!
That implementation I linked to definitely doesn’t work, here’s an updated version that handles the type conversion correctly:
We’ll probably come up with a slightly better solution for when this is properly added to the library, but this should work for now.
Could you give that a try and let me know if it works ok?