0

I am using dot net core 3.1, and the streamreader reads the req body as

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

Then converts the request body to required model as

var response = JsonConvert.DeserializeObject<MyModel>(requestBody);

MyModel class:

public class MyModel
{
    [Required]
    [MaxLength(50)]
    public string Name { get; set; }
}

But the maxlength property is not working here it is simply taking string value of any length.

arunraj770
  • 767
  • 1
  • 10
  • 29

1 Answers1

0

The MaxLength attribute is not used during the deserialization. It won't validate that this field contain less than 50 characters and throw an exception or trim the string.

If you want to validate this you'll have to deserialize this json and add your custom validation.

You could get the attribute using this method and then implement your own logic.

Also take a look at IValidatableObject if what you want to do is simply validate your MyModel instances with the attributes that you provided.

New Rhino
  • 319
  • 3
  • 11