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 overload fluentvalidation.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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -