Repository navigation
Expand file tree
/
Copy pathValidationExtensions.CollectionRule.cs
More file actions
136 lines (125 loc) · 12.5 KB
/
Copy pathValidationExtensions.CollectionRule.cs
File metadata and controls
136 lines (125 loc) · 12.5 KB
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
namespace CoreEx.Validation;
public static partial class ValidationExtensions
{
/// <summary>
/// Chains a collection (<see cref="CollectionRule{TEntity, TProperty, TItem}"/>) validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The <see cref="IEnumerable{T}"/> item <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> to chain to.</param>
/// <param name="with">Extends configuration <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
public static CollectionRule<TEntity, IEnumerable<TItem>, TItem> Collection<TEntity, TItem>(this IPropertyRule<TEntity, IEnumerable<TItem?>> rule, Func<CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With, CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With> with) where TEntity : class
=> (CollectionRule<TEntity, IEnumerable<TItem>, TItem>)Chain(rule, new CollectionRule<TEntity, IEnumerable<TItem>, TItem>(null, null, with));
/// <summary>
/// Chains a collection (<see cref="CollectionRule{TEntity, TProperty, TItem}"/>) validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The <see cref="IEnumerable{T}"/> item <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> to chain to.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="with">Extends configuration <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
public static CollectionRule<TEntity, IEnumerable<TItem>, TItem> Collection<TEntity, TItem>(this IPropertyRule<TEntity, IEnumerable<TItem?>> rule, int maxCount, Func<CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With, CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With>? with = null) where TEntity : class
=> (CollectionRule<TEntity, IEnumerable<TItem>, TItem>)Chain(rule, new CollectionRule<TEntity, IEnumerable<TItem>, TItem>(null, _ => maxCount, with));
/// <summary>
/// Chains a collection (<see cref="CollectionRule{TEntity, TProperty, TItem}"/>) validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The <see cref="IEnumerable{T}"/> item <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> to chain to.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="with">Extends configuration <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
public static CollectionRule<TEntity, IEnumerable<TItem>, TItem> Collection<TEntity, TItem>(this IPropertyRule<TEntity, IEnumerable<TItem?>> rule, Func<PropertyContext<TEntity, IEnumerable<TItem>>, int?>? maxCount, Func<CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With, CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With>? with = null) where TEntity : class
=> (CollectionRule<TEntity, IEnumerable<TItem>, TItem>)Chain(rule, new CollectionRule<TEntity, IEnumerable<TItem>, TItem>(null, maxCount, with));
/// <summary>
/// Chains a collection (<see cref="CollectionRule{TEntity, TProperty, TItem}"/>) validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The <see cref="IEnumerable{T}"/> item <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> to chain to.</param>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="with">Extends configuration <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
public static CollectionRule<TEntity, IEnumerable<TItem>, TItem> Collection<TEntity, TItem>(this IPropertyRule<TEntity, IEnumerable<TItem?>> rule, int minCount, int? maxCount, Func<CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With, CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With>? with = null) where TEntity : class
=> (CollectionRule<TEntity, IEnumerable<TItem>, TItem>)Chain(rule, new CollectionRule<TEntity, IEnumerable<TItem>, TItem>(_ => minCount, _ => maxCount, with));
/// <summary>
/// Chains a collection (<see cref="CollectionRule{TEntity, TProperty, TItem}"/>) validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The <see cref="IEnumerable{T}"/> item <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> to chain to.</param>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="with">Extends configuration <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
public static CollectionRule<TEntity, IEnumerable<TItem>, TItem> Collection<TEntity, TItem>(this IPropertyRule<TEntity, IEnumerable<TItem?>> rule, Func<PropertyContext<TEntity, IEnumerable<TItem>>, int>? minCount, Func<PropertyContext<TEntity, IEnumerable<TItem>>, int?>? maxCount, Func<CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With, CollectionRule<TEntity, IEnumerable<TItem>, TItem>.With>? with = null) where TEntity : class
=> (CollectionRule<TEntity, IEnumerable<TItem>, TItem>)Chain(rule, new CollectionRule<TEntity, IEnumerable<TItem>, TItem>(minCount, maxCount, with));
/* WithDuplicateCheck-extensions */
/// <summary>
/// Sets the duplicate check based on the <see cref="IIdentifierCore.Id"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="with">The <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
/// <param name="duplicateText">The duplicate <see cref="LText"/> to be used in the error message.</param>
/// <returns>The <paramref name="with"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="duplicateText"/> defaults to <see cref="ValidatorStrings.IdentifierText"/>.</remarks>
public static CollectionRule<TEntity, TProperty, TItem>.With WithDuplicateIdCheck<TEntity, TProperty, TItem>(this CollectionRule<TEntity, TProperty, TItem>.With with, LText? duplicateText = null) where TEntity : class where TProperty : IEnumerable<TItem> where TItem : IIdentifierCore
=> with.ThrowIfNull().WithDuplicateCheckingInternal(item => item.EntityKey, CompositeKeyComparer.Default, () => duplicateText ?? ValidatorStrings.IdentifierText);
/// <summary>
/// Sets the duplicate check based on the <see cref="IEntityKey.EntityKey"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="with">The <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
/// <param name="duplicateText">The duplicate <see cref="LText"/> to be used in the error message.</param>
/// <returns>The <paramref name="with"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="duplicateText"/> defaults to <see cref="ValidatorStrings.KeyText"/>.</remarks>
public static CollectionRule<TEntity, TProperty, TItem>.With WithDuplicateKeyCheck<TEntity, TProperty, TItem>(this CollectionRule<TEntity, TProperty, TItem>.With with, LText? duplicateText = null) where TEntity : class where TProperty : IEnumerable<TItem> where TItem : IEntityKey
=> with.ThrowIfNull().WithDuplicateCheckingInternal(item => item.EntityKey, CompositeKeyComparer.Default, () => duplicateText ?? ValidatorStrings.KeyText);
/// <summary>
/// Sets the duplicate check based on the <paramref name="propertyExpression"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <typeparam name="TItemProperty">The item property <see cref="Type"/>.</typeparam>
/// <param name="with">The <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
/// <param name="propertyExpression">The <see cref="Expression"/> to reference the entity property.</param>
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/>.</param>
/// <param name="duplicateText">The duplicate <see cref="LText"/> to be used in the error message.</param>
/// <returns>The <paramref name="with"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="duplicateText"/> defaults to the resulting <paramref name="propertyExpression"/> <see cref="IPropertyRuntimeMetadata.Text"/>.</remarks>
public static CollectionRule<TEntity, TProperty, TItem>.With WithDuplicatePropertyCheck<TEntity, TProperty, TItem, TItemProperty>(this CollectionRule<TEntity, TProperty, TItem>.With with, Expression<Func<TItem, TItemProperty>> propertyExpression, IEqualityComparer<TItemProperty>? comparer = null, LText? duplicateText = null) where TEntity : class where TProperty : IEnumerable<TItem> where TItem : class
{
var dcp = RuntimeMetadata.GetForExpression(propertyExpression.ThrowIfNull());
return with.ThrowIfNull().WithDuplicateCheckingInternal(dcp.GetValue<TItemProperty>, comparer, () => duplicateText ??= dcp.Text);
}
/// <summary>
/// Sets the duplicate check based on the item value (<see cref="IEquatable{T}"/>).
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="with">The <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
/// <param name="duplicateText">The duplicate <see cref="LText"/> to be used in the error message.</param>
/// <param name="comparer">The equality comparer.</param>
/// <returns>The <paramref name="with"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="duplicateText"/> defaults to <see cref="ValidatorStrings.KeyText"/>.</remarks>
public static CollectionRule<TEntity, TProperty, TItem>.With WithDuplicateCheck<TEntity, TProperty, TItem>(this CollectionRule<TEntity, TProperty, TItem>.With with, IEqualityComparer<TItem>? comparer = null, LText? duplicateText = null) where TEntity : class where TProperty : IEnumerable<TItem> where TItem : IEquatable<TItem>
=> with.ThrowIfNull().WithDuplicateCheckingInternal(item => item, comparer, () => duplicateText ?? ValidatorStrings.ItemText);
/// <summary>
/// Sets the duplicate check based on the <paramref name="keySelector"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <typeparam name="TKey">The key <see cref="Type"/>.</typeparam>
/// <param name="with">The <see cref="CollectionRule{TEntity, TProperty, TItem}.With"/>.</param>
/// <param name="keySelector">The key selector.</param>
/// <param name="comparer">The equality comparer.</param>
/// <param name="duplicateText">The duplicate <see cref="LText"/> to be used in the error message.</param>
/// <returns>The <paramref name="with"/> to support fluent-style method-chaining.</returns>
public static CollectionRule<TEntity, TProperty, TItem>.With WithDuplicateCheck<TEntity, TProperty, TItem, TKey>(this CollectionRule<TEntity, TProperty, TItem>.With with, Func<TItem, TKey> keySelector, IEqualityComparer<TKey>? comparer = null, LText? duplicateText = null) where TEntity : class where TProperty : IEnumerable<TItem>
=> with.ThrowIfNull().WithDuplicateCheckingInternal(keySelector, comparer, () => duplicateText ?? ValidatorStrings.ItemText);
}