c# - Using .Matches with regex for a char in fluent -
i have class called transaction
contains property named source
within transaction
class have validation using fluentvalidation
, trying validate source property using regex
i'm having issue
//source isnt required when present must 1 character 'x' or 'y' rulefor(transaction => transacion.source) .matches("^(x|y)?$") .when(transaction => transaction.source != null);
i getting:
error 1
fluentvalidation.irulebuilderinitial<myutility.transaction,char?>
not contain definition 'matches' , best extension method overloadfluentvalidation.defaultvalidatorextensions.matches<t>(fluentvalidation.irulebuilder<t,string>, system.text.regularexpressions.regex)
has invalid arguments
i have used exact same code different property no problems, although string not char.
there's dot .
in code between matches
, ("^(x|y)?$")
.
.rulefor(transaction => transaction.source) .matches("^(x|y)?$") // dot here .when(transaction => transaction.source != null);
and robin pointed out, regex more readable in [xy]
format.
edit
i reread post , says source
property char
if convert string
won't error.
.rulefor(transaction => transaction.source.hasvalue ? transaction.source.tostring() : "") .matches("^[xy]?$") // dot here .when(transaction => transaction.source != null);
Comments
Post a Comment