-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SharedConditionTests.cs
690 lines (578 loc) · 24.1 KB
/
SharedConditionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#region License
// Copyright (c) .NET Foundation and contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
#endregion
#pragma warning disable 1998
namespace FluentValidation.Tests {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Results;
public class SharedConditionTests {
class SharedConditionValidator : AbstractValidator<Person> {
public SharedConditionValidator() {
// Start with a predicate to group rules together.
//
// The AbstractValidator appends this predicate
// to each inner RuleFor so you only need write,
// maintain, and think about it in one place.
//
// You can finish with an Unless clause that will
// void the validation for the entire set when it's
// predicate is true.
//
When(x => x.Id > 0, () => {
RuleFor(x => x.Forename).NotEmpty();
RuleFor(x => x.Surname).NotEmpty().Equal("Smith");
});
}
}
class SharedAsyncConditionValidator : AbstractValidator<Person> {
public SharedAsyncConditionValidator() {
// Start with a predicate to group rules together.
//
// The AbstractValidator appends this predicate
// to each inner RuleFor so you only need write,
// maintain, and think about it in one place.
//
// You can finish with an Unless clause that will
// void the validation for the entire set when it's
// predicate is true.
//
WhenAsync(async (x,c) => x.Id > 0,
() => {
RuleFor(x => x.Forename).NotEmpty();
RuleFor(x => x.Surname).NotEmpty().Equal("Smith");
}
);
}
}
class SharedConditionWithScopedUnlessValidator : AbstractValidator<Person> {
public SharedConditionWithScopedUnlessValidator() {
// inner RuleFor() calls can contain their own,
// locally scoped When and Unless calls that
// act only on that individual RuleFor() yet the
// RuleFor() respects the grouped When() and
// Unless() predicates.
//
When(x => x.Id > 0 && x.Age <= 65, () => { RuleFor(x => x.Orders.Count).Equal(0).Unless(x => String.IsNullOrWhiteSpace(x.CreditCard) == false); });
//.Unless(x => x.Age > 65);
}
}
class SharedAsyncConditionWithScopedUnlessValidator : AbstractValidator<Person> {
public SharedAsyncConditionWithScopedUnlessValidator() {
// inner RuleFor() calls can contain their own,
// locally scoped When and Unless calls that
// act only on that individual RuleFor() yet the
// RuleFor() respects the grouped When() and
// Unless() predicates.
//
WhenAsync(async (x,c) => x.Id > 0 && x.Age <= 65,
() => {
RuleFor(x => x.Orders.Count).Equal(0).UnlessAsync(async (x,c) => String.IsNullOrWhiteSpace(x.CreditCard) == false);
}
);
}
}
class SharedConditionInverseValidator : AbstractValidator<Person> {
public SharedConditionInverseValidator() {
Unless(x => x.Id == 0, () => { RuleFor(x => x.Forename).NotNull(); });
}
}
class SharedAsyncConditionInverseValidator : AbstractValidator<Person>
{
public SharedAsyncConditionInverseValidator()
{
UnlessAsync(async (x,c) => x.Id == 0, () => { RuleFor(x => x.Forename).NotNull(); });
}
}
class BadValidatorDisablesNullCheck : AbstractValidator<string> {
public BadValidatorDisablesNullCheck() {
When(x => x != null, () => {
RuleFor(x => x).Must(x => x != "foo");
});
}
protected override void EnsureInstanceNotNull(object instanceToValidate) {
//bad.
}
}
class AsyncBadValidatorDisablesNullCheck : AbstractValidator<string> {
public AsyncBadValidatorDisablesNullCheck() {
When(x => x != null, () => {
RuleFor(x => x).Must(x => x != "foo");
});
WhenAsync(async (x, ct) => x != null, () => {
RuleFor(x => x).Must(x => x != "foo");
});
}
protected override void EnsureInstanceNotNull(object instanceToValidate) {
//bad.
}
}
[Fact]
public void Shared_When_is_not_applied_to_grouped_rules_when_initial_predicate_is_false() {
var validator = new SharedConditionValidator();
var person = new Person(); // fails the shared When predicate
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public async Task Shared_async_When_is_not_applied_to_grouped_rules_when_initial_predicate_is_false() {
var validator = new SharedAsyncConditionValidator();
var person = new Person(); // fails the shared When predicate
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public void Shared_When_is_applied_to_grouped_rules_when_initial_predicate_is_true() {
var validator = new SharedConditionValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(3);
}
[Fact]
public async Task Shared_async_When_is_applied_to_grouped_rules_when_initial_predicate_is_true() {
var validator = new SharedAsyncConditionValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(3);
}
[Fact]
public void Shared_When_is_applied_to_groupd_rules_when_initial_predicate_is_true_and_all_individual_rules_are_satisfied() {
var validator = new SharedConditionValidator();
var person = new Person() {
Id = 4, // triggers the shared When predicate
Forename = "Kevin", // satisfies RuleFor( x => x.Forename ).NotEmpty()
Surname = "Smith", // satisfies RuleFor( x => x.Surname ).NotEmpty().Equal( "Smith" )
};
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public async Task Shared_async_When_is_applied_to_groupd_rules_when_initial_predicate_is_true_and_all_individual_rules_are_satisfied() {
var validator = new SharedAsyncConditionValidator();
var person = new Person() {
Id = 4, // triggers the shared When predicate
Forename = "Kevin", // satisfies RuleFor( x => x.Forename ).NotEmpty()
Surname = "Smith", // satisfies RuleFor( x => x.Surname ).NotEmpty().Equal( "Smith" )
};
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public void Shared_When_respects_the_smaller_scope_of_an_inner_Unless_when_the_inner_Unless_predicate_is_satisfied() {
var validator = new SharedConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
person.CreditCard = "1234123412341234"; // satisfies the inner Unless predicate
person.Orders.Add(new Order());
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public async Task Shared_async_When_respects_the_smaller_scope_of_an_inner_Unless_when_the_inner_Unless_predicate_is_satisfied() {
var validator = new SharedAsyncConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
person.CreditCard = "1234123412341234"; // satisfies the inner Unless predicate
person.Orders.Add(new Order());
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public void Shared_When_respects_the_smaller_scope_of_a_inner_Unless_when_the_inner_Unless_predicate_fails() {
var validator = new SharedConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
person.Orders.Add(new Order()); // fails the inner Unless predicate
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(1);
}
[Fact]
public async Task Shared_async_When_respects_the_smaller_scope_of_a_inner_Unless_when_the_inner_Unless_predicate_fails() {
var validator = new SharedAsyncConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4 // triggers the shared When predicate
};
person.Orders.Add(new Order()); // fails the inner Unless predicate
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(1);
}
[Fact]
public void Outer_Unless_clause_will_trump_an_inner_Unless_clause_when_inner_fails_but_the_outer_is_satisfied() {
var validator = new SharedConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4, // triggers the shared When predicate
Age = 70 // satisfies the outer Unless predicate
};
person.Orders.Add(new Order()); // fails the inner Unless predicate
var result = validator.Validate(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public async Task Outer_async_Unless_clause_will_trump_an_inner_Unless_clause_when_inner_fails_but_the_outer_is_satisfied() {
var validator = new SharedAsyncConditionWithScopedUnlessValidator();
var person = new Person() {
Id = 4, // triggers the shared When predicate
Age = 70 // satisfies the outer Unless predicate
};
person.Orders.Add(new Order()); // fails the inner Unless predicate
var result = await validator.ValidateAsync(person);
result.Errors.Count.ShouldEqual(0);
}
[Fact]
public void Condition_can_be_used_inside_ruleset() {
var validator = new TestValidator();
validator.RuleSet("foo", () => { validator.When(x => x.Id > 0, () => { validator.RuleFor(x => x.Forename).NotNull(); }); });
validator.RuleFor(x => x.Surname).NotNull();
#pragma warning disable 618
var result = validator.Validate(new Person {Id = 5}, v => v.IncludeRuleSets("foo"));
#pragma warning restore 618
result.Errors.Count.ShouldEqual(1);
result.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public async Task Async_condition_can_be_used_inside_ruleset() {
var validator = new TestValidator();
validator.RuleSet("foo", () => {
validator.WhenAsync(async (x,c) => (x.Id > 0), () => {
validator.RuleFor(x => x.Forename).NotNull();
});
});
validator.RuleFor(x => x.Surname).NotNull();
var result = await validator.ValidateAsync(new Person {Id = 5}, v => v.IncludeRuleSets("foo"));
result.Errors.Count.ShouldEqual(1);
result.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public void RuleSet_can_be_used_inside_condition() {
var validator = new TestValidator();
validator.When(x => x.Id > 0, () => { validator.RuleSet("foo", () => { validator.RuleFor(x => x.Forename).NotNull(); }); });
validator.RuleFor(x => x.Surname).NotNull();
var result = validator.Validate(new Person {Id = 5}, v => v.IncludeRuleSets("foo"));
result.Errors.Count.ShouldEqual(1);
result.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public async Task RuleSet_can_be_used_inside_async_condition() {
var validator = new TestValidator();
validator.WhenAsync(async (x,c) => (x.Id > 0), () => { validator.RuleSet("foo", () => { validator.RuleFor(x => x.Forename).NotNull(); }); });
validator.RuleFor(x => x.Surname).NotNull();
var result = await validator.ValidateAsync(new Person {Id = 5}, v => v.IncludeRuleSets("foo"));
result.Errors.Count.ShouldEqual(1);
result.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public void Rules_invoke_when_inverse_shared_condition_matches() {
var validator = new SharedConditionInverseValidator();
var result = validator.Validate(new Person {Id = 1});
result.IsValid.ShouldBeFalse();
}
[Fact]
public async Task Rules_invoke_when_inverse_shared_async_condition_matches() {
var validator = new SharedAsyncConditionInverseValidator();
var result = await validator.ValidateAsync(new Person {Id = 1});
result.IsValid.ShouldBeFalse();
}
[Fact]
public void Rules_not_invoked_when_inverse_shared_condition_does_not_match() {
var validator = new SharedConditionInverseValidator();
var result = validator.Validate(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Rules_not_invoked_when_inverse_shared_async_condition_does_not_match() {
var validator = new SharedAsyncConditionInverseValidator();
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Does_not_execute_custom_Rule_when_condition_false() {
var validator = new TestValidator();
validator.When(x => false, () => {
validator.RuleFor(x=>x).Custom((x,ctx)=> ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = validator.Validate(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Does_not_execute_custom_Rule_when_async_condition_false() {
var validator = new TestValidator();
validator.WhenAsync(async (x,c) =>(false), () => {
validator.RuleFor(x=>x).Custom((x,ctx)=> ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public void Does_not_execute_customasync_Rule_when_condition_false()
{
var validator = new TestValidator();
validator.When(x => false, () => {
validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = validator.Validate(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Does_not_execute_customasync_Rule_when_async_condition_false() {
var validator = new TestValidator();
validator.WhenAsync(async (x,c) =>(false), () => {
validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public void Executes_custom_rule_when_condition_true() {
var validator = new TestValidator();
validator.When(x => true, () => {
validator.RuleFor(x=>x).Custom((x,ctx) => ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = validator.Validate(new Person());
result.IsValid.ShouldBeFalse();
}
[Fact]
public async Task Executes_custom_rule_when_async_condition_true() {
var validator = new TestValidator();
validator.WhenAsync(async (x,c) =>(true), () => {
validator.RuleFor(x=>x).Custom((x,ctx) => ctx.AddFailure(new ValidationFailure("foo", "bar")));
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeFalse();
}
[Fact]
public async Task Executes_customasync_rule_when_condition_true() {
var validator = new TestValidator();
validator.When(x => true, () => validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("foo", "bar"))));
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeFalse();
}
[Fact]
public async Task Executes_customasync_rule_when_async_condition_true() {
var validator = new TestValidator();
validator.WhenAsync(async (x,c) =>(true), () => validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("foo", "bar"))));
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeFalse();
}
[Fact]
public void Nested_conditions_with_Custom_rule() {
var validator = new TestValidator();
validator.When(x => true, () => {
validator.When(x => false, () => {
validator.RuleFor(x=>x).Custom((x,ctx) => ctx.AddFailure(new ValidationFailure("Custom", "The validation failed")));
});
});
var result = validator.Validate(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Nested_async_conditions_with_Custom_rule() {
var validator = new TestValidator();
validator.When(x => true, () => {
validator.WhenAsync(async (x,c) =>(false), () => {
validator.RuleFor(x=>x).Custom((x,ctx) => ctx.AddFailure(new ValidationFailure("Custom", "The validation failed")));
});
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Nested_conditions_with_CustomAsync_rule() {
var validator = new TestValidator();
validator.When(x => true, () => {
validator.When(x => false, () => {
validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("Custom", "The validation failed")));
});
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Nested_async_conditions_with_CustomAsync_rule() {
var validator = new TestValidator();
validator.When(x => true, () => {
validator.WhenAsync(async (x,c) =>(false), () => {
validator.RuleFor(x=>x).CustomAsync(async (x,ctx,c) => ctx.AddFailure(new ValidationFailure("Custom", "The validation failed")));
});
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeTrue();
}
[Fact]
public void When_condition_only_executed_once() {
var validator = new TestValidator();
int executions = 0;
validator.When(x => {
executions++;
return x.Age > 10;
}, () => {
validator.RuleFor(x => x.Surname).NotNull();
validator.RuleFor(x => x.Forename).NotNull();
});
validator.Validate(new Person {Age = 11});
executions.ShouldEqual(1);
}
[Fact]
public async Task WhenAsync_condition_only_executed_once() {
var validator = new TestValidator();
int executions = 0;
validator.WhenAsync(async (x, ct) => {
executions++;
return x.Age > 10;
}, () => {
validator.RuleFor(x => x.Surname).NotNull();
validator.RuleFor(x => x.Forename).NotNull();
});
await validator.ValidateAsync(new Person {Age = 11});
executions.ShouldEqual(1);
}
[Fact]
public void Runs_otherwise_conditions_for_When() {
var validator = new TestValidator();
validator.When(x => x.Age > 10, () => {
validator.RuleFor(x => x.Forename).NotNull();
}).Otherwise(() => {
validator.RuleFor(x => x.Surname).NotNull();
});
var result1 = validator.Validate(new Person {Age = 11});
result1.Errors.Single().PropertyName.ShouldEqual("Forename");
var result2 = validator.Validate(new Person {Age = 9});
result2.Errors.Single().PropertyName.ShouldEqual("Surname");
}
[Fact]
public void Runs_otherwise_conditons_for_Unless() {
var validator = new TestValidator();
validator.Unless(x => x.Age > 10, () => {
validator.RuleFor(x => x.Forename).NotNull();
}).Otherwise(() => {
validator.RuleFor(x => x.Surname).NotNull();
});
var result1 = validator.Validate(new Person {Age = 11});
result1.Errors.Single().PropertyName.ShouldEqual("Surname");
var result2 = validator.Validate(new Person {Age = 9});
result2.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public async Task Runs_otherwise_conditions_for_WhenAsync() {
var validator = new TestValidator();
validator.WhenAsync(async (x, ct) => x.Age > 10, () => {
validator.RuleFor(x => x.Forename).NotNull();
}).Otherwise(() => {
validator.RuleFor(x => x.Surname).NotNull();
});
var result1 = await validator.ValidateAsync(new Person {Age = 11});
result1.Errors.Single().PropertyName.ShouldEqual("Forename");
var result2 = await validator.ValidateAsync(new Person {Age = 9});
result2.Errors.Single().PropertyName.ShouldEqual("Surname");
}
[Fact]
public async Task Runs_otherwise_conditions_for_UnlessAsync() {
var validator = new TestValidator();
validator.UnlessAsync(async (x, ct) => x.Age > 10, () => {
validator.RuleFor(x => x.Forename).NotNull();
}).Otherwise(() => {
validator.RuleFor(x => x.Surname).NotNull();
});
var result1 = await validator.ValidateAsync(new Person {Age = 11});
result1.Errors.Single().PropertyName.ShouldEqual("Surname");
var result2 = await validator.ValidateAsync(new Person {Age = 9});
result2.Errors.Single().PropertyName.ShouldEqual("Forename");
}
[Fact]
public void Nested_when_inside_otherwise() {
var validator = new InlineValidator<Person>();
validator.When(x => x.Id == 1, () => {
validator.RuleFor(x => x.Forename).NotNull();
}).Otherwise(() => {
validator.When(x => x.Age > 18, () => {
validator.RuleFor(x => x.Email).NotNull();
});
});
var result = validator.Validate(new Person() {Id = 1});
result.Errors.Count.ShouldEqual(1);
result.Errors[0].PropertyName.ShouldEqual("Forename");
result = validator.Validate(new Person() {Id = 2, Age = 20});
result.Errors.Count.ShouldEqual(1);
result.Errors[0].PropertyName.ShouldEqual("Email");
}
[Fact]
public void When_condition_executed_for_each_instance_of_RuleForEach_condition_should_not_be_cached() {
var person = new Person {
Children = new List<Person> {
new Person { Id = 1},
new Person { Id = 0}
}
};
var childValidator = new InlineValidator<Person>();
int executions = 0;
childValidator.When(a => {
executions++;
return a.Id != 0;
}, () => {
childValidator.RuleFor(a => a.Id).Equal(1);
});
var personValidator = new InlineValidator<Person>();
personValidator.RuleForEach(p => p.Children).SetValidator(childValidator);
var validationResult = personValidator.Validate(person);
validationResult.IsValid.ShouldBeTrue();
executions.ShouldEqual(2);
}
[Fact]
public async Task When_async_condition_executed_for_each_instance_of_RuleForEach_condition_should_not_be_cached() {
var person = new Person {
Children = new List<Person> {
new Person { Id = 1},
new Person { Id = 0}
}
};
var childValidator = new InlineValidator<Person>();
int executions = 0;
childValidator.WhenAsync(async (a, ct) => {
executions++;
return a.Id != 0;
}, () => {
childValidator.RuleFor(a => a.Id).Equal(1);
});
var personValidator = new InlineValidator<Person>();
personValidator.RuleForEach(p => p.Children).SetValidator(childValidator);
var validationResult = await personValidator.ValidateAsync(person);
validationResult.IsValid.ShouldBeTrue();
executions.ShouldEqual(2);
}
[Fact]
public void Doesnt_throw_NullReferenceException_when_instance_not_null() {
var v = new BadValidatorDisablesNullCheck();
var result = v.Validate((string) null);
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Doesnt_throw_NullReferenceException_when_instance_not_null_async() {
var v = new AsyncBadValidatorDisablesNullCheck();
var result = await v.ValidateAsync((string) null);
result.IsValid.ShouldBeTrue();
}
}
}