编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Fluent validation(fluentvalidation元数据)

wxchong 2024-11-05 12:36:47 开源技术 21 ℃ 0 评论

内置验证程序

FluentValidation 有几个内置的验证器,这些验证器的错误消息都可以使用特定占位符。

NotNull 验证程序

说明:确保指定的属性不是 null。

RuleFor(customer => customer.Surname).NotNull();

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{PropertyValue} = 属性的当前值

NotEmpty 验证程序

说明: 确保指定的属性不是 null、空字符串或空格 (或值类型的默认值, 例如 int 0)。

RuleFor(customer => customer.Surname).NotEmpty();

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{PropertyValue} = 属性的当前值

NotEqual 验证程序

说明: 确保指定属性的值不等于特定值 (或不等于其他属性的值)

//Not equal to a particular value

RuleFor(customer => customer.Surname).NotEqual("Foo");

//Not equal to another property

RuleFor(customer => customer.Surname).NotEqual(customer => customer.Forename);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue} = 属性不应等于的值

Equal 相等验证程序

说明: 确保指定属性的值等于特定值 (或等于另一个属性的值)

//Equal to a particular value

RuleFor(customer => customer.Surname).Equal("Foo");

//Equal to another property

RuleFor(customer => customer.Password).Equal(customer => customer.PasswordConfirmation);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue} = 属性应相等的值

{PropertyValue} = 属性的当前值

Length 长度验证程序

确保特定字符串属性的长度位于指定范围内。但是, 它不能确保字符串属性是否为 null。

RuleFor(customer => customer.Surname).Length(1, 250); //must be between 1 and 250 chars (inclusive)

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{MinLength} = 最小长度

{MaxLength} = 最大长度

{TotalLength} = 输入的字符数

{PropertyValue} = 属性的当前值

MaxLength 最大长度验证程序

说明:确保特定字符串属性的长度不超过指定的值。

RuleFor(customer => customer.Surname).MaximumLength(250); //must be 250 chars or fewer

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{MaxLength} = 最大长度

{TotalLength} = 输入的字符数

{PropertyValue} = 属性的当前值

MinLength 最小长度验证程序

说明:确保特定字符串属性的长度不能小于指定的值。

RuleFor(customer => customer.Surname).MinimumLength(10); //must be 10 chars or more

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{MinLength} = 最小长度

{TotalLength} = 输入的字符数

{PropertyValue} = 属性的当前值

LessThan 小于验证程序

说明: 确保指定属性的值小于特定值 (或小于另一个属性的值)

//Less than a particular value

RuleFor(customer => customer.CreditLimit).LessThan(100);

//Less than another property

RuleFor(customer => customer.CreditLimit).LessThan(customer => customer.MaxCreditLimit);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue}-属性比较的值

{PropertyValue} = 属性的当前值

LessThanOrEqualTo 小于等于验证程序

说明: 确保指定属性的值小于等于特定值 (或小于等于另一个属性的值)

//Less than a particular value

RuleFor(customer => customer.CreditLimit).LessThanOrEqualTo(100);

//Less than another property

RuleFor(customer => customer.CreditLimit).LessThanOrEqualTo(customer => customer.MaxCreditLimit);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue}-属性比较的值

{PropertyValue} = 属性的当前值

GreaterThan 大于验证程序

说明: 确保指定属性的值大于特定值 (或大于另一个属性的值)

//Greater than a particular value

RuleFor(customer => customer.CreditLimit).GreaterThan(0);

//Greater than another property

RuleFor(customer => customer.CreditLimit).GreaterThan(customer => customer.MinimumCreditLimit);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue}-属性比较的值

{PropertyValue} = 属性的当前值

GreaterThanOrEqualTo 大于等于验证程序

说明: 确保指定属性的值大于等于特定值 (或大于等于另一个属性的值)

//Greater than a particular value

RuleFor(customer => customer.CreditLimit).GreaterThanOrEqualTo(1);

//Greater than another property

RuleFor(customer => customer.CreditLimit).GreaterThanOrEqualTo(customer => customer.MinimumCreditLimit);

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{ComparisonValue}-属性比较的值

{PropertyValue} = 属性的当前值

Must 验证程序

描述: 将指定属性的值传递到一个委托中, 可以对该值执行自定义验证逻辑

RuleFor(customer => customer.Surname).Must(surname => surname == "Foo");

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{PropertyValue} = 属性的当前值

请注意, 委托参数不仅传递参数,还支持直接传递验证对象参数:

RuleFor(customer => customer.Surname).Must((customer, surname) => surname != customer.Forename)

正则表达式验证程序

说明: 确保指定属性的值与给定的正则表达式匹配,正则表达式可参阅正则表达式教程这篇文章

RuleFor(customer => customer.Surname).Matches("some regex here");

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{PropertyValue} = 属性的当前值

Email 电子邮件验证程序

说明: 确保指定属性的值是有效的电子邮件地址格式。

RuleFor(customer => customer.Email).EmailAddress();

可用的格式参数占位符:

{PropertyName} = 正在验证的属性的名称

{PropertyValue} = 属性的当前值

自定义验证程序

Must 验证程序

实现自定义验证程序的最简单方法是使用方法 Must 方法,假设我们有以下类:

public class Person {public IList<Person> Pets {get;set;} = new List<Person>();

}

为了确保列表中至少包含10个元素, 我们可以这样做:

public class PersonValidator:AbstractValidator<Person> {

public PersonValidator() {

RuleFor(x => x.Pets).Must(list => list.Count <= 10).WithMessage("The list must contain fewer than 10 items");

}

}

为了使这种逻辑可重用, 我们可以将其封装为扩展方法。

public static class MyCustomValidators {public static IRuleBuilderOptions<T, IList<TElement>> ListMustContainFewerThan<T, TElement>(this IRuleBuilder<T, IList<TElement>> ruleBuilder, int num) {

return ruleBuilder.Must(list => list.Count < num).WithMessage("The list contains too many items");

}

}

在这里,我们通过为 IRuleBuilder 创建扩展方法实现可重用逻辑,使用方法很简单。

RuleFor(x => x.Pets).ListMustContainFewerThan(10);

编写自定义验证程序

如果您想灵活控制可重用的验证器, 则可以使用 Must 方法编写自定义规则,此方法允许您手动创建与验证错误关联的实例。

public class PersonValidator:AbstractValidator<Person> {

public PersonValidator() {

RuleFor(x => x.Pets).Custom((list, context) => {

if(list.Count > 10) {

context.AddFailure("The list must contain 10 items or fewer");

}

});

}

}

此方法的优点是它允许您为同一规则返回多个错误。

context.AddFailure("SomeOtherProperty", "The list must contain 10 items or fewer");

// Or you can instantiate the ValidationFailure directly:

context.AddFailure(new ValidationFailure("SomeOtherProperty", "The list must contain 10 items or fewer");

自定义属性验证程序

在某些情况下, 针对某些属性的验证逻辑非常复杂, 我们希望将基于属性的自定义逻辑移动到单独的类中,可通过重写 PropertyValidator 类来完成。

using System.Collections.Generic;

using FluentValidation.Validators;

public class ListCountValidator<T> : PropertyValidator {

private int _max;

public ListCountValidator(int max)

: base("{PropertyName} must contain fewer than {MaxElements} items.") {

_max = max;

}

protected override bool IsValid(PropertyValidatorContext context) {

var list = context.PropertyValue as IList<T>;

if(list != null && list.Count >= _max) {

context.MessageFormatter.AppendArgument("MaxElements", _max);

return false;

}

return true;

}

}

继承 PropertyValidator 时, 必须重写 IsValid 方法,此方法接受一个对象, 并返回一个布尔值, 指示验证是否成功,可通过 PropertyValidatorContext 属性访问:

Instance-正在验证的对象

PropertyDescription-属性的名称 (或者是由调用 WithName 的自定义的别名)

PropertyValue-正在验证的属性值

Member-描述正在验证的属性的 MemberInfo 信息

若要使用自定义的属性验证程序, 可以在定义验证规则时调用:

public class PersonValidator : AbstractValidator<Person> {

public PersonValidator() {

RuleFor(person => person.Pets).SetValidator(new ListCountValidator<Pet>(10));

}

}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表