Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ASP.NET web API support IValidatableObject?

I have a view model that implements IValidatableObject and also has several validation attributes. When I attempt to call an action on my ApiController, only the attribute validation is performed. Does ASP.NET Web API not support IValidatableObject? What's the alternative for complex validation that cannot be represented by a single attribute?

Edit: Somewhere along the line, I must have fudged something up. The validation mysteriously started working as expected. Looks like IValidatableObject is definitely supported by default.

like image 754
Chris Avatar asked Aug 13 '12 19:08

Chris


1 Answers

With Web API 2.1 (Microsoft.AspNet.WebApi nuget 5.1.x), I experienced IValidatableObject's Validate method not being called if any of the validation attributes are invalid. Hence, all validation attributes that have been applied to your class's properties must first pass as valid before that class's Validate method will be called.

If, for example, you have a property with the RequiredAttribute and you do not put a value in that field, your implementation of IValidatableObject's Validate method will not be called. Although not technically a bug, I expected the Validate method to be called every time I validate.

like image 165
Jeremy Cook Avatar answered Oct 11 '22 14:10

Jeremy Cook