15

Here is my Test class with data annotations:

class Test
{
  [Required, StringLength(10)]
  public string MyProperty { get; set; }
}

Here is my console test program:

class Program
{
  static void Main(string[] args)
  {
    var test = new Test {
      MyProperty = "this is way more than 10 characters and should fail."
    };

    var context = new ValidationContext(test, null, null);

    // No exception here! (why not?)
    Validator.ValidateObject(test, context);

    test.MyProperty = null;

    // Exception here, as expected
    Validator.ValidateObject(test, context);
  }
}

For some reason, I do not get a validation exception when the string length is too long. I do get a validation exception (as expected) when I set the property to null and re-validate. Any ideas why my string length annotation is not being enforced?

Mike
  • 7,500
  • 8
  • 44
  • 62

3 Answers3

32

It's a bit non-intuitive, but changing

Validator.ValidateObject(test, context);

to

Validator.ValidateObject(test, context, true);

solves the problem. The third argument is bool validateAllProperties. I'm not sure why the [Required] attribute was previously being enforced while the [StringLength] wasn't, but at least it all works now.

Mike
  • 7,500
  • 8
  • 44
  • 62
3

Thanks for posting this. I posted a bug on Microsoft Connect for this issue. I assume that they are incorrectly skipping a System.String property as it is not a value type because they do not do a deep/recursive validation without the "validateAllProperties" parameter.

Here's the bug link if you're interested: https://connect.microsoft.com/VisualStudio/feedback/details/672247/system-componentmodel-dataannotations-validator-does-not-validate-stringlengthattribute-unless-validateallproperties-specified

Dan Mork
  • 1,768
  • 1
  • 12
  • 12
  • Connect is gone now, I'm curious, what came of this issue? Cause it's still seems to be bugged. – mxmissile Oct 23 '20 at 19:56
  • @mxmissile, not sure what happened to the bug because Microsoft Connect has been decommissioned. However, I think the code is in GitHub – maybe in dotnet/runtime – so it might be easier to determine what's going on and log an issue, if appropriate. – Dan Mork Dec 04 '20 at 19:55
-1

It did not work for me in the WebAPI context. However, with little further research I found a solution.

Use ModelState.IsValid along with your data annotations to resolve this problem as follows:

if (!ModelState.IsValid)
{
    return BadRequest(ModelState);
}

This will create a 400 (bad request) response to the caller as follows:

{"Message":"The request is invalid.","ModelState":{"reqs.ImportRows[0].SMSConsent":["The field SMSConsent must be a string with a maximum length of 1."]}}

The best part is, it provides the caller with the exact row number of the list if the request is a list object. This is very helpful to your 3rd party clients using your api to debug, analyze and fix the issue.

AV2000
  • 459
  • 5
  • 5