generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ValidationExtensions.cs
1796 lines (1607 loc) · 150 KB
/
ValidationExtensions.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Http;
using CoreEx.Localization;
using CoreEx.RefData;
using CoreEx.Results;
using CoreEx.Validation.Clauses;
using CoreEx.Validation.Rules;
using CoreEx.Wildcards;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Validation
{
/// <summary>
/// Provides extension methods required by the validation framework (including support for fluent-style method chaining).
/// </summary>
public static class ValidationExtensions
{
#region Text
/// <summary>
/// Updates the rule friendly name text used in validation messages (see <see cref="IPropertyRule.Text"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="text">The text for the rule.</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Text<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText text) where TEntity : class
{
rule.ThrowIfNull(nameof(rule)).Text = text;
return rule;
}
#endregion
#region When
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> where the <typeparamref name="TEntity"/> <paramref name="predicate"/> must be <c>true</c> for the rule to be validated.
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">A function to determine whether the preceeding rule is to be validated.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> When<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TEntity> predicate) where TEntity : class
{
if (predicate == null)
return rule;
rule.ThrowIfNull(nameof(rule)).AddClause(new WhenClause<TEntity, TProperty>(predicate));
return rule;
}
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> where the <typeparamref name="TProperty"/> <paramref name="predicate"/> must be <c>true</c> for the rule to be validated.
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">A function to determine whether the preceeding rule is to be validated.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> WhenValue<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TProperty> predicate) where TEntity : class
{
if (predicate == null)
return rule;
rule.ThrowIfNull(nameof(rule)).AddClause(new WhenClause<TEntity, TProperty>(predicate));
return rule;
}
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> where the <typeparamref name="TProperty"/> must have a value (i.e. not the default value for the Type) for the rule to be validated.
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> WhenHasValue<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule) where TEntity : class
=> WhenValue(rule, (TProperty pv) => Comparer<TProperty>.Default.Compare(pv, default!) != 0);
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> which must be <c>true</c> for the rule to be validated.
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="when">A function to determine whether the preceeding rule is to be validated.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> When<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<bool> when) where TEntity : class
{
if (when == null)
return rule;
rule.ThrowIfNull(nameof(rule)).AddClause(new WhenClause<TEntity, TProperty>(when));
return rule;
}
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> which must be <c>true</c> for the rule to be validated.
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="when">A <see cref="bool"/> to determine whether the preceeding rule is to be validated.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> When<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool when) where TEntity : class
=> When(rule, () => when);
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> that states that the
/// <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext"/> <see cref="ExecutionContext.OperationType"/> is equal to the specified
/// (<paramref name="operationType"/>).
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="operationType">The <see cref="OperationType"/>.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> WhenOperation<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, OperationType operationType) where TEntity : class
=> When(rule, x => ExecutionContext.Current.OperationType == operationType);
/// <summary>
/// Adds a <see cref="WhenClause{TEntity, TProperty}"/> to this <see cref="IPropertyRule{TEntity, TProperty}"/> that states that the
/// <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext"/> <see cref="ExecutionContext.OperationType"/> is not equal to the specified
/// (<paramref name="operationType"/>).
/// </summary>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="operationType">The <see cref="OperationType"/>.</param>
/// <returns>The <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> WhenNotOperation<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, OperationType operationType) where TEntity : class
=> When(rule, x => ExecutionContext.Current.OperationType != operationType);
#endregion
#region Mandatory
/// <summary>
/// Adds a mandatory validation (<see cref="MandatoryRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Mandatory<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MandatoryRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds a not empty validation (<see cref="MandatoryRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
/// <remarks>This is a synonym for <see cref="Mandatory{TEntity, TProperty}(IPropertyRule{TEntity, TProperty}, Localization.LText?)"/>.</remarks>
public static IPropertyRule<TEntity, TProperty> NotEmpty<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MandatoryRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds a not <c>null</c> validation (<see cref="NotNullRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> NotNull<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NotNullRule<TEntity, TProperty> { ErrorText = errorText });
#endregion
#region None
/// <summary>
/// Adds a none validation (<see cref="NoneRule{TEntity, TProperty}"/>) where it is expected that the value equals its default.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> None<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NoneRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds an empty validation (<see cref="NoneRule{TEntity, TProperty}"/>) where it is expected that the value equals its default.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
/// <remarks>This is a synonym for <see cref="None{TEntity, TProperty}(IPropertyRule{TEntity, TProperty}, Localization.LText?)"/>.</remarks>
public static IPropertyRule<TEntity, TProperty> Empty<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NoneRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds a <c>null</c> validation (<see cref="NoneRule{TEntity, TProperty}"/>) where it is expected that the value is <c>null</c>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Null<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NullRule<TEntity, TProperty> { ErrorText = errorText });
#endregion
#region Must
/// <summary>
/// Adds a validation where the rule <paramref name="predicate"/> <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">The must predicate.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Must<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TEntity> predicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MustRule<TEntity, TProperty>(predicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="must"/> function <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="must">The must function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Must<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<bool> must, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MustRule<TEntity, TProperty>(must) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="must"/> value be <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="must">The must value.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Must<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool must, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MustRule<TEntity, TProperty>(() => must) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="mustAsync"/> function <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="mustAsync">The must function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> MustAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<CancellationToken, Task<bool>> mustAsync, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new MustRule<TEntity, TProperty>(mustAsync) { ErrorText = errorText });
#endregion
#region Exists
/// <summary>
/// Adds a validation where the rule <paramref name="predicate"/> <b>exists</b> return <c>true</c> to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">The exists predicate.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Exists<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TEntity> predicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>(predicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="exists"/> function <b>exists</b> return <c>true</c> to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="exists">The exists function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ExistsAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<bool>> exists, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>(exists) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="exists"/> resultant value is <c>true</c> to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="exists">The exists value.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Exists<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool exists, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>((_, __) => Task.FromResult(exists)) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="exists"/> function must return <b>not null</b> to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="exists">The exists function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <remarks>Where the resultant value is an <see cref="IResult"/> then existence is confirmed when <see cref="IResult.IsSuccess"/> and the the underlying <see cref="IResult.Value"/> is not null.</remarks>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ValueExistsAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<object?>> exists, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>(exists) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="exists"/> resultant value is <b>not null</b> to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="exists">The exists function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <remarks>Where the resultant value is an <see cref="IResult"/> then existence is confirmed when <see cref="IResult.IsSuccess"/> and the the underlying <see cref="IResult.Value"/> is not null.</remarks>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ValueExists<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, object? exists, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>((_, __) => Task.FromResult(exists != null)) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="agentResult"/> function must return a successful response to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="agentResult">The <see cref="HttpResult"/> function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> AgentExistsAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<HttpResult>> agentResult, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>(async (v, ct) => await agentResult(v, ct).ConfigureAwait(false)) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="agentResult"/> function must return a successful response to verify it exists (see <see cref="ExistsRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TAgentValue">The corresponding <see cref="HttpResult{T}"/> <see cref="HttpResult{T}.Value"/> <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="agentResult">The <see cref="HttpResult"/> function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> AgentExistsAsync<TEntity, TProperty, TAgentValue>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<HttpResult<TAgentValue>>> agentResult, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ExistsRule<TEntity, TProperty>(async (v, ct) => await agentResult(v, ct).ConfigureAwait(false)) { ErrorText = errorText });
#endregion
#region Duplicate
/// <summary>
/// Adds a validation where the rule <paramref name="predicate"/> <b>must</b> return <c>false</c> to not be considered a duplicate (see <see cref="DuplicateRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">The must predicate.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Duplicate<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TEntity> predicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new DuplicateRule<TEntity, TProperty>(predicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="duplicate"/> function <b>must</b> return <c>false</c> to not be considered a duplicate (see <see cref="DuplicateRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="duplicate">The duplicate function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Duplicate<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<bool> duplicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new DuplicateRule<TEntity, TProperty>(duplicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="duplicate"/> value must be <c>false</c> to not be considered a duplicate (see <see cref="DuplicateRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="duplicate">The duplicate value.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Duplicate<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool duplicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new DuplicateRule<TEntity, TProperty>(() => duplicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where considered a duplicate (see <see cref="DuplicateRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Duplicate<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new DuplicateRule<TEntity, TProperty>(() => true) { ErrorText = errorText });
#endregion
#region Immutable
/// <summary>
/// Adds a validation where the rule <paramref name="predicate"/> <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="predicate">The must predicate.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Immutable<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Predicate<TEntity> predicate, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ImmutableRule<TEntity, TProperty>(predicate) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="immutable"/> function <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="immutable">The must function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Immutable<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<bool> immutable, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ImmutableRule<TEntity, TProperty>(immutable) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="immutableAsync"/> function <b>must</b> return <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="immutableAsync">The must function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ImmutableAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<CancellationToken, Task<bool>> immutableAsync, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ImmutableRule<TEntity, TProperty>(immutableAsync) { ErrorText = errorText });
/// <summary>
/// Adds a validation where the rule <paramref name="immutable"/> value be <c>true</c> to be considered valid (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="immutable">The must value.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Immutable<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool immutable, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ImmutableRule<TEntity, TProperty>(() => immutable) { ErrorText = errorText });
/// <summary>
/// Adds a validation where considered immutable (see <see cref="MustRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Immutable<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ImmutableRule<TEntity, TProperty>(() => false) { ErrorText = errorText });
#endregion
#region Between
/// <summary>
/// Adds a between comparision validation against a specified from and to value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValue">The compare from value.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareFromText">The compare from text to be passed for the error message (default is to use <paramref name="compareFromValue"/>).</param>
/// <param name="compareToText">The compare to text to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="exclusiveBetween">Indicates whether the between comparison is exclusive or inclusive (default).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Between<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareFromValue, TProperty compareToValue, LText? compareFromText = null, LText? compareToText = null, bool exclusiveBetween = false, LText ? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValue, compareToValue, compareFromText, compareToText, exclusiveBetween) { ErrorText = errorText });
/// <summary>
/// Adds a between comparision validation against from and to values returned by functions (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValueFunction">The compare from value function.</param>
/// <param name="compareToValueFunction">The compare to value function.</param>
/// <param name="compareFromTextFunction">The compare from text function (default is to use the result of the <paramref name="compareFromValueFunction"/>).</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="exclusiveBetween">Indicates whether the between comparison is exclusive or inclusive (default).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Between<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareFromValueFunction, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareFromTextFunction = null, Func<TEntity, LText>? compareToTextFunction = null, bool exclusiveBetween = false, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValueFunction, compareToValueFunction, compareFromTextFunction, compareToTextFunction, exclusiveBetween) { ErrorText = errorText });
/// <summary>
/// Adds a between comparision validation against from and to values returned by async functions (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValueFunctionAsync">The compare from value function.</param>
/// <param name="compareToValueFunctionAsync">The compare to value function.</param>
/// <param name="compareFromTextFunction">The compare from text function (default is to use the result of the <paramref name="compareFromValueFunctionAsync"/>).</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunctionAsync"/>).</param>
/// <param name="exclusiveBetween">Indicates whether the between comparison is exclusive or inclusive (default).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> BetweenAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<TProperty>> compareFromValueFunctionAsync, Func<TEntity, CancellationToken, Task<TProperty>> compareToValueFunctionAsync, Func<TEntity, LText>? compareFromTextFunction = null, Func<TEntity, LText>? compareToTextFunction = null, bool exclusiveBetween = false, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValueFunctionAsync, compareToValueFunctionAsync, compareFromTextFunction, compareToTextFunction, exclusiveBetween) { ErrorText = errorText });
/// <summary>
/// Adds an inclusive between comparision validation against a specified from and to value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValue">The compare from value.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareFromText">The compare from text to be passed for the error message (default is to use <paramref name="compareFromValue"/>).</param>
/// <param name="compareToText">The compare to text to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> InclusiveBetween<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareFromValue, TProperty compareToValue, LText? compareFromText = null, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValue, compareToValue, compareFromText, compareToText, false) { ErrorText = errorText });
/// <summary>
/// Adds a inclusive between comparision validation against from and to values returned by functions (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValueFunction">The compare from value function.</param>
/// <param name="compareToValueFunction">The compare to value function.</param>
/// <param name="compareFromTextFunction">The compare from text function (default is to use the result of the <paramref name="compareFromValueFunction"/>).</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> InclusiveBetween<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareFromValueFunction, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareFromTextFunction = null, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValueFunction, compareToValueFunction, compareFromTextFunction, compareToTextFunction, false) { ErrorText = errorText });
/// <summary>
/// Adds an exclusive between comparision validation against a specified from and to value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValue">The compare from value.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareFromText">The compare from text to be passed for the error message (default is to use <paramref name="compareFromValue"/>).</param>
/// <param name="compareToText">The compare to text to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ExclusiveBetween<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareFromValue, TProperty compareToValue, LText? compareFromText = null, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValue, compareToValue, compareFromText, compareToText, true) { ErrorText = errorText });
/// <summary>
/// Adds a exclusive between comparision validation against from and to values returned by functions (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareFromValueFunction">The compare from value function.</param>
/// <param name="compareToValueFunction">The compare to value function.</param>
/// <param name="compareFromTextFunction">The compare from text function (default is to use the result of the <paramref name="compareFromValueFunction"/>).</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> ExclusiveBetween<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareFromValueFunction, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareFromTextFunction = null, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new BetweenRule<TEntity, TProperty>(compareFromValueFunction, compareToValueFunction, compareFromTextFunction, compareToTextFunction, true) { ErrorText = errorText });
#endregion
#region CompareValue
/// <summary>
/// Adds a comparision <see cref="CompareOperator.Equal"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Equal<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.Equal, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.Equal"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Equal<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.Equal, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.NotEqual"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> NotEqual<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.NotEqual, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.NotEqual"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> NotEqual<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.NotEqual, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.LessThan"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> LessThan<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.LessThan, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.LessThan"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> LessThan<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.LessThan, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.LessThanEqual"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> LessThanOrEqualTo<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.LessThanEqual, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.LessThanEqual"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> LessThanOrEqualTo<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.LessThanEqual, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.GreaterThan"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> GreaterThan<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.GreaterThan, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.GreaterThan"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> GreaterThan<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.GreaterThan, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.GreaterThanEqual"/> validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> GreaterThanOrEqualTo<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.GreaterThanEqual, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision <see cref="CompareOperator.GreaterThanEqual"/> validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> GreaterThanOrEqualTo<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(CompareOperator.GreaterThanEqual, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against a specified value (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareOperator">The <see cref="CompareOperator"/>.</param>
/// <param name="compareToValue">The compare to value.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToValue"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareValue<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, CompareOperator compareOperator, TProperty compareToValue, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(compareOperator, compareToValue, compareToText) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against a value returned by a function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareOperator">The <see cref="CompareOperator"/>.</param>
/// <param name="compareToValueFunction">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunction"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareValue<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, CompareOperator compareOperator, Func<TEntity, TProperty> compareToValueFunction, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(compareOperator, compareToValueFunction, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against a value returned by an async function (<see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareOperator">The <see cref="CompareOperator"/>.</param>
/// <param name="compareToValueFunctionAsync">The compare to function.</param>
/// <param name="compareToTextFunction">The compare to text function (default is to use the result of the <paramref name="compareToValueFunctionAsync"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareValueAsync<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, CompareOperator compareOperator, Func<TEntity, CancellationToken, Task<TProperty>> compareToValueFunctionAsync, Func<TEntity, LText>? compareToTextFunction = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValueRule<TEntity, TProperty>(compareOperator, compareToValueFunctionAsync, compareToTextFunction) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against one or more specified values (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValues">The compare to values.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, IEnumerable<TProperty> compareToValues, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValuesRule<TEntity, TProperty>(compareToValues) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against one or more specified values (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValuesFunctionAsync">The compare to values function.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<TEntity, CancellationToken, Task<IEnumerable<TProperty>>> compareToValuesFunctionAsync, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValuesRule<TEntity, TProperty>(compareToValuesFunctionAsync) { ErrorText = errorText });
/// <summary>
/// Adds a comparision validation against one or more specified values (see <see cref="CompareValueRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareToValues">The compare to values.</param>
/// <param name="ignoreCase">Indicates whether to ignore the casing of the value when comparing.</param>
/// <param name="overrideValue">Indicates whether to override the underlying property value with the corresponding matched value.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, string> CompareValues<TEntity>(this IPropertyRule<TEntity, string> rule, IEnumerable<string> compareToValues, bool ignoreCase, bool overrideValue = false, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new CompareValuesRule<TEntity, string>(compareToValues) { EqualityComparer = ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal, OverrideValue = overrideValue, ErrorText = errorText });
#endregion
#region CompareProperty
/// <summary>
/// Adds a comparision validation against a specified property (see <see cref="ComparePropertyRule{TEntity, TProperty, TProperty2}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TCompareProperty">The compare to property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="compareOperator">The <see cref="CompareOperator"/>.</param>
/// <param name="compareToPropertyExpression">The <see cref="Expression"/> to reference the compare to entity property.</param>
/// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to use <paramref name="compareToPropertyExpression"/>).</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, TProperty}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> CompareProperty<TEntity, TProperty, TCompareProperty>(this IPropertyRule<TEntity, TProperty> rule, CompareOperator compareOperator, Expression<Func<TEntity, TCompareProperty>> compareToPropertyExpression, LText? compareToText = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new ComparePropertyRule<TEntity, TProperty, TCompareProperty>(compareOperator, compareToPropertyExpression, compareToText) { ErrorText = errorText });
#endregion
#region String
/// <summary>
/// Adds a <see cref="string"/> validation with a maximum length (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="maxLength">The maximum string length.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string> rule, int maxLength, Regex? regex = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { MaxLength = maxLength, Regex = regex, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with a minimum and maximum length (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="minLength">The minimum string length.</param>
/// <param name="maxLength">The maximum string length.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string> rule, int minLength, int? maxLength, Regex? regex = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { MinLength = minLength, MaxLength = maxLength, Regex = regex, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with a <paramref name="regex"/> (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string> rule, Regex? regex = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { Regex = regex, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with a <paramref name="regex"/> (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> Matches<TEntity>(this IPropertyRule<TEntity, string> rule, Regex? regex = null, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { Regex = regex, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with an exact length (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="exactLength">The exact string length.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> Length<TEntity>(this IPropertyRule<TEntity, string> rule, int exactLength, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { MinLength = exactLength, MaxLength = exactLength, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with a minimum length (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="minimumLength">The minimum string length.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> MinimumLength<TEntity>(this IPropertyRule<TEntity, string> rule, int minimumLength, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { MinLength = minimumLength, ErrorText = errorText });
/// <summary>
/// Adds a <see cref="string"/> validation with a maximum length (see <see cref="StringRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="maximumLength">The maximum string length.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, string> MaximumLength<TEntity>(this IPropertyRule<TEntity, string> rule, int maximumLength, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new StringRule<TEntity> { MaxLength = maximumLength, ErrorText = errorText });
#endregion
#region Email
/// <summary>
/// Adds an e-mail validation (see <see cref="EmailRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="maxLength">The maximum string length for the e-mail address; defaults to 254.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
/// <remarks>The maximum length for an email address is '<c>254</c>' or '<c>256</c>' or '<c>320</c>' as per this <see href="https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address#:~:text=%20The%20length%20limits%20are%20as%20follows%3A%20,i.e.%2C%20example.com%20--%20254%20characters%20maximum.%20More%20">article</see>,
/// in the absense of a definitive answer '<c>254</c>' has been chosen as the default. As it appears there is no real correct answer, simply adjust accordingly.</remarks>
public static IPropertyRule<TEntity, string> Email<TEntity>(this IPropertyRule<TEntity, string> rule, int? maxLength = 254, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new EmailRule<TEntity> { MaxLength = maxLength, ErrorText = errorText });
/// <summary>
/// Adds an e-mail validation (see <see cref="EmailRule{TEntity}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, String}"/> being extended.</param>
/// <param name="maxLength">The maximum string length for the e-mail address; defaults to 254.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
/// <remarks>The maximum length for an email address is '<c>254</c>' or '<c>256</c>' or '<c>320</c>' as per this <see href="https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address#:~:text=%20The%20length%20limits%20are%20as%20follows%3A%20,i.e.%2C%20example.com%20--%20254%20characters%20maximum.%20More%20">article</see>,
/// in the absense of a definitive answer '<c>254</c>' has been chosen as the default. As it appears there is no real correct answer, simply adjust accordingly.</remarks>
public static IPropertyRule<TEntity, string> EmailAddress<TEntity>(this IPropertyRule<TEntity, string> rule, int? maxLength = 254, LText? errorText = null) where TEntity : class
=> rule.ThrowIfNull(nameof(rule)).AddRule(new EmailRule<TEntity> { MaxLength = maxLength, ErrorText = errorText });
#endregion
#region Enum
/// <summary>
/// Adds an <see cref="System.Enum"/> validation to ensure that the value has been defined (see <see cref="EnumRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> Enum<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class where TProperty : struct, Enum
=> rule.ThrowIfNull(nameof(rule)).AddRule(new EnumRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds an <see cref="System.Enum"/> validation to ensure that the value has been defined (see <see cref="NullableEnumRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, TProperty?> Enum<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, LText? errorText = null) where TEntity : class where TProperty : struct, Enum
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NullableEnumRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds an <see cref="System.Enum"/> validation to ensure that the value has been defined (see <see cref="EnumRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, TProperty> IsInEnum<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, LText? errorText = null) where TEntity : class where TProperty : struct, Enum
=> rule.ThrowIfNull(nameof(rule)).AddRule(new EnumRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Adds an <see cref="System.Enum"/> validation to ensure that the value has been defined (see <see cref="NullableEnumRule{TEntity, TProperty}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="errorText">The error message format text <see cref="LText"/> (overrides the default).</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>
public static IPropertyRule<TEntity, TProperty?> IsIsEnum<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, LText? errorText = null) where TEntity : class where TProperty : struct, Enum
=> rule.ThrowIfNull(nameof(rule)).AddRule(new NullableEnumRule<TEntity, TProperty> { ErrorText = errorText });
/// <summary>
/// Enables the addition of an <see cref="EnumValueRule{TEntity, TEnum}"/> using an <see cref="EnumValueRuleAs{TEntity}.As{TEnum}"/> to validate against a specified <see cref="System.Enum"/> <see cref="Type"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <returns>A <see cref="IPropertyRule{TEntity, String}"/>.</returns>