Validator.TryValidateObject does not validate attr

2019-07-14 21:52发布

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条回答
可以哭但决不认输i
2楼-- · 2019-07-14 22:46

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; }
查看更多
登录 后发表回答