流畅验证只检查ID属性



我在我的项目中使用流畅验证。

我有一个验证器类。

public class CarValidator : AbstractValidator<Car>
{
public CarValidator()
{
RuleFor(p => p.Id).NotEmpty();
RuleFor(p => p.Name).MinimumLength(2);
RuleFor(p => p.DailyPrice).GreaterThanOrEqualTo(0);
RuleFor(p => p.ColorId).NotEmpty();
RuleFor(p => p.BrandId).NotEmpty();
RuleFor(p => p.DailyPrice).GreaterThanOrEqualTo(0);
RuleFor(p => p.ModelYear).GreaterThanOrEqualTo(1950);
RuleFor(p => p.Name).NotEmpty();
RuleFor(p => p.Description).NotEmpty().MinimumLength(10);

}
}

控制器类中的Delete方法

[ValidationAspect(typeof(CarValidator))]
public IResult Delete(Car car)
{
_carDal.Delete(car);
return new SuccessResult(Messages.CarDeleted);
}

邮差响应体

FluentValidation.ValidationException: Validation failed: 
-- Id: 'Id' must not be empty.
-- ColorId: 'Color Id' must not be empty.
-- BrandId: 'Brand Id' must not be empty.
-- ModelYear: 'Model Year' must be greater than or equal to '1950'.
-- Name: 'Name' must not be empty.
-- Description: 'Description' must not be empty.

当我向delete方法发出请求时,所有属性都被检查。但是我只想检查Id ?

您可以参考Fluentvalidation文档中的Validator Customization部分。

添加CustomizeValidator属性设置为Id财产。

public IResult Delete([CustomizeValidator(Properties="Id")] Car car)
{
_carDal.Delete(car);
return new SuccessResult(Messages.CarDeleted);
}

最新更新