1

I have an ASP.NET Core 6 Web API project with all controllers decorated with the [ApiController] annotation. I use validation annotations like [Required], [MaxLength] etc. to validate properties of DTOs received in incoming requests.

Some of my DTOs also implement the IValidatableObject interface, to handle more complex validation scenarios not covered by the attributes.

When DTO in the request is invalid because IValidatableObject.Validate() returned some ValidationResults, the corresponding validation error messages appear in the response.

But when the DTO ALSO has validation errors because of the attributes, only attribute-related error messages appear in the response.

How can I get all errors to appear?

Here's another, more complex scenario, when not all errors appear in the response. Let's say I have two DTOs - Parent and Child. Parent has property Children of type ICollection<Child>. Child has some validation attributes on its properties. Parent has a validation attribute on the Children property that checks that the property value, which is a collection, does not contain nulls.

If in the request I send a Parent with Children collection containing two items - an invalid Child and a null, the response has only one error message, the one about the invalid child. If I make the child valid, then the message about null in the Children collection starts coming up.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew
  • 1,139
  • 1
  • 12
  • 27

1 Answers1

0

If you want to show error message together, You can custom validation attribute with ValidationAttribute instead of implement IValidatableObject, Here is a simple demo:

public class CountAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            
            int Count = (int)value;

            if (Count > 3 && Count < 7)
            {
                return new ValidationResult("Count is not valid, Please try again");
            }
            return ValidationResult.Success;
        }
    }

Model

public class Parent 
    {
        [Range(1,5)]
        public int MaxCount { get; set; }

        [Count]
        public int Count { get; set; }
       
    }

Now if the request model is invalid, It will response all the error message.

enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
  • Thanks Xinran Shen. Yes I have a few custom attributes and they do work as intended. But I also have some complex validation scenarios spanning several DTO properties, where adding an attribute to any one of those properties would not make sense. Also your answer does not cover my second example with two DTOs. – Andrew Mar 06 '23 at 10:55
  • Your some complex validation scenarios can't be achieved by `ValidationAttribute`? Unfortunately, I don't find any articles about why `IValidatableObject` doesn't show error message with built-in attribute . Maybe You can post a issue in asp.net core github and microsoft developer will explain it. – Xinran Shen Mar 07 '23 at 06:02