Skip to content

Commit 456d77e

Browse files
committed
Base types on nested validation exclude
1 parent 8383994 commit 456d77e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/Simplify.Web.Tests/Model/Validation/ValidationAttributesExecutorTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,19 @@ public void Validate_InheritedProperty_NestedNullAttributeException()
7373
Assert.That(ex.Message,
7474
Does.StartWith($"Required property '{nameof(BaseNestedModel.BuiltInType)}' is null or empty"));
7575
}
76+
77+
[Test]
78+
public void Validate_NotNullSystemTypes_NoExceptions()
79+
{
80+
// Arrange
81+
var model = new SystemTypesModel
82+
{
83+
StringProperty = "test",
84+
IntProperty = 23
85+
};
86+
87+
// Act
88+
_validator.Validate(model, null);
89+
}
7690
}
7791
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#nullable disable
2+
3+
using Simplify.Web.Model.Validation.Attributes;
4+
5+
namespace Simplify.Web.Tests.TestEntities
6+
{
7+
public class SystemTypesModel
8+
{
9+
[Required]
10+
public string StringProperty { get; set; }
11+
12+
public int IntProperty { get; set; }
13+
14+
public bool BoolProperty { get; set; }
15+
}
16+
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static void ValidateProperty(object? value, PropertyInfo propertyInfo, I
5656
private void Validate(Type type, object? value, IDIResolver resolver)
5757
{
5858
if (Nesting)
59-
if (type.BaseType != null && type.BaseType != typeof(object))
59+
if (type.BaseType != null && type.BaseType != typeof(object) && !IsSystemType(type.BaseType))
6060
Validate(type.BaseType, value, resolver);
6161

6262
foreach (var item in type.GetProperties())
@@ -65,9 +65,14 @@ private void Validate(Type type, object? value, IDIResolver resolver)
6565

6666
ValidateProperty(currentItemValue, item, resolver);
6767

68-
if (Nesting && currentItemValue != default)
68+
if (Nesting && currentItemValue != default && !IsSystemType(item.PropertyType))
6969
Validate(item.PropertyType, currentItemValue, resolver);
7070
}
7171
}
72+
73+
private bool IsSystemType(Type type)
74+
{
75+
return type.Namespace?.StartsWith("System") ?? false;
76+
}
7277
}
7378
}

0 commit comments

Comments
 (0)