对于自定义验证,你可以使用Must或Custom方法。 使用Must方法: Must方法接受一个lambda表达式和一个错误消息。如果lambda表达式返回false,则会触发验证失败,并显示指定的错误消息。 csharp RuleFor(u => u.Username) .Must(BeAValidUsername) .WithMessage("Invalid username."); private bool BeAValidUsername(...
子规则不允许您传入父对象。Must我认为Custom(通过context.ParentContext.InstanceToValidate也许)将允许您添加涉及父级和子级的规则,但规则将针对集合而不是每个项目。更好的方法,以及我通常的做法是为您的Load实体创建一个子验证器:public class LoadValidator :...
我不认为有一个很好的方法来做内联。子规则不允许传入父对象。Must和我认为Custom(可能通过context....
我认为你能做的就是测试控制器逻辑。示例可能如下所示:
demo地址: https://github.com/thePengLong/CustomFluentValidationDemo 如有问题,恳请指出。---.net core自定义使用FluentValidationhttps://blog.51cto.com/u_15162069/2698734 标签: FluentValidation 好文要顶 关注我 收藏该文 微信分享 dreamw 粉丝- 109 关注- 224 +加关注 0 0 « 上一篇: NET 5...
});//[Optional] Add INameResolver (SystemTextJsonNameResolver will be registered by default)//services.AddSingleton<INameResolver, CustomNameResolver>();//Adds FluentValidationRules staff to Swagger. (Minimal configuration)services.AddFluentValidationRulesToSwagger();//[Optional] Configure generation opt...
我已经开始编写这样的自定义规则:-RuleFor(pd => pd.PurchasePrice).Custom((a, context) => { var parent = context.ParentContext.InstanceToValidate as ParentObject; var parentOfParent = context.ParentContext.ParentContext.InstanceToValidate as GrandParentObject; });然而,第二个 ParentContext 根本不...
C# FluentValidation custom error messages Custom error messages can be provided withWithMessage. Program.cs using FluentValidation; var validator = new UserValidator(); var u = new User("Roger Roe", ""); var res = validator.Validate(u); ...
Looking to create custom validation response for my .net core web Api. Here I want to have response model like [{ ErrorCode: ErrorField: ErrorMsg: }] I want to use [CustomizeValidator] attribute instead of physically creating validator class instance to get ValidationResult . But I am not...
改变原来的方式后,少写了部分重复代码,更加方便。但同样要注意,避免出现未注册的情况出现。 FluentValidation的文档地址:https://docs.fluentvalidation.net/en/latest/start.html demo地址:https://github.com/thePengLong/CustomFluentValidationDemo 如有问题,恳请指出。