-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringValueObject.cs
More file actions
27 lines (20 loc) · 1.03 KB
/
Copy pathStringValueObject.cs
File metadata and controls
27 lines (20 loc) · 1.03 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
using System.Text.RegularExpressions;
public abstract record StringValueObject(int MinLength, int MaxLength)
{
protected Regex ValidationRegex { get; } = CreateDefaultRegex(MinLength, MaxLength);
public string Value { get; }
protected StringValueObject(string value, int minLength, int maxLength)
: this(minLength, maxLength)
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Value cannot be empty", nameof(value));
if (value.Length < MinLength || value.Length > MaxLength)
throw new ArgumentException($"Value must be between {MinLength} and {MaxLength} characters", nameof(value));
if (!ValidationRegex.IsMatch(value))
throw new ArgumentException($"Value does not match required pattern", nameof(value));
Value = value;
}
protected static Regex CreateDefaultRegex(int minLength, int maxLength) => new(
$@"^[\p{{L}}\p{{M}}\p{{N}}]{{{minLength},{maxLength}}}$",
RegexOptions.Singleline | RegexOptions.Compiled);
}