Repository navigation
Expand file tree
/
Copy pathValidationExtensions.StringRule.cs
More file actions
97 lines (89 loc) · 8 KB
/
Copy pathValidationExtensions.StringRule.cs
File metadata and controls
97 lines (89 loc) · 8 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
namespace CoreEx.Validation;
public static partial class ValidationExtensions
{
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="maxLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="maxLength">The maximum string length.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string?> rule, int maxLength) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(maxLength: _ => maxLength));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="minLength"/> and <paramref name="maxLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> 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>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
/// <remarks>The <paramref name="regex"/> is executed directly against the value being validated; where the value may originate from an untrusted source, construct the <see cref="Regex"/> with an explicit
/// <c>matchTimeout</c> to guard against catastrophic backtracking (ReDoS), as a timeout cannot be applied to an already-constructed <see cref="Regex"/> instance.</remarks>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string?> rule, int minLength, int? maxLength, Regex? regex = null) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(minLength: _ => minLength, maxLength: _ => maxLength, regex: _ => regex));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="minLength"/> and <paramref name="maxLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> 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>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
/// <remarks>The <paramref name="regex"/> is executed directly against the value being validated; where the value may originate from an untrusted source, construct the <see cref="Regex"/> with an explicit
/// <c>matchTimeout</c> to guard against catastrophic backtracking (ReDoS), as a timeout cannot be applied to an already-constructed <see cref="Regex"/> instance.</remarks>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string?> rule, Func<PropertyContext<TEntity, string>, int>? minLength, Func<PropertyContext<TEntity, string>, int?>? maxLength, Func<PropertyContext<TEntity, string>, Regex?>? regex = null) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(minLength: minLength, maxLength: maxLength, regex: regex));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="regex"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
/// <remarks>The <paramref name="regex"/> is executed directly against the value being validated; where the value may originate from an untrusted source, construct the <see cref="Regex"/> with an explicit
/// <c>matchTimeout</c> to guard against catastrophic backtracking (ReDoS), as a timeout cannot be applied to an already-constructed <see cref="Regex"/> instance.</remarks>
public static IPropertyRule<TEntity, string> String<TEntity>(this IPropertyRule<TEntity, string?> rule, Regex regex) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(regex: _ => regex));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="regex"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="regex">The <see cref="Regex"/>.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
/// <remarks>The <paramref name="regex"/> is executed directly against the value being validated; where the value may originate from an untrusted source, construct the <see cref="Regex"/> with an explicit
/// <c>matchTimeout</c> to guard against catastrophic backtracking (ReDoS), as a timeout cannot be applied to an already-constructed <see cref="Regex"/> instance.</remarks>
public static IPropertyRule<TEntity, string> Matches<TEntity>(this IPropertyRule<TEntity, string?> rule, Regex regex) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(regex: _ => regex));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="exactLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="exactLength">The exact string length.</param>
/// <param name="regex">The optional <see cref="Regex"/>.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
public static IPropertyRule<TEntity, string> Length<TEntity>(this IPropertyRule<TEntity, string?> rule, int exactLength, Regex? regex = null) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(minLength: _ => exactLength, maxLength: _ => exactLength, regex: _ => regex));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="minLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="minLength">The minimum string length.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
public static IPropertyRule<TEntity, string> MinimumLength<TEntity>(this IPropertyRule<TEntity, string?> rule, int minLength) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(minLength: _ => minLength));
/// <summary>
/// Chains a <see langword="string"/> (<see cref="StringRule{TEntity}"/>) <paramref name="maxLength"/> validation to the existing <paramref name="rule"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="rule">The <see cref="IPropertyRule{TEntity, TProperty}"/> being extended.</param>
/// <param name="maxLength">The maximum string length.</param>
/// <returns>The <see cref="StringRule{TEntity}"/>.</returns>
public static IPropertyRule<TEntity, string> MaximumLength<TEntity>(this IPropertyRule<TEntity, string?> rule, int maxLength) where TEntity : class
=> Chain(rule, new StringRule<TEntity>(maxLength: _ => maxLength));
}