Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for validation of properties of types within a collection

  • Loading branch information...
commit c7b6c6c57943d056e586babcb2c455df53875cc1 1 parent bdaa0f4
@adrianaisemberg authored
Showing with 93 additions and 35 deletions.
  1. +57 −35 CLAP/TypeValidator.cs
  2. +10 −0 Tests/Samples.cs
  3. +26 −0 Tests/Tests.cs
View
92 CLAP/TypeValidator.cs
@@ -3,6 +3,7 @@
#if !FW2
using System.Linq;
+using System.Collections;
#endif
namespace CLAP
@@ -38,54 +39,70 @@ public static void Validate(object obj)
validator.Validate(propsAndValues);
}
- // property validators:
- // validate each property value, in case property is a custom class
+ // no need to validate properties of GAC objects
//
- foreach (var property in properties)
+ if (!type.Assembly.GlobalAssemblyCache &&
+ !type.IsArray)
{
- var value = property.GetValue(obj, null);
-
- // single validators
+ // property validators:
+ // validate each property value, in case property is a custom class
//
- var propertySingleValidators = property.
- GetAttributes<ValidationAttribute>().
- Select(a => a.GetValidator());
-
- foreach (var propertySingleValidator in propertySingleValidators)
+ foreach (var property in properties)
{
- propertySingleValidator.Validate(new ValueInfo(property.Name, property.PropertyType, value));
+ var value = property.GetValue(obj, null);
+
+ // single validators
+ //
+ var propertySingleValidators = property.
+ GetAttributes<ValidationAttribute>().
+ Select(a => a.GetValidator());
+
+ foreach (var propertySingleValidator in propertySingleValidators)
+ {
+ propertySingleValidator.Validate(new ValueInfo(property.Name, property.PropertyType, value));
+ }
+
+ // no need to validate primitives etc with collection validators
+ //
+ if (!Ignore(value))
+ {
+ // collection validators
+ //
+ var propertyCollectionValidators = property.
+ GetAttributes<CollectionValidationAttribute>().
+ Select(a => a.GetValidator());
+
+ foreach (var propertyValidator in propertyCollectionValidators)
+ {
+ var propertyPropsAndValues = value.GetType().
+ GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
+ Select(p => new ValueInfo(p.Name, p.PropertyType, p.GetValue(value, null))).
+ ToArray();
+
+ propertyValidator.Validate(propertyPropsAndValues);
+ }
+ }
}
- // no need to validate primitives etc with collection validators
+ // recursion:
+ // validate all values, in case their type has additional validation
//
- if (Ignore(value))
- {
- continue;
- }
-
- // collection validators
- //
- var propertyCollectionValidators = property.
- GetAttributes<CollectionValidationAttribute>().
- Select(a => a.GetValidator());
-
- foreach (var propertyValidator in propertyCollectionValidators)
+ foreach (var value in propsAndValues.Select(p => p.Value))
{
- var propertyPropsAndValues = value.GetType().
- GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
- Select(p => new ValueInfo(p.Name, p.PropertyType, p.GetValue(value, null))).
- ToArray();
-
- propertyValidator.Validate(propertyPropsAndValues);
+ Validate(value);
}
}
- // recursion:
- // validate all values, in case their type has additional validation
+ // IEnumerable
//
- foreach (var value in propsAndValues.Select(p => p.Value))
+ var collection = obj as IEnumerable;
+
+ if (collection != null)
{
- Validate(value);
+ foreach (var item in collection)
+ {
+ Validate(item);
+ }
}
}
@@ -97,6 +114,11 @@ private static bool Ignore(object obj)
return true;
}
+ if (obj is IEnumerable)
+ {
+ return false;
+ }
+
var type = obj.GetType();
if (type.IsArray ||
View
10 Tests/Samples.cs
@@ -799,6 +799,11 @@ public void Val(MyValidatedType t)
}
[Verb]
+ public void ValCollection(MyValidatedType[] t)
+ {
+ }
+
+ [Verb]
public void Complex(MyComplexType t)
{
}
@@ -812,6 +817,11 @@ public void Props(MyComplexType_2 t)
public void Zoo(TypeWithProps t)
{
}
+
+ [Verb]
+ public void ZooCollection(TypeWithProps[] t)
+ {
+ }
}
public class Sample_43
View
26 Tests/Tests.cs
@@ -1840,6 +1840,32 @@ public void ComplexType_WithTypeValidation_Fail()
}
[Test]
+ [ExpectedException(typeof(ValidationException))]
+ public void ArrayOfComplexType_WithTypeValidation_Fail_1()
+ {
+ var s = new Sample_42();
+
+ Parser.Run(new[]
+ {
+ "ZooCollection",
+ "-t:[{Number: 100},{Number: 5}]",
+ }, s);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ValidationException))]
+ public void ArrayOfComplexType_WithTypeValidation_Fail_2()
+ {
+ var s = new Sample_42();
+
+ Parser.Run(new[]
+ {
+ "ValCollection",
+ "-t:[{Number: 100},{Number: 5}]",
+ }, s);
+ }
+
+ [Test]
public void ComplexType_WithTypeValidation_Pass()
{
var s = new Sample_42();
Please sign in to comment.
Something went wrong with that request. Please try again.