Using FluentValidation's WithMessage method with a list of named parameters

后端 未结 5 654
长发绾君心
长发绾君心 2021-02-07 04:57

I am using FluentValidation and I want to format a message with some of the object\'s properties value. The problem is I have very little experience with expressions and delegat

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 05:30

    For anyone looking into this now - current FluentValidation (v8.0.100) allows you use a lamda in WithMessage (As ErikE suggested above) so you can use:

    RuleFor(x => x.Name).NotEmpty()
       .WithMessage(x => $"The name {x.Name} is not valid for Id {x.Id}.");
    

    Hope this helps someone.

提交回复
热议问题