Been trying to incorporate server side DataAnnotation validation to my project and I've found out that DataAnnotations has it's own type of error, the ValidationException. My problem with it, though, is that it only returns one validation error at a time so if 3 properties failed validation only the first one is thrown. I'm looking for a way to throw all of the errors as an exception so instead of informing the user/developer that the validation failed, it would state which properties/fields failed validation at one go.
I found the Validator.TryValidateObject(...) method but it just populates ValidationResults and leaves the developer an option to throw and exception or not. What I currently implement is iterating through the ValidationResults to create a list of ValidationExceptions from that, wrap the list into an AggregateException then throw another ValidationException with the AggregateException in its InnerExceptions.
ValidationContext validationContext = new ValidationContext(entity, null, null);
List<ValidationResult> validationResults = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(entity, validationContext, validationResults, true);
if (!isValid)
{
List<ValidationException> validationErrors = new List<ValidationException>();
foreach (ValidationResult validationResult in validationResults)
{
validationErrors.Add(new ValidationException(validationResult.ErrorMessage);
}
throw new ValidationException("Entity validation failed.", new AggregateException(validationErrors));
}
So basically, my questions would be:
Answer your first question:
Because each validation attribute have their own IsValid
property and return public ValidationResult(string errorMessage, IEnumerable<string> memberNames);
validation result and you can get member name from list of member name. So each validation failed of property return isValid. It is not a good idea that the entity has validation you are applying on property of entity.
Answer your second question:
You can create your own ValidationAttribute
list to validate entity:
var validationResults = new List<ValidationResult>();
var validationAttributes = new List<ValidationAttribute>();
validationAttributes.Add(new CustomValidationAttribute(typeof(ClaimValidator), "ValidateClaim"));
var result = Validator.TryValidateValue(claimObject,
new ValidationContext(claimObject, null, null),
validationResults,
validationAttributes);
Third Answer:
You can get the member name from ValidationResult
:
public ValidationResult(string errorMessage, IEnumerable<string> memberNames)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With