-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUsernameAttribute.cs
More file actions
50 lines (40 loc) · 1.82 KB
/
UsernameAttribute.cs
File metadata and controls
50 lines (40 loc) · 1.82 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
using System.ComponentModel.DataAnnotations;
using OpenShock.Common.Validation;
namespace OpenShock.Common.DataAnnotations;
/// <summary>
/// An attribute used to validate whether a display name is valid.
/// </summary>
/// <remarks>
/// Inherits from <see cref="ValidationAttribute"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class UsernameAttribute : ValidationAttribute
{
/// <summary>
/// Example value used to generate OpenApi documentation.
/// </summary>
private const string ExampleValue = "String";
private const string ErrMsgCannotBeNull = "Username cannot be null";
private const string ErrMsgMustBeString = "Username must be a string";
/// <summary>
/// Indicates whether validation should be performed.
/// </summary>
public bool ShouldValidate { get; }
/// <summary>
/// Initializes a new instance of the <see cref="UsernameAttribute"/> class with the specified validation behavior.
/// </summary>
/// <param name="shouldValidate">True if validation should be performed; otherwise, false.</param>
public UsernameAttribute(bool shouldValidate) => ShouldValidate = shouldValidate;
/// <inheritdoc/>
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (!ShouldValidate) return ValidationResult.Success;
if (value is null) return new ValidationResult(ErrMsgCannotBeNull);
if (value is not string displayName) return new ValidationResult(ErrMsgMustBeString);
var result = UsernameValidator.Validate(displayName);
return result.Match(
_ => ValidationResult.Success,
error => new ValidationResult($"{error.Type} - {error.Message}")
);
}
}