Repository navigation
Expand file tree
/
Copy pathValidationExtensions.CompareValuesRule.cs
More file actions
54 lines (49 loc) · 4.78 KB
/
Copy pathValidationExtensions.CompareValuesRule.cs
File metadata and controls
54 lines (49 loc) · 4.78 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
namespace CoreEx.Validation;
public static partial class ValidationExtensions
{
/// <summary>
/// Chains a <paramref name="values"/> comparison (<see cref="CompareValuesRule{TEntity, TProperty}"/>) validation.
/// </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="values">The compare-to value(s).</param>
/// <param name="comparer">The optional <see cref="IEqualityComparer{T}"/>.</param>
/// <param name="overrideValueWhereMatched">Indicates whether to override the underlying property value with the corresponding matched value.</param>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, IEnumerable<TProperty> values, IEqualityComparer<TProperty>? comparer = null, bool overrideValueWhereMatched = false) where TEntity : class where TProperty : IEquatable<TProperty>
=> Chain(rule, new CompareValuesRule<TEntity, TProperty>(_ => values, comparer, overrideValueWhereMatched));
/// <summary>
/// Chains a <paramref name="values"/> comparison (<see cref="CompareValuesRule{TEntity, TProperty}"/>) validation.
/// </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="values">The function to get the compare-to value(s).</param>
/// <param name="comparer">The optional <see cref="IEqualityComparer{T}"/>.</param>
/// <param name="overrideValueWhereMatched">Indicates whether to override the underlying property value with the corresponding matched value.</param>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, Func<PropertyContext<TEntity, TProperty>, IEnumerable<TProperty>> values, IEqualityComparer<TProperty>? comparer = null, bool overrideValueWhereMatched = false) where TEntity : class where TProperty : IEquatable<TProperty>
=> Chain(rule, new CompareValuesRule<TEntity, TProperty>(c => values.ThrowIfNull()(c), comparer, overrideValueWhereMatched));
/* Nullable/Struct */
/// <summary>
/// Chains a <paramref name="values"/> comparison (<see cref="CompareValuesRule{TEntity, TProperty}"/>) validation.
/// </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="values">The compare-to value(s).</param>
/// <param name="comparer">The optional <see cref="IEqualityComparer{T}"/>.</param>
/// <param name="overrideValueWhereMatched">Indicates whether to override the underlying property value with the corresponding matched value.</param>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, IEnumerable<TProperty> values, IEqualityComparer<TProperty>? comparer = null, bool overrideValueWhereMatched = false) where TEntity : class where TProperty : struct, IEquatable<TProperty>
=> Chain(rule, new CompareValuesRule<TEntity, TProperty>(_ => values, comparer, overrideValueWhereMatched));
/// <summary>
/// Chains a <paramref name="values"/> comparison (<see cref="CompareValuesRule{TEntity, TProperty}"/>) validation.
/// </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="values">The function to get the compare-to value(s).</param>
/// <param name="comparer">The optional <see cref="IEqualityComparer{T}"/>.</param>
/// <param name="overrideValueWhereMatched">Indicates whether to override the underlying property value with the corresponding matched value.</param>
public static IPropertyRule<TEntity, TProperty> CompareValues<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, Func<PropertyContext<TEntity, TProperty>, IEnumerable<TProperty>> values, IEqualityComparer<TProperty>? comparer = null, bool overrideValueWhereMatched = false) where TEntity : class where TProperty : struct, IEquatable<TProperty>
=> Chain(rule, new CompareValuesRule<TEntity, TProperty>(c => values.ThrowIfNull()(c), comparer, overrideValueWhereMatched));
}