@@ -12,6 +12,20 @@ namespace Simplify.Web.Model.Validation
12
12
/// </summary>
13
13
public class ValidationAttributesExecutor : IModelValidator
14
14
{
15
+ /// <summary>
16
+ /// Initializes a new instance of the <see cref="ValidationAttributesExecutor"/> class.
17
+ /// </summary>
18
+ /// <param name="nesting">if set to <c>true</c> then <see cref="ValidationAttributesExecutor"/> should validate nested and inherited properties.</param>
19
+ public ValidationAttributesExecutor ( bool nesting = true )
20
+ {
21
+ Nesting = nesting ;
22
+ }
23
+
24
+ /// <summary>
25
+ /// Gets a value indicating whether <see cref="ValidationAttributesExecutor"/> should validate nested and inherited properties.
26
+ /// </summary>
27
+ public bool Nesting { get ; }
28
+
15
29
/// <summary>
16
30
/// Validates the specified model.
17
31
/// </summary>
@@ -24,21 +38,6 @@ public void Validate<T>(T model, IDIResolver resolver)
24
38
Validate ( typeof ( T ) , model , resolver ) ;
25
39
}
26
40
27
- private static void Validate ( Type type , object ? value , IDIResolver resolver )
28
- {
29
- var properties = type . GetProperties ( ) ;
30
-
31
- foreach ( var item in properties )
32
- {
33
- var currentItemValue = item . GetValue ( value ) ;
34
-
35
- ValidateProperty ( currentItemValue , item , resolver ) ;
36
-
37
- if ( currentItemValue != default )
38
- Validate ( item . PropertyType , currentItemValue , resolver ) ;
39
- }
40
- }
41
-
42
41
/// <summary>
43
42
/// Validates the specified value.
44
43
/// </summary>
@@ -53,5 +52,22 @@ private static void ValidateProperty(object? value, PropertyInfo propertyInfo, I
53
52
foreach ( var attribute in validationAttributes )
54
53
attribute . Validate ( value , propertyInfo , resolver ) ;
55
54
}
55
+
56
+ private void Validate ( Type type , object ? value , IDIResolver resolver )
57
+ {
58
+ if ( Nesting )
59
+ if ( type . BaseType != null && type . BaseType != typeof ( object ) )
60
+ Validate ( type . BaseType , value , resolver ) ;
61
+
62
+ foreach ( var item in type . GetProperties ( ) )
63
+ {
64
+ var currentItemValue = item . GetValue ( value ) ;
65
+
66
+ ValidateProperty ( currentItemValue , item , resolver ) ;
67
+
68
+ if ( Nesting && currentItemValue != default )
69
+ Validate ( item . PropertyType , currentItemValue , resolver ) ;
70
+ }
71
+ }
56
72
}
57
73
}
0 commit comments