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
The
TryValidateObject
is a red herring, the problem actually lies with theRangeAttribute
.The problem is the attribute has 2 overloads (one for
int
and one fordouble
), in your attribute definition you have declaredint
values which means internally the property value is cast toint
which will truncate the floating point - so in essence1.1
will be truncated to1
so will always be in range.Simple fix, make sure your attribute knows you want to compare
double
's and notint
i.e.