Permalink
Browse files

Refactored GetValidators method

  • Loading branch information...
1 parent 9f207dd commit 00805539a033fd1566415b88751591309a871193 @thecodejunkie thecodejunkie committed Mar 2, 2014
Showing with 21 additions and 13 deletions.
  1. +21 −13 src/Nancy.Validation.DataAnnotations/DefaultPropertyValidatorFactory.cs
@@ -32,22 +32,19 @@ public IEnumerable<IPropertyValidator> GetValidators(Type type)
var typeDescriptor =
new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
- var propertyDescriptors =
- typeDescriptor.GetProperties();
-
var results =
new List<IPropertyValidator>();
- var classAttributes =
- typeDescriptor.GetAttributes().OfType<ValidationAttribute>();
+ results.Add(this.GetTypeValidator(typeDescriptor));
+ results.AddRange(this.GetPropertyValidators(typeDescriptor));
- var classValidator =
- new PropertyValidator
- {
- AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
- };
+ return results;
+ }
- results.Add(classValidator);
+ private IEnumerable<PropertyValidator> GetPropertyValidators(ICustomTypeDescriptor typeDescriptor)
+ {
+ var propertyDescriptors =
+ typeDescriptor.GetProperties();
foreach (PropertyDescriptor descriptor in propertyDescriptors)
{
@@ -61,10 +58,21 @@ public IEnumerable<IPropertyValidator> GetValidators(Type type)
Descriptor = descriptor
};
- results.Add(validator);
+ yield return validator;
}
+ }
- return results;
+ private PropertyValidator GetTypeValidator(ICustomTypeDescriptor typeDescriptor)
+ {
+ var classAttributes =
+ typeDescriptor.GetAttributes().OfType<ValidationAttribute>();
+
+ var classValidator =
+ new PropertyValidator
+ {
+ AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
+ };
+ return classValidator;
}
private IDictionary<ValidationAttribute, IEnumerable<IDataAnnotationsValidatorAdapter>> GetAttributeAdaptors(IEnumerable<ValidationAttribute> attributes)

0 comments on commit 0080553

Please sign in to comment.