Upgrade Solution to use FluentValidation Ver 10 Exception Issue

0

Please I need your help to solve FluentValidation issue. I have an old desktop application which I wrote a few years ago. I used FluentValidation Ver 4 and Now I'm trying to upgrade this application to use .Net framework 4.8 and FluentValidation Ver 10, but unfortunately, I couldn't continue because of an exception that I still cannot fix.

I have this customer class:

    class Customer : MyClassBase
    {
        string _CustomerName = string.Empty;
        public string CustomerName
        {
            get { return _CustomerName; }
            set
            {
                if (_CustomerName == value)
                    return;

                _CustomerName = value;
            }
        }


        class CustomerValidator : AbstractValidator<Customer>
        {
            public CustomerValidator()
            {
                RuleFor(obj => obj.CustomerName).NotEmpty().WithMessage("{PropertyName} is Empty");
            }
        }

        protected override IValidator GetValidator()
        {
            return new CustomerValidator();
        }

    }

This is my base class:

    class MyClassBase
    {
        public MyClassBase()
        {
            _Validator = GetValidator();
            Validate();
        }

        protected IValidator _Validator = null;

        protected IEnumerable<ValidationFailure> _ValidationErrors = null;

        protected virtual IValidator GetValidator()
        {
            return null;
        }
        public IEnumerable<ValidationFailure> ValidationErrors
        {
            get { return _ValidationErrors; }
            set { }
        }

        public void Validate()
        {
            if (_Validator != null)
            {
                var context = new ValidationContext<Object>(_Validator);
                var results = _Validator.Validate(context); **// <======= Exception is here in this line**
                _ValidationErrors = results.Errors;
            }
        }

        public virtual bool IsValid
        {
            get
            {
                if (_ValidationErrors != null && _ValidationErrors.Count() > 0)
                    return false;
                else
                    return true;
            }
        }

    }

When I run the application test I get the below exception:

System.InvalidOperationException HResult=0x80131509 Message=Cannot validate instances of type 'CustomerValidator'. This validator can only validate instances of type 'Customer'. Source=FluentValidation StackTrace: at FluentValidation.ValidationContext1.GetFromNonGenericContext(IValidationContext context) in C:\Projects\FluentValidation\src\FluentValidation\IValidationContext.cs:line 211 at FluentValidation.AbstractValidator1.FluentValidation.IValidator.Validate(IValidationContext context)

Please, what is the issue here and How can I fix it?

Thank you

fluentvalidation
asked on Stack Overflow May 2, 2021 by user3885917 • edited May 5, 2021 by rgvlee

1 Answer

0

Your overall implementation isn't what I'd consider normal usage however the problem is that you're asking FV to validate the validator instance, rather than the customer instance:

var context = new ValidationContext<Object>(_Validator);
var results = _Validator.Validate(context);

It should start working if you change it to:

var context = new ValidationContext<object>(this);
var results = _Validator.Validate(context);

You're stuck with using the object argument for the validation context unless you introduce a generic argument to the base class, or create it using reflection.

answered on Stack Overflow May 3, 2021 by rgvlee

User contributions licensed under CC BY-SA 3.0