RegexStringValidator attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Berryl Hesh

    RegexStringValidator attribute

    I want a ConfigurationPr operty 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 = "IEmployeeRepos itory"

    Iinstantiating a RegexStringVali dator as a variable works as expected:
    [Test] public void InterfaceValida torTest() {
    var validator = new RegexStringVali dator(@"^I");
    <----------------succeds without a "*"
    validator.Valid ate("IEmployeeR epository");
    }

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

    [ConfigurationPr operty("reposit oryInterface", IsKey = true, IsRequired =
    true)]
    [RegexStringVali dator(@"^I*")] public string InterfaceName {
    <------------------fails without a "*"
    get { return (string) this["repositoryInte rface"]; }
    set { this["repositoryInte rface"] = value; }
    }

    Why the discrepency?


  • Berryl Hesh

    #2
    Re: RegexStringVali dator attribute

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

    Thanks. BH

    "Berryl Hesh" <efinger9@yahoo .comwrote in message
    news:j67Pk.2549 $U%6.417@newsfe 01.iad...
    >I want a ConfigurationPr operty 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 = "IEmployeeRepos itory"
    >
    Iinstantiating a RegexStringVali dator as a variable works as expected:
    [Test] public void InterfaceValida torTest() {
    var validator = new RegexStringVali dator(@"^I");
    <----------------succeds without a "*"
    validator.Valid ate("IEmployeeR epository");
    }
    >
    But as an attribute, it fails unless I add an asterisk ("*") after the
    pattern:
    >
    [ConfigurationPr operty("reposit oryInterface", IsKey = true, IsRequired =
    true)]
    [RegexStringVali dator(@"^I*")] public string InterfaceName {
    <------------------fails without a "*"
    get { return (string) this["repositoryInte rface"]; }
    set { this["repositoryInte rface"] = value; }
    }
    >
    Why the discrepency?
    >

    Comment

    • Hans Kesting

      #3
      Re: RegexStringVali dator attribute

      It happens that Berryl Hesh formulated :
      I want a ConfigurationPr operty 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 = "IEmployeeRepos itory"
      >
      Iinstantiating a RegexStringVali dator as a variable works as expected:
      [Test] public void InterfaceValida torTest() {
      var validator = new RegexStringVali dator(@"^I");
      <----------------succeds without a "*"
      validator.Valid ate("IEmployeeR epository");
      }
      >
      But as an attribute, it fails unless I add an asterisk ("*") after the
      pattern:
      >
      [ConfigurationPr operty("reposit oryInterface", IsKey = true, IsRequired =
      true)]
      [RegexStringVali dator(@"^I*")] public string InterfaceName {
      <------------------fails without a "*"
      get { return (string) this["repositoryInte rface"]; }
      set { this["repositoryInte rface"] = value; }
      }
      >
      Why the discrepency?
      If you use the attribute [RegexStringVali dator] you really use the
      RegexStringVali datorAttribute class. That might be a source of
      differences.

      I don't know this class, but I do know the RegularExpressi onValidator
      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


      Comment

      Working...