Repository navigation
Expand file tree
/
Copy pathStringRule.cs
More file actions
52 lines (43 loc) · 2.37 KB
/
Copy pathStringRule.cs
File metadata and controls
52 lines (43 loc) · 2.37 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
namespace CoreEx.Validation.Rules;
/// <summary>
/// Provides <see langword="string"/> validation including minimum and maximum length, and regular expression.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <param name="minLength">The minimum length.</param>
/// <param name="maxLength">The maximum length.</param>
/// <param name="regex">The regular expression.</param>
public sealed class StringRule<TEntity>(Func<PropertyContext<TEntity, string>, int>? minLength = null, Func<PropertyContext<TEntity, string>, int?>? maxLength = null, Func<PropertyContext<TEntity, string>, Regex?>? regex = null) : PropertyRuleBase<TEntity, string> where TEntity : class
{
private readonly Func<PropertyContext<TEntity, string>, int>? _minLength = minLength;
private readonly Func<PropertyContext<TEntity, string>, int?>? _maxLength = maxLength;
private readonly Func<PropertyContext<TEntity, string>, Regex?>? _regex = regex;
/// <inheritdoc/>
protected override Task OnValidateAsync(PropertyContext<TEntity, string> context, CancellationToken cancellationToken)
{
var minLength = _minLength?.Invoke(context) ?? 0;
var maxLength = _maxLength?.Invoke(context);
var regex = _regex?.Invoke(context);
if (minLength < 0)
throw new InvalidOperationException($"Minimum length must be zero or greater.");
if (maxLength is not null && maxLength.Value <= 0)
throw new InvalidOperationException($"Maximum length must be greater than zero.");
if (minLength > 0 && maxLength.HasValue && minLength == maxLength!.Value && context.Value.Length != minLength)
{
context.AddError(ErrorText ?? ValidatorStrings.ExactLengthFormat, minLength);
return Task.CompletedTask;
}
if (context.Value.Length < minLength)
{
context.AddError(ErrorText ?? ValidatorStrings.MinLengthFormat, minLength);
return Task.CompletedTask;
}
if (maxLength.HasValue && context.Value.Length > maxLength.Value)
{
context.AddError(ErrorText ?? ValidatorStrings.MaxLengthFormat, maxLength);
return Task.CompletedTask;
}
if (regex is not null && !regex.IsMatch(context.Value))
context.AddError(ErrorText ?? ValidatorStrings.RegexFormat);
return Task.CompletedTask;
}
}