说明:确保指定的属性不是 null。 RuleFor(customer=>customer.Surname).NotNull(); 可用的格式参数占位符: {PropertyName} = 正在验证的属性的名称 {PropertyValue} = 属性的当前值 NotEmpty 验证程序 说明: 确保指定的属性不是 null、空字符串或空格 (或值类型的默认值, 例如 int 0)。 RuleFor(customer=>c...
1.NotNull:确保属性的值不是Null,例如代码: 1 RuleFor(customer => customer.Surname).NotNull(); 2.NotEmpty:确保属性值不是Null,不为空,或者空白字符串,相当于String的IsNullOrWhiteSpace方法 1 RuleFor(customer => customer.Surname).NotEmpty(); 上面应该是最简单也是最常用的吧,毕竟验证主要是空值相关的。
{//设置 FluentValidation 默认的资源文件提供程序 - 中文资源ValidatorOptions.ResourceProviderType =typeof(FluentValidationResource);/*比如验证用户名 not null、not empty、length(2,int.MaxValue) 时,链式验证时,如果第一个验证失败,则停止验证*/ValidatorOptions.CascadeMode= CascadeMode.S...
1.1 空值验证:NotNull,NotEmpty 1.NotNull:确保属性的值不是Null,例如代码: 1 RuleFor(customer => customer.Surname).NotNull(); 2.NotEmpty:确保属性值不是Null,不为空,或者空白字符串,相当于String的IsNullOrWhiteSpace方法 1 RuleFor(customer => customer.Surname).NotEmpty(); 上面应该是最简单也是最常用...
.When(x => x.Name != null) .InvalidWhen(x => !string.IsNullOrEmpty(x.Name)) .WithMessage("ID must be greater than 0 when Name is not null or empty."); RuleFor(x => x.Name) .NotNull() .WithMessage("Name is required."); ...
内置规则方法已经非常丰富: NotEmpty 、 NotEqual 、 Length 等,也可以使用 Must 进行自定义设置; 可以用 When 设定规则验证的前提; 默认常见内置规则,都有统一内置的验证不通过的消息;可通过 WithMessage 设置独立的验证不通过的消息; FluentValidation 的相关包 ...
list cannot be null or empty (i.e. at least 1 user is requiredRuleFor(list=>list).NotEmpty...
.NotNull() .When(p => (DateTime.Now > p.Birthday.AddYears(1))) .WithMessage(p => $"出生日期为{p.Birthday},现在时间为{DateTime.Now},大于一岁,CardID值必填!") .NotEmpty() .When(p => (DateTime.Now > p.Birthday.AddYears(1))) ...
NotNull/NotEmpty Matches (regex) InclusiveBetween (range) CreditCard Email EqualTo (cross-property equality comparison) MaxLength MinLength Length To enable clientside integration you need to install theFluentValidation.AspNetCorepackage and call theAddFluentValidationClientsideAdaptersin your application start...
//Ensures that the specified property is not null, an empty string or whitespace (or the default value for value types, eg 0 for int) RuleFor(customer => customer.Surname).NotEmpty(); //Not equal to a particular value RuleFor(customer => customer.Surname).NotEqual("Foo"); ...