简体   繁体   English

在 FluentValidation 中忽略大小写敏感

[英]Ignore case sensitive in FluentValidation

Anyone have any ideas to ignore case in FluentValidation任何人都有任何想法可以忽略 FluentValidation 中的大小写

Example RuleFor(x => x.Gender).Ignorecase();示例RuleFor(x => x.Gender).Ignorecase(); so user would able to enter F or f .所以用户可以输入Ff

你能做到以下几点吗?

RuleFor(x.ToLower() => x.Gender.ToLower());

I think built-in Regex validation will do the work: https://github.com/JeremySkinner/FluentValidation/wiki/c.-Built-In-Validators#regular-expression-validator我认为内置的正则表达式验证可以完成这项工作: https : //github.com/JeremySkinner/FluentValidation/wiki/c.-Built-In-Validators#regular-expression-validator

Somethink like:有人认为:

RuleFor(x => x.Gender).Matches("^[FfMm]{1}$");

It is possible by create an Extension method可以通过创建扩展方法

public static bool EqualsIgnoreCase(this string from, string to)
 { 
   return string.Equals(from, to, StringComparison.OrdinalIgnoreCase); 
 }

then use it like bellow:然后像下面这样使用它:

RuleFor(x => x.Gender.EqualsIgnoreCase("f"))

平等验证器支持这一点:

RuleFor(x => x.Gender).Equal("f", StringComparer.OrdinalIgnoreCase);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM