Search code examples
.netasp.net-core-mvcdata-annotationsmodelstate

ModelState.IsValid is always true while using Data annotation, MVC as Web API


I have seen many questions like this one but no answer has solved my problem. I can submit a name that contains a space, or leaving a description field empty and the ModelState is still valid.

in my StartUp.cs I do use

services.AddMvc();

I did try without the Validator.TryValidateObject, and the Model.State is always valid.

my class has data annotation

public class UpdateAttributeResource
    {
        [Required, MaxLength(96), RegularExpression(@"^[A-Za-z0-9~_$\-]*$")]
        public string Name;
        [Required, MaxLength(96)]
        public string DisplayName;
        [Required, MaxLength(160)]
        public string DescriptionEn;
        [Required, MaxLength(160)]
        public string DescriptionFr;
        public string Comments;
    }

my controller receives a collection of my class and even when forcing validation using TryValidateObject the ModelState is still valid

public IActionResult UpdateAttributes([FromBody]UpdateAttributeResource[] updateAttributes)
{
    var validationContext = new ValidationContext(updateAttributes[0], null, null);
    var validationResults = new List<ValidationResult>();

    var isValid = Validator.TryValidateObject(updateAttributes[0], validationContext, validationResults, true);

    if (!isValid)
    {
        ModelState.AddModelError("NOPE", "Nope");
        return BadRequest(ModelState);
    }

Solution

  • I actually finally found the answer here DataAnnotations tryvalidateobject always returns true

    I don't know why it did not pop up before, anyway

    The validator ignores the [RequiredAttribute] on fields - it takes into an account only properties;

    So I simply needed to change my class to

    public class UpdateAttributeResource
        {
            [Required, MaxLength(96), RegularExpression(@"^[A-Za-z0-9~_$\-]*$")]
            public string Name { get; set; }
            [Required, MaxLength(96)]
            public string DisplayName { get; set; }
            [Required, MaxLength(160)]
            public string DescriptionEn { get; set; }
            [Required, MaxLength(160)]
            public string DescriptionFr { get; set; }
            public string Comments;
        }