Click here to Skip to main content
15,886,766 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Hi guys, i have a problem about FluentValidation. I am taking this error when i am starting app:
System.InvalidCastException: 'Unable to cast object of type 'DevFramework.Northwind.Entities.Concrete.Product' to type 'FluentValidation.IValidationContext'.'


Broken Fluent Validation Code:

C#
using FluentValidation;



namespace DevFramework.Core.CrossCuttingConcerns.Validation.FluentValidation
{
    public class ValidatorTool
    {
        
        public static void FluentValidate(IValidator validator, object entity)
        {
            var result = validator.Validate((IValidationContext)entity);
            if (result.Errors.Count > 0)
            {
                throw new ValidationException(result.Errors);
            }
        }
    }
}


-----------------------------------------------------------------------
FluentValidationAspect(I am using postsharp) which i used for:
C#
[FluentValidationAspect(typeof(ProductValidator))]
[CacheRemoveAspect(typeof(MemoryCacheManager))]
[LogAspect(typeof(FileLogger))]
public Product Add(Product product)
{

    return _productDal.Add(product);
}



Thanks!

What I have tried:

I've tried this code:
C#
public class ValidatorTool
    {
        
        public static void FluentValidate(IValidator validator, object entity)
        {
            var result = validator.Validate(entity);
            if (result.Errors.Count > 0)
            {
                throw new ValidationException(result.Errors);
            }
        }
    }


and I took this error:
cannot convert from 'object' to 'FluentValidation.IValidationContext'  DevFramework.Core   17  Active
Posted
Updated 15-Jul-20 0:30am
v2
Comments
[no name] 14-Jul-20 12:45pm    
Can you show ProductValidator class?
||Memo|| 14-Jul-20 18:28pm    
public class ProductValidator:AbstractValidator<product>
{
public ProductValidator()
{
RuleFor(p => p.CategoryID).NotEmpty().WithMessage("Category Id cannot be empty");
RuleFor(p => p.ProductName).NotEmpty().WithMessage("Product name cannot be empty");
RuleFor(p => p.UnitPrice).GreaterThan(0).WithMessage("Unit Price cannot be empty and must be greater than 0");
RuleFor(p => p.QuantityPerUnit).NotEmpty().WithMessage("Category Id cannot be empty");
RuleFor(p => p.ProductName).Length(2, 20).WithMessage("Product Name length must be between 2 and 20");
RuleFor(p => p.UnitPrice).GreaterThan(20).When(p => p.CategoryID == 1).WithMessage("Prices cannot be lower than 20 for member of this category");

}
BillWoodruff 14-Jul-20 12:59pm    
You appear to be using a FrameWork that is no longer supported; their website has no content.
||Memo|| 14-Jul-20 18:29pm    
I'm using EntityFramework. App is working but FluentValidation does not work correctly. I can return get all method.
BillWoodruff 14-Jul-20 19:26pm    
Your code shows an error in the DevFrameWork.

Guys i found the solution.
C#
public class ValidatorTool
    {
        
        public static void FluentValidate(IValidator validator, object entity)
        {
            var context = new ValidationContext<object>(entity);
            var result = validator.Validate(context); 
            if (result.Errors.Count > 0)
            {
                throw new ValidationException(result.Errors);
            }
        }
    }

FluentValidation has removed Non-Generic validate overload for security with 9.0 update.
Here is the update link 9.0 Upgrade Guide — FluentValidation documentation[^]
 
Share this answer
 
Comments
yasin erdem 20-Jan-22 17:53pm    
Thanks A Lot
The error message is quite clear - your Product class does not implement the IValidationContext interface. It looks like you're passing in the wrong value to your ValidatorTool.FluentValidate method.

This appears to be the same code that was posted to the GitHub repo back in 2018:
Validate method is throwing exception · Issue #763 · FluentValidation/FluentValidation · GitHub[^]
 
Share this answer
 
Comments
||Memo|| 15-Jul-20 6:23am    
Thanks dude i asked jeremy my issue. And He answered just now. I'll share the solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900