2011-10-05 79 views
42

我有一个洒有DataAnnotation属性的对象图,其中对象的某些属性是本身具有验证属性的类,等等。DataAnnotations:递归验证整个对象图

在以下情形:

public class Employee 
{ 
    [Required] 
    public string Name { get; set; } 

    [Required] 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    [Required] 
    public string Line1 { get; set; } 

    public string Line2 { get; set; } 

    [Required] 
    public string Town { get; set; } 

    [Required] 
    public string PostalCode { get; set; } 
} 

如果我尝试验证的EmployeeAddress没有价值PostalCode,那么我想(和期望)例外,但我得到没有。下面是我如何做它:

var employee = new Employee 
{ 
    Name = "Neil Barnwell", 
    Address = new Address 
    { 
     Line1 = "My Road", 
     Town = "My Town", 
     PostalCode = "" // <- INVALID! 
    } 
}; 

Validator.ValidateObject(employee, new ValidationContext(employee, null, null)); 

我有什么其他的选择与Validator,以确保所有的属性进行验证递归

非常感谢提前。

回答

47

我的回答得到了太长时间放在这里,所以我把它变成了博客文章:)

Recursive Validation Using DataAnnotations

该解决方案为您提供了一个方式使用您使用的是相同的基本方法来实现递归验证现在。

+1

+ 1为好的解决方案 – Jehof

+0

不错,但收集呢?能够验证诸如“公共IList

地址”这样的属性是非常棒的。无论如何,感谢您的解决方案。 – altso

+0

该属性不为我评估。 –

23

下面是opt-in属性方法的替代方案。我相信这将正确地遍历对象图并验证一切。

public bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results) { 

bool result = TryValidateObject(obj, results); 

var properties = obj.GetType().GetProperties().Where(prop => prop.CanRead 
    && !prop.GetCustomAttributes(typeof(SkipRecursiveValidation), false).Any() 
    && prop.GetIndexParameters().Length == 0).ToList(); 

foreach (var property in properties) 
{ 
    if (property.PropertyType == typeof(string) || property.PropertyType.IsValueType) continue; 

    var value = obj.GetPropertyValue(property.Name); 

    if (value == null) continue; 

    var asEnumerable = value as IEnumerable; 
    if (asEnumerable != null) 
    { 
     foreach (var enumObj in asEnumerable) 
     { 
      var nestedResults = new List<ValidationResult>(); 
      if (!TryValidateObjectRecursive(enumObj, nestedResults)) 
      { 
       result = false; 
       foreach (var validationResult in nestedResults) 
       { 
        PropertyInfo property1 = property; 
        results.Add(new ValidationResult(validationResult.ErrorMessage, validationResult.MemberNames.Select(x => property1.Name + '.' + x))); 
       } 
      }; 
     } 
    } 
    else 
    { 
     var nestedResults = new List<ValidationResult>(); 
     if (!TryValidateObjectRecursive(value, nestedResults)) 
     { 
      result = false; 
      foreach (var validationResult in nestedResults) 
      { 
       PropertyInfo property1 = property; 
       results.Add(new ValidationResult(validationResult.ErrorMessage, validationResult.MemberNames.Select(x => property1.Name + '.' + x))); 
      } 
     } 
    } 
} 

return result; 
} 

最先进的最新代码: https://github.com/reustmd/DataAnnotationsValidatorRecursive

套餐: https://www.nuget.org/packages/DataAnnotationsValidator/

而且,我已经更新了这个解决方案来处理循环对象图。感谢您的反馈。

+6

我喜欢这个解决方案,但是当对象图包含循环时要小心无限循环。 –

+0

@ manu08 ....平平无奇 - 感谢您的时间保存。 – Pakk

+0

上面的代码示例与git版本相比有一些问题 - 如果您打算实施,那么绝对[遵循链接](https://github.com/reustmd/DataAnnotationsValidatorRecursive/tree/master/DataAnnotationsValidator/DataAnnotationsValidator)这个(或者通过[nuget](https://www.nuget.org/packages/DataAnnotationsValidator/)提供的'Install-Package dataannotationsvalidator'!) – rogersillito