Skip to content

Commit 7e9eb55

Browse files
committed
Nesting toggle
1 parent 095e132 commit 7e9eb55

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/Simplify.Web/Bootstrapper/BaseBootstrapper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public virtual void RegisterDefaultModelBinders()
426426
/// </summary>
427427
public virtual void RegisterDefaultModelValidators()
428428
{
429-
BootstrapperFactory.ContainerProvider.Register<ValidationAttributesExecutor>(LifetimeType.Singleton);
429+
BootstrapperFactory.ContainerProvider.Register(r => new ValidationAttributesExecutor(), LifetimeType.Singleton);
430430
}
431431

432432
#endregion Simplify.Web types registration

src/Simplify.Web/Model/Validation/ValidationAttributesExecutor.cs

+31-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ namespace Simplify.Web.Model.Validation
1212
/// </summary>
1313
public class ValidationAttributesExecutor : IModelValidator
1414
{
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+
1529
/// <summary>
1630
/// Validates the specified model.
1731
/// </summary>
@@ -24,24 +38,6 @@ public void Validate<T>(T model, IDIResolver resolver)
2438
Validate(typeof(T), model, resolver);
2539
}
2640

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-
4541
/// <summary>
4642
/// Validates the specified value.
4743
/// </summary>
@@ -56,5 +52,22 @@ private static void ValidateProperty(object? value, PropertyInfo propertyInfo, I
5652
foreach (var attribute in validationAttributes)
5753
attribute.Validate(value, propertyInfo, resolver);
5854
}
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+
}
5972
}
6073
}

0 commit comments

Comments
 (0)