Skip to content

Commit 095e132

Browse files
committed
Fixes #95, refactored
1 parent ccfd346 commit 095e132

File tree

13 files changed

+98
-58
lines changed

13 files changed

+98
-58
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using Simplify.Web.Model.Validation;
55
using Simplify.Web.Tests.Model.Validation.Attributes;
66
using Simplify.Web.Tests.TestEntities;
7-
using Simplify.Web.Tests.TestEntities.HierarchyValidation;
7+
using Simplify.Web.Tests.TestEntities.Inheritance;
8+
using Simplify.Web.Tests.TestEntities.Nesting;
89

910
namespace Simplify.Web.Tests.Model.Validation
1011
{
@@ -35,17 +36,16 @@ public void Validate_ModelWithOnePropertyAndOneValidateAttribute_AttributeValida
3536
}
3637

3738
[Test]
38-
public void Validate_HierarchicalModel_NestedNullAttributeException()
39+
public void Validate_NestedProperties_NestedNullAttributeException()
3940
{
4041
// Arrange
41-
var model = new RootModel
42+
var model = new NestingRootModel
4243
{
43-
//BuiltInType = "test",
44-
CustomType = new ChildModel
44+
NestedProperty = new NestedModel
4545
{
46-
//BuiltInType = "test",
47-
CustomType = new SubChildModel()
48-
}
46+
NestedProperty = new SubNestedModel()
47+
},
48+
TestInt = 1
4949
};
5050

5151
// Act
@@ -54,7 +54,24 @@ public void Validate_HierarchicalModel_NestedNullAttributeException()
5454

5555
// Assert
5656
Assert.That(ex.Message,
57-
Does.StartWith($"Required property '{nameof(SubChildModel.BuiltInType)}' is null or empty"));
57+
Does.StartWith($"Required property '{nameof(SubNestedModel.BuiltInType)}' is null or empty"));
58+
}
59+
60+
[Test]
61+
public void Validate_InheritedProperty_NestedNullAttributeException()
62+
{
63+
// Arrange
64+
var model = new InheritanceRootModel
65+
{
66+
NestedProperty = new BaseNestedModel()
67+
};
68+
69+
// Act
70+
var ex = Assert.Throws<ModelValidationException>(() => _validator.Validate(model, null));
71+
72+
// Assert
73+
Assert.That(ex.Message,
74+
Does.StartWith($"Required property '{nameof(BaseNestedModel.BuiltInType)}' is null or empty"));
5875
}
5976
}
6077
}

src/Simplify.Web.Tests/TestEntities/HierarchyValidation/BaseModel.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Simplify.Web.Tests/TestEntities/HierarchyValidation/ChildModel.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Simplify.Web.Tests/TestEntities/HierarchyValidation/RootModel.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Simplify.Web.Tests/TestEntities/HierarchyValidation/SubChildModel.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Simplify.Web.Model.Validation.Attributes;
2+
3+
#nullable disable
4+
5+
namespace Simplify.Web.Tests.TestEntities.Inheritance
6+
{
7+
public class BaseModel
8+
{
9+
[Required]
10+
public BaseNestedModel NestedProperty { get; set; }
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Simplify.Web.Model.Validation.Attributes;
2+
3+
#nullable disable
4+
5+
namespace Simplify.Web.Tests.TestEntities.Inheritance
6+
{
7+
public class BaseNestedModel
8+
{
9+
[Required]
10+
public string BuiltInType { get; set; }
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#nullable disable
2+
3+
namespace Simplify.Web.Tests.TestEntities.Inheritance
4+
{
5+
public class InheritanceRootModel : BaseModel
6+
{
7+
}
8+
}

src/Simplify.Web.Tests/TestEntities/HierarchyValidation/ISubChildModel.cs renamed to src/Simplify.Web.Tests/TestEntities/Nesting/ISubNestedModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Simplify.Web.Model.Validation.Attributes;
22

3-
namespace Simplify.Web.Tests.TestEntities.HierarchyValidation
3+
namespace Simplify.Web.Tests.TestEntities.Nesting
44
{
5-
public interface ISubChildModel
5+
public interface ISubNestedModel
66
{
77
[Required]
88
string BuiltInType { get; set; }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#nullable disable
2+
3+
using Simplify.Web.Model.Validation.Attributes;
4+
5+
namespace Simplify.Web.Tests.TestEntities.Nesting
6+
{
7+
public class NestedModel
8+
{
9+
[Required]
10+
public ISubNestedModel NestedProperty { get; set; }
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable disable
2+
3+
using Simplify.Web.Model.Validation.Attributes;
4+
5+
namespace Simplify.Web.Tests.TestEntities.Nesting
6+
{
7+
public class NestingRootModel
8+
{
9+
public NestedModel NestedProperty { get; set; }
10+
11+
[Required]
12+
public int? TestInt { get; set; }
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#nullable disable
2+
3+
namespace Simplify.Web.Tests.TestEntities.Nesting
4+
{
5+
public class SubNestedModel : ISubNestedModel
6+
{
7+
public string BuiltInType { get; set; }
8+
}
9+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ private static void Validate(Type type, object? value, IDIResolver resolver)
2828
{
2929
var properties = type.GetProperties();
3030

31+
if (type.BaseType != null && type.BaseType != typeof(object))
32+
Validate(type.BaseType, value, resolver);
33+
3134
foreach (var item in properties)
3235
{
3336
var currentItemValue = item.GetValue(value);

0 commit comments

Comments
 (0)