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.

FluentValidation 9 ChildValidatorAdaptor Upgrade guide

See original GitHub issue

FluentValidation 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wastcommented, Jul 28, 2020

It works. Thank you!

1reaction
JeremySkinnercommented, Jul 28, 2020

That implementation I linked to definitely doesn’t work, here’s an updated version that handles the type conversion correctly:

public class PolymorphicValidator<T, TInterface> : ChildValidatorAdaptor<T, TInterface>
{
	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<TInterface>)null, typeof(IValidator<TInterface>))
	{
	}

	public PolymorphicValidator<T, TInterface> Add<TDerived>(IValidator<TDerived> derivedValidator) where TDerived : TInterface {
		_derivedValidators[typeof(TDerived)] = derivedValidator;
		return this;
	}

	public override IValidator<TInterface> 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 new ValidatorWrapper(derivedValidator);
		}

		return null;
	}

	private class ValidatorWrapper : AbstractValidator<TInterface> {

		private IValidator _innerValidator;
		public ValidatorWrapper(IValidator innerValidator) {
			_innerValidator = innerValidator;
		}

		public override ValidationResult Validate(ValidationContext<TInterface> context) {
			return _innerValidator.Validate(context);
		}

		public override Task<ValidationResult> ValidateAsync(ValidationContext<TInterface> context, CancellationToken cancellation = new CancellationToken()) {
			return _innerValidator.ValidateAsync(context, cancellation);
		}

		public override IValidatorDescriptor CreateDescriptor() {
			return _innerValidator.CreateDescriptor();
		}
	}
}

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?

Read more comments on GitHub >

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

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