@@ -41,17 +41,16 @@ [new RequiredAttribute()])
41
41
{ typeof ( Address ) , addressType }
42
42
} ) ;
43
43
44
- var context = new ValidateContext
45
- {
46
- ValidationOptions = validationOptions ,
47
- } ;
48
-
49
44
var personWithMissingRequiredFields = new Person
50
45
{
51
46
Age = 150 , // Invalid age
52
47
Address = new Address ( ) // Missing required City and Street
53
48
} ;
54
- context . ValidationContext = new ValidationContext ( personWithMissingRequiredFields ) ;
49
+ var context = new ValidateContext
50
+ {
51
+ ValidationOptions = validationOptions ,
52
+ ValidationContext = new ValidationContext ( personWithMissingRequiredFields )
53
+ } ;
55
54
56
55
// Act
57
56
await personType . ValidateAsync ( personWithMissingRequiredFields , context , default ) ;
@@ -96,21 +95,20 @@ [new RequiredAttribute()]),
96
95
[ ] )
97
96
] ) ;
98
97
99
- var context = new ValidateContext
100
- {
101
- ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
102
- {
103
- { typeof ( Employee ) , employeeType }
104
- } )
105
- } ;
106
-
107
98
var employee = new Employee
108
99
{
109
100
Name = "John Doe" ,
110
101
Department = "IT" ,
111
102
Salary = - 5000 // Negative salary will trigger IValidatableObject validation
112
103
} ;
113
- context . ValidationContext = new ValidationContext ( employee ) ;
104
+ var context = new ValidateContext
105
+ {
106
+ ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
107
+ {
108
+ { typeof ( Employee ) , employeeType }
109
+ } ) ,
110
+ ValidationContext = new ValidationContext ( employee )
111
+ } ;
114
112
115
113
// Act
116
114
await employeeType . ValidateAsync ( employee , context , default ) ;
@@ -142,22 +140,21 @@ [new RequiredAttribute()])
142
140
[ new RangeAttribute ( 2 , 5 ) ] )
143
141
] ) ;
144
142
143
+ var car = new Car
144
+ {
145
+ // Missing Make and Model (required in base type)
146
+ Doors = 7 // Invalid number of doors
147
+ } ;
145
148
var context = new ValidateContext
146
149
{
147
150
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
148
151
{
149
152
{ typeof ( Vehicle ) , baseType } ,
150
153
{ typeof ( Car ) , derivedType }
151
- } )
154
+ } ) ,
155
+ ValidationContext = new ValidationContext ( car )
152
156
} ;
153
157
154
- var car = new Car
155
- {
156
- // Missing Make and Model (required in base type)
157
- Doors = 7 // Invalid number of doors
158
- } ;
159
- context . ValidationContext = new ValidationContext ( car ) ;
160
-
161
158
// Act
162
159
await derivedType . ValidateAsync ( car , context , default ) ;
163
160
@@ -203,15 +200,6 @@ [new RequiredAttribute()]),
203
200
[ ] )
204
201
] ) ;
205
202
206
- var context = new ValidateContext
207
- {
208
- ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
209
- {
210
- { typeof ( OrderItem ) , itemType } ,
211
- { typeof ( Order ) , orderType }
212
- } )
213
- } ;
214
-
215
203
var order = new Order
216
204
{
217
205
OrderNumber = "ORD-12345" ,
@@ -222,7 +210,15 @@ [new RequiredAttribute()]),
222
210
new OrderItem { ProductName = "Another Product" , Quantity = 200 /* Invalid quantity */ }
223
211
]
224
212
} ;
225
- context . ValidationContext = new ValidationContext ( order ) ;
213
+ var context = new ValidateContext
214
+ {
215
+ ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
216
+ {
217
+ { typeof ( OrderItem ) , itemType } ,
218
+ { typeof ( Order ) , orderType }
219
+ } ) ,
220
+ ValidationContext = new ValidationContext ( order )
221
+ } ;
226
222
227
223
// Act
228
224
await orderType . ValidateAsync ( order , context , default ) ;
@@ -260,20 +256,19 @@ public async Task Validate_HandlesNullValues_Appropriately()
260
256
[ ] )
261
257
] ) ;
262
258
259
+ var person = new Person
260
+ {
261
+ Name = null ,
262
+ Address = null
263
+ } ;
263
264
var context = new ValidateContext
264
265
{
265
266
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
266
267
{
267
268
{ typeof ( Person ) , personType }
268
- } )
269
- } ;
270
-
271
- var person = new Person
272
- {
273
- Name = null ,
274
- Address = null
269
+ } ) ,
270
+ ValidationContext = new ValidationContext ( person )
275
271
} ;
276
- context . ValidationContext = new ValidationContext ( person ) ;
277
272
278
273
// Act
279
274
await personType . ValidateAsync ( person , context , default ) ;
@@ -305,12 +300,6 @@ [new RequiredAttribute()]),
305
300
} ) ;
306
301
validationOptions . MaxDepth = 3 ; // Set a small max depth to trigger the limit
307
302
308
- var context = new ValidateContext
309
- {
310
- ValidationOptions = validationOptions ,
311
- ValidationErrors = [ ]
312
- } ;
313
-
314
303
// Create a deep tree with circular references
315
304
var rootNode = new TreeNode { Name = "Root" } ;
316
305
var level1 = new TreeNode { Name = "Level1" , Parent = rootNode } ;
@@ -328,7 +317,12 @@ [new RequiredAttribute()]),
328
317
// Add a circular reference
329
318
level5 . Children . Add ( rootNode ) ;
330
319
331
- context . ValidationContext = new ValidationContext ( rootNode ) ;
320
+ var context = new ValidateContext
321
+ {
322
+ ValidationOptions = validationOptions ,
323
+ ValidationErrors = [ ] ,
324
+ ValidationContext = new ValidationContext ( rootNode )
325
+ } ;
332
326
333
327
// Act + Assert
334
328
var exception = await Assert . ThrowsAsync < InvalidOperationException > (
@@ -349,17 +343,16 @@ public async Task Validate_HandlesCustomValidationAttributes()
349
343
CreatePropertyInfo ( typeof ( Product ) , typeof ( string ) , "SKU" , "SKU" , [ new RequiredAttribute ( ) , new CustomSkuValidationAttribute ( ) ] ) ,
350
344
] ) ;
351
345
346
+ var product = new Product { SKU = "INVALID-SKU" } ;
352
347
var context = new ValidateContext
353
348
{
354
349
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
355
350
{
356
351
{ typeof ( Product ) , productType }
357
- } )
352
+ } ) ,
353
+ ValidationContext = new ValidationContext ( product )
358
354
} ;
359
355
360
- var product = new Product { SKU = "INVALID-SKU" } ;
361
- context . ValidationContext = new ValidationContext ( product ) ;
362
-
363
356
// Act
364
357
await productType . ValidateAsync ( product , context , default ) ;
365
358
@@ -385,17 +378,16 @@ public async Task Validate_HandlesMultipleErrorsOnSameProperty()
385
378
] )
386
379
] ) ;
387
380
381
+ var user = new User { Password = "abc" } ; // Too short and not complex enough
388
382
var context = new ValidateContext
389
383
{
390
384
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
391
385
{
392
386
{ typeof ( User ) , userType }
393
- } )
387
+ } ) ,
388
+ ValidationContext = new ValidationContext ( user )
394
389
} ;
395
390
396
- var user = new User { Password = "abc" } ; // Too short and not complex enough
397
- context . ValidationContext = new ValidationContext ( user ) ;
398
-
399
391
// Act
400
392
await userType . ValidateAsync ( user , context , default ) ;
401
393
@@ -429,23 +421,22 @@ public async Task Validate_HandlesMultiLevelInheritance()
429
421
CreatePropertyInfo ( typeof ( DerivedEntity ) , typeof ( string ) , "Name" , "Name" , [ new RequiredAttribute ( ) ] )
430
422
] ) ;
431
423
424
+ var entity = new DerivedEntity
425
+ {
426
+ Name = "" , // Invalid: required
427
+ CreatedAt = DateTime . Now . AddDays ( 1 ) // Invalid: future date
428
+ } ;
432
429
var context = new ValidateContext
433
430
{
434
431
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
435
432
{
436
433
{ typeof ( BaseEntity ) , baseType } ,
437
434
{ typeof ( IntermediateEntity ) , intermediateType } ,
438
435
{ typeof ( DerivedEntity ) , derivedType }
439
- } )
436
+ } ) ,
437
+ ValidationContext = new ValidationContext ( entity )
440
438
} ;
441
439
442
- var entity = new DerivedEntity
443
- {
444
- Name = "" , // Invalid: required
445
- CreatedAt = DateTime . Now . AddDays ( 1 ) // Invalid: future date
446
- } ;
447
- context . ValidationContext = new ValidationContext ( entity ) ;
448
-
449
440
// Act
450
441
await derivedType . ValidateAsync ( entity , context , default ) ;
451
442
@@ -475,17 +466,16 @@ public async Task Validate_RequiredOnPropertyShortCircuitsOtherValidations()
475
466
[ new RequiredAttribute ( ) , new PasswordComplexityAttribute ( ) ] )
476
467
] ) ;
477
468
469
+ var user = new User { Password = null } ; // Invalid: required
478
470
var context = new ValidateContext
479
471
{
480
472
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
481
473
{
482
474
{ typeof ( User ) , userType }
483
- } )
475
+ } ) ,
476
+ ValidationContext = new ValidationContext ( user ) // Invalid: required
484
477
} ;
485
478
486
- var user = new User { Password = null } ; // Invalid: required
487
- context . ValidationContext = new ValidationContext ( user ) ;
488
-
489
479
// Act
490
480
await userType . ValidateAsync ( user , context , default ) ;
491
481
@@ -503,18 +493,17 @@ public async Task Validate_IValidatableObject_WithZeroAndMultipleMemberNames_Beh
503
493
var globalType = new TestValidatableTypeInfo (
504
494
typeof ( GlobalErrorObject ) ,
505
495
[ ] ) ; // no properties – nothing sets MemberName
496
+ var globalErrorInstance = new GlobalErrorObject { Data = - 1 } ;
506
497
507
498
var context = new ValidateContext
508
499
{
509
500
ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
510
501
{
511
502
{ typeof ( GlobalErrorObject ) , globalType }
512
- } )
503
+ } ) ,
504
+ ValidationContext = new ValidationContext ( globalErrorInstance )
513
505
} ;
514
506
515
- var globalErrorInstance = new GlobalErrorObject { Data = - 1 } ;
516
- context . ValidationContext = new ValidationContext ( globalErrorInstance ) ;
517
-
518
507
await globalType . ValidateAsync ( globalErrorInstance , context , default ) ;
519
508
520
509
Assert . NotNull ( context . ValidationErrors ) ;
0 commit comments