3
votes

Using WebApi v2 I've built something that takes an object as argument. I'm using both IValidateableObject and dataannotations for model validations that I trigger using an WebApi filter.

However, one object contains an array of items that all need to be validated. I've created a custom attribute in order to do this using something like Asp.net Web Api nested model validation, but I can't get the validation going. Also - I've use the ValidateAllProperties flag already.

So I built a console app to verify the behavior and the validation doesn't seem to be triggered (or I'm calling the API incorrectly). Here's something that does not work for me:

namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations;

class Program
{
    static void Main()
    {
        var s = new Boundary { LowerDecimal = 1.1m };
        var isValid = Validator.TryValidateObject(s, new ValidationContext(s, null, null), new List<ValidationResult>(), true);
        Console.WriteLine("Validation resulted in " + isValid);
        Console.ReadLine();
    }
}

public class Boundary
{
    [Range(0,1)]
    public decimal LowerDecimal { get; set; }
}

}

isValid always returns true. What am I doing wrong here?

EDIT - Also tried defining MetaData class like here: Validation does not work when I use Validator.TryValidateObject

1
Could you show your custom ValidationAttribute code? The above will always return true because the Validator.TryValidateObject method will only check for the RequiredAttribute on each of the properties of the specified type, it doesn't recognize other validation attributes - that's why you need to use a custom attribute. This is stated in the remarks section of the docs. - James
That Range attribute is inside the framework, msdn.microsoft.com/en-us/library/…, also, I've used msdn.microsoft.com/en-us/library/dd411772(v=vs.110).aspx with the boolean at the end, indicating 'validateAllProperties, true to validate all properties; if false, only required attributes are validated' - Jochen van Wylick
the code I was referring to was your custom attribute you mentioned you wrote. The documentation is quite confusing, the remarks section appears to contradict what the validateAllProperties says. Regardless, I think you are correct this doesn't appear to be the correct behaviour it should probably be validating the RangeAttribute on the property. - James
Ah sorry for misunderstanding. In order to eliminate possible errors in my code, I created this setup with only standard stuff and I can't even get that to work. - Jochen van Wylick
From your example, it would appear what the remarks says is the defined behaviour. However, maybe there is a step missing for this to work on the properties - will do a bit of digging. - James

1 Answers

10
votes

The TryValidateObject is a red herring, the problem actually lies with the RangeAttribute.

The problem is the attribute has 2 overloads (one for int and one for double), in your attribute definition you have declared int values which means internally the property value is cast to int which will truncate the floating point - so in essence 1.1 will be truncated to 1 so will always be in range.

Simple fix, make sure your attribute knows you want to compare double's and not int i.e.

[Range(0.0, 1.0)]
public decimal LowerDecimal { get; set; }