@@ -15,6 +15,8 @@ public class ValidatableTypeInfoTests
15
15
public async Task Validate_ValidatesComplexType_WithNestedProperties ( )
16
16
{
17
17
// Arrange
18
+ List < ValidationErrorContext > validationErrors = [ ] ;
19
+
18
20
var personType = new TestValidatableTypeInfo (
19
21
typeof ( Person ) ,
20
22
[
@@ -52,6 +54,8 @@ [new RequiredAttribute()])
52
54
ValidationContext = new ValidationContext ( personWithMissingRequiredFields )
53
55
} ;
54
56
57
+ context . OnValidationError += validationErrors . Add ;
58
+
55
59
// Act
56
60
await personType . ValidateAsync ( personWithMissingRequiredFields , context , default ) ;
57
61
@@ -78,12 +82,43 @@ [new RequiredAttribute()])
78
82
Assert . Equal ( "Address.City" , kvp . Key ) ;
79
83
Assert . Equal ( "The City field is required." , kvp . Value . First ( ) ) ;
80
84
} ) ;
85
+
86
+ Assert . Collection ( validationErrors ,
87
+ context =>
88
+ {
89
+ Assert . Equal ( "Name" , context . Name ) ;
90
+ Assert . Equal ( "Name" , context . Path ) ;
91
+ Assert . Equal ( "The Name field is required." , context . Errors . Single ( ) ) ;
92
+ Assert . Same ( context . Container , personWithMissingRequiredFields ) ;
93
+ } ,
94
+ context =>
95
+ {
96
+ Assert . Equal ( "Age" , context . Name ) ;
97
+ Assert . Equal ( "Age" , context . Path ) ;
98
+ Assert . Equal ( "The field Age must be between 0 and 120." , context . Errors . Single ( ) ) ;
99
+ Assert . Same ( context . Container , personWithMissingRequiredFields ) ;
100
+ } ,
101
+ context =>
102
+ {
103
+ Assert . Equal ( "Street" , context . Name ) ;
104
+ Assert . Equal ( "Address.Street" , context . Path ) ;
105
+ Assert . Equal ( "The Street field is required." , context . Errors . Single ( ) ) ;
106
+ Assert . Same ( context . Container , personWithMissingRequiredFields . Address ) ;
107
+ } ,
108
+ context =>
109
+ {
110
+ Assert . Equal ( "City" , context . Name ) ;
111
+ Assert . Equal ( "Address.City" , context . Path ) ;
112
+ Assert . Equal ( "The City field is required." , context . Errors . Single ( ) ) ;
113
+ Assert . Same ( context . Container , personWithMissingRequiredFields . Address ) ;
114
+ } ) ;
81
115
}
82
116
83
117
[ Fact ]
84
118
public async Task Validate_HandlesIValidatableObject_Implementation ( )
85
119
{
86
120
// Arrange
121
+ var validationErrors = new List < ValidationErrorContext > ( ) ;
87
122
var employeeType = new TestValidatableTypeInfo (
88
123
typeof ( Employee ) ,
89
124
[
@@ -110,6 +145,8 @@ [new RequiredAttribute()]),
110
145
ValidationContext = new ValidationContext ( employee )
111
146
} ;
112
147
148
+ context . OnValidationError += validationErrors . Add ;
149
+
113
150
// Act
114
151
await employeeType . ValidateAsync ( employee , context , default ) ;
115
152
@@ -118,6 +155,12 @@ [new RequiredAttribute()]),
118
155
var error = Assert . Single ( context . ValidationErrors ) ;
119
156
Assert . Equal ( "Salary" , error . Key ) ;
120
157
Assert . Equal ( "Salary must be a positive value." , error . Value . First ( ) ) ;
158
+
159
+ var errorContext = Assert . Single ( validationErrors ) ;
160
+ Assert . Equal ( "Salary" , errorContext . Name ) ;
161
+ Assert . Equal ( "Salary" , errorContext . Path ) ;
162
+ Assert . Equal ( "Salary must be a positive value." , errorContext . Errors . Single ( ) ) ;
163
+ Assert . Same ( errorContext . Container , employee ) ;
121
164
}
122
165
123
166
[ Fact ]
0 commit comments