@@ -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,24 +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
- if ( type . BaseType != null && type . BaseType != typeof ( object ) )
32
- Validate ( type . BaseType , value , resolver ) ;
33
-
34
- foreach ( var item in properties )
35
- {
36
- var currentItemValue = item . GetValue ( value ) ;
37
-
38
- ValidateProperty ( currentItemValue , item , resolver ) ;
39
-
40
- if ( currentItemValue != default )
41
- Validate ( item . PropertyType , currentItemValue , resolver ) ;
42
- }
43
- }
44
-
45
41
/// <summary>
46
42
/// Validates the specified value.
47
43
/// </summary>
@@ -56,5 +52,22 @@ private static void ValidateProperty(object? value, PropertyInfo propertyInfo, I
56
52
foreach ( var attribute in validationAttributes )
57
53
attribute . Validate ( value , propertyInfo , resolver ) ;
58
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
+ }
59
72
}
60
73
}
0 commit comments