-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathValidationExtensions.NumericRule.cs
More file actions
64 lines (57 loc) · 4.67 KB
/
Copy pathValidationExtensions.NumericRule.cs
File metadata and controls
64 lines (57 loc) · 4.67 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
namespace CoreEx.Validation;
public static partial class ValidationExtensions
{
/// <summary>
/// Chains a <paramref name="allowNegatives"/> numeric value (<see cref="NumericRule{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="allowNegatives">Indicates whether to allow negative values.</param>
public static IPropertyRule<TEntity, TProperty> Numeric<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, bool allowNegatives = true) where TEntity : class where TProperty : INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(_ => allowNegatives));
/// <summary>
/// Chains a <paramref name="allowNegatives"/> numeric value (<see cref="NumericRule{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="allowNegatives">Indicates whether to allow negative values.</param>
public static IPropertyRule<TEntity, TProperty> Numeric<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule, Func<PropertyContext<TEntity, TProperty>, bool> allowNegatives) where TEntity : class where TProperty : INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(allowNegatives));
/// <summary>
/// Chains a positive-only numeric value (<see cref="NumericRule{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>
public static IPropertyRule<TEntity, TProperty> Positive<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty> rule) where TEntity : class where TProperty : INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(allowNegatives: _ => false));
/* Nullable/Struct */
/// <summary>
/// Chains a <paramref name="allowNegatives"/> numeric value (<see cref="NumericRule{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="allowNegatives">Indicates whether to allow negative values.</param>
public static IPropertyRule<TEntity, TProperty> Numeric<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, bool allowNegatives = true) where TEntity : class where TProperty : struct, INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(_ => allowNegatives));
/// <summary>
/// Chains a <paramref name="allowNegatives"/> numeric value (<see cref="NumericRule{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="allowNegatives">Indicates whether to allow negative values.</param>
public static IPropertyRule<TEntity, TProperty> Numeric<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule, Func<PropertyContext<TEntity, TProperty>, bool> allowNegatives) where TEntity : class where TProperty : struct, INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(allowNegatives));
/// <summary>
/// Chains a positive-only numeric value (<see cref="NumericRule{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>
public static IPropertyRule<TEntity, TProperty> Positive<TEntity, TProperty>(this IPropertyRule<TEntity, TProperty?> rule) where TEntity : class where TProperty : struct, INumber<TProperty>
=> Chain(rule, new NumericRule<TEntity, TProperty>(allowNegatives: _ => false));
}