StringLengthAttribute doesn't seem to be working
Asked Answered
N

3

16

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?

Nonego answered 23/1, 2011 at 20:36 Comment(0)
N
34

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.

Nonego answered 23/1, 2011 at 20:42 Comment(2)
Thanks, this bit me today.Hin
For me, it was TryValidateObject — and there's an overload that takes a fourth bool parameter.Antiseptic
J
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

Joachima answered 1/6, 2011 at 19:33 Comment(2)
Connect is gone now, I'm curious, what came of this issue? Cause it's still seems to be bugged.Hin
@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.Joachima
C
-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.

Chrischrism answered 9/9, 2021 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.