0
[Validator(typeof(ProductDetailsRequestDTO))]
public class ProductDetailsRequestDTO
{
public int ArticleGroup { get; set; }
public DateTime ProducedAt { get; set; }
}
public class ProductDetailsRequestDTOValidator : AbstractValidator<ProductDetailsRequestDTO>
{
public ProductDetailsRequestDTOValidator()
{
RuleFor(r => r.ArticleGroup).NotEmpty().WithMessage("custom message");
RuleFor(r => r.ProducedAt).NotEmpty().WithMessage("custom message");
}
}
// FluentValidation setup
config.Services.Add(typeof(System.Web.Http.Validation.ModelValidatorProvider), new FluentValidationModelValidatorProvider());
fluentvalidation從哪裏知道我創建的驗證器以及爲什麼我的模型始終爲真?如何用Fluent驗證代替默認的網頁api模型驗證
也許是因爲這兩個類型的具有非空默認值。所以'int'總是用'0'實例化。它不是可以空的類型。與'DateTime'一樣。你有沒有試過說'.MoreThan(0)'或者等價的。 –
MoreThan在我的智能感知中不存在。從文檔:NotEmpty驗證程序 說明:確保指定的屬性不爲空,空字符串或空白(或值類型的默認值,例如0代表int) – Pascal
Your Model有2個屬性,它們是整數和DateTime類型,它們不能清空,默認的整數是0,DateTime是MinValue的Date –