RegexStringValidator attribute

  • Thread starter Thread starter Berryl Hesh
  • Start date Start date
B

Berryl Hesh

I want a ConfigurationProperty property to be validated; it should always be
the name of some interface of some sort so it just needs to start with an
"I".

Pattern = @"^I"
TestInput = "IEmployeeRepository"

Iinstantiating a RegexStringValidator as a variable works as expected:
[Test] public void InterfaceValidatorTest() {
var validator = new RegexStringValidator(@"^I");
<----------------succeds without a "*"
validator.Validate("IEmployeeRepository");
}

But as an attribute, it fails unless I add an asterisk ("*") after the
pattern:

[ConfigurationProperty("repositoryInterface", IsKey = true, IsRequired =
true)]
[RegexStringValidator(@"^I*")] public string InterfaceName {
<------------------fails without a "*"
get { return (string) this["repositoryInterface"]; }
set { this["repositoryInterface"] = value; }
}

Why the discrepency?
 
To clarify: I'm trying to understand what looks like a discrepency between a
RegexStringValidator when used declaratively vs. usage in code. The details
are below.

Thanks. BH
 
It happens that Berryl Hesh formulated :
I want a ConfigurationProperty property to be validated; it should always be
the name of some interface of some sort so it just needs to start with an
"I".

Pattern = @"^I"
TestInput = "IEmployeeRepository"

Iinstantiating a RegexStringValidator as a variable works as expected:
[Test] public void InterfaceValidatorTest() {
var validator = new RegexStringValidator(@"^I");
<----------------succeds without a "*"
validator.Validate("IEmployeeRepository");
}

But as an attribute, it fails unless I add an asterisk ("*") after the
pattern:

[ConfigurationProperty("repositoryInterface", IsKey = true, IsRequired =
true)]
[RegexStringValidator(@"^I*")] public string InterfaceName {
<------------------fails without a "*"
get { return (string) this["repositoryInterface"]; }
set { this["repositoryInterface"] = value; }
}

Why the discrepency?

If you use the attribute [RegexStringValidator] you really use the
RegexStringValidatorAttribute class. That might be a source of
differences.

I don't know this class, but I do know the RegularExpressionValidator
from ASP.Net. This validator checks not only that the supplied string
matches the regex, but also checks that the matched length is equal to
the complete length of the string. So the regex works as if it always
starts with a "^" and ends with a "$".
So a "^I" would effectively match an "I" only. A "^I.*" (that is ".*"
instead of a plain "*", typo in your post?) would match anything
starting with an "I".

Hans Kesting
 
Back
Top