验证器未应用于子属性

时间:2017-08-08 09:08:03

标签: c# fluentvalidation

我正在创建if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); String cameraId = null; // Usually back camera is at 0 position. try { cameraId = camManager.getCameraIdList()[0]; camManager.setTorchMode(cameraId, false); // TODO Turn ON Flash Light On mScannerView.setFlash(true); } catch (CameraAccessException e) { e.printStackTrace(); } } API,使用POSTOrder作为输入。此[FromBody]具有Order属性,并且正在使用Buyer验证这两个属性。以下是课程:

FluentValidation

这些类的验证器:

[Validator(typeof(OrderValidator))]
public class Order
{
    public int ProductId { get; set; }
    public Buyer Buyer { get; set; }
}

[Validator(typeof(BuyerValidator))]
public class Buyer
{
    public string Name { get; set; }
    public string Email { get; set; }
}

在这里,我在两个类上使用public class OrderValidator : AbstractValidator<Order> { public OrderValidator() { RuleFor(x => x.ProductId).GreaterThan(0); RuleFor(x => x.Buyer).NotNull(); } } public class BuyerValidator : AbstractValidator<Buyer> { public BuyerValidator() { RuleFor(x => x.Name).Length(3, 100); RuleFor(x => x.Email).Length(5, 100); } } 属性应用验证器。它在Validator类上正常工作,但未在Order类上执行。我是否必须以其他方式为Buyer类设置验证器?

1 个答案:

答案 0 :(得分:1)

像这样扩展买家验证:

public class OrderValidator : AbstractValidator<Order>
{
    public OrderValidator()
    {
        RuleFor(x => x.ProductId).GreaterThan(0);
        RuleFor(x => x.Buyer).NotNull().SetValidator(new BuyerValidator())
    }
}

未经过真实测试,可在文档here

中找到