-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEndsWithCheckAttribute.cs
More file actions
55 lines (50 loc) · 2.14 KB
/
EndsWithCheckAttribute.cs
File metadata and controls
55 lines (50 loc) · 2.14 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
using System;
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 ends with the provided string value.
/// </summary>
public sealed class EndsWithAttribute : ParameterCheckAttribute
{
/// <summary>
/// Initialises a new <see cref="EndsWithAttribute" /> with the specified string value and
/// <see cref="StringComparison.OrdinalIgnoreCase" />.
/// </summary>
/// <param name="value"> The string value. </param>
public EndsWithAttribute(string value)
: this(value, StringComparison.OrdinalIgnoreCase)
{
}
/// <summary>
/// Initialises a new <see cref="EndsWithAttribute" /> with the specified string value and
/// <see cref="System.StringComparison" />.
/// </summary>
/// <param name="value"> The string value. </param>
/// <param name="comparison"> The <see cref="System.StringComparison" /> used for comparison. </param>
public EndsWithAttribute(string value, StringComparison comparison)
: base(Utilities.IsStringType)
{
Value = value;
StringComparison = comparison;
}
/// <summary>
/// Gets the required string value.
/// </summary>
public string Value { get; }
/// <summary>
/// Gets the <see cref="System.StringComparison" /> used for comparison.
/// </summary>
public StringComparison StringComparison { get; }
/// <inheritdoc />
public override ValueTask<CheckResult> CheckAsync(object argument, CommandContext context)
{
return (argument as string).EndsWith(Value, StringComparison)
? CheckResult.Successful
: CheckResult.Unsuccessful(
$"The provided argument must end with the {(StringComparison.IsCaseSensitive() ? "case-sensitive" : "case-insensitive")} value: {Value}.");
}
}
}