-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRegexCheckAttribute.cs
More file actions
47 lines (43 loc) · 1.97 KB
/
RegexCheckAttribute.cs
File metadata and controls
47 lines (43 loc) · 1.97 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
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Ultz.Extensions.Commands.Context;
using Ultz.Extensions.Commands.Results.User;
namespace Ultz.Extensions.Commands.Attributes.Checks.Bundled.String
{
/// <summary>
/// Represents a parameter check that ensures the provided string argument matches the provided
/// <see cref="System.Text.RegularExpressions.Regex" /> pattern.
/// </summary>
public sealed class RegexAttribute : ParameterCheckAttribute
{
/// <summary>
/// Initialises a new <see cref="RegexAttribute" /> with the specified <see cref="System.Text.RegularExpressions.Regex" />
/// pattern.
/// </summary>
/// <param name="pattern"> The <see cref="System.Text.RegularExpressions.Regex" /> pattern. </param>
public RegexAttribute(string pattern)
: this(pattern, RegexOptions.Compiled)
{
}
/// <summary>
/// Initialises a new <see cref="RegexAttribute" /> with the specified <see cref="System.Text.RegularExpressions.Regex" />
/// pattern and <see cref="RegexOptions" />.
/// </summary>
/// <param name="pattern"> The <see cref="System.Text.RegularExpressions.Regex" /> pattern. </param>
/// <param name="options"> The <see cref="RegexOptions" />. </param>
public RegexAttribute(string pattern, RegexOptions options)
: base(Utilities.IsStringType)
{
Regex = new Regex(pattern, options);
}
/// <summary>
/// Gets the required <see cref="System.Text.RegularExpressions.Regex" />.
/// </summary>
public Regex Regex { get; }
/// <inheritdoc />
public override ValueTask<CheckResult> CheckAsync(object argument, CommandContext context)
=> Regex.IsMatch(argument as string)
? CheckResult.Successful
: CheckResult.Unsuccessful($"The provided argument must match the regex pattern: {Regex}.");
}
}