Validator.TryValidateObject does not validate attribute

坚强是说给别人听的谎言 提交于 2019-12-24 05:38:07

问题


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:


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; }


来源:https://stackoverflow.com/questions/21111905/validator-tryvalidateobject-does-not-validate-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!