-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMinimumCheckAttribute.cs
More file actions
41 lines (38 loc) · 1.44 KB
/
MinimumCheckAttribute.cs
File metadata and controls
41 lines (38 loc) · 1.44 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
using System;
using System.Threading.Tasks;
using Ultz.Extensions.Commands.Context;
using Ultz.Extensions.Commands.Results.User;
namespace Ultz.Extensions.Commands.Attributes.Checks.Bundled
{
/// <summary>
/// Represents a parameter check that ensures the provided numeric/string argument has the minimum value/length.
/// </summary>
public sealed class MinimumAttribute : ParameterCheckAttribute
{
/// <summary>
/// Initialises a new <see cref="MinimumAttribute" /> with the specified minimum value.
/// </summary>
/// <param name="minimum"> The minimum value. </param>
public MinimumAttribute(double minimum)
: base(Utilities.IsNumericOrStringType)
{
Minimum = minimum;
}
/// <summary>
/// Gets the minimum value required.
/// </summary>
public double Minimum { get; }
/// <inheritdoc />
public override ValueTask<CheckResult> CheckAsync(object argument, CommandContext context)
{
var isString = argument is string;
var value = isString
? (argument as string).Length
: Convert.ToDouble(argument);
return value >= Minimum
? CheckResult.Successful
: CheckResult.Unsuccessful(
$"The provided argument must have a minimum {(isString ? "length" : "value")} of {Minimum}.");
}
}
}