-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEnumMemberAttribute.cs
More file actions
37 lines (32 loc) · 1.08 KB
/
StringEnumMemberAttribute.cs
File metadata and controls
37 lines (32 loc) · 1.08 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
using System.ComponentModel.DataAnnotations;
namespace Odin.System;
/// <summary>
/// Used to validate an attribute as being a member of a class derived from StringEnum.
/// </summary>
/// <typeparam name="TStringEnum"></typeparam>
public class StringEnumMemberAttribute<TStringEnum> : ValidationAttribute where TStringEnum : StringEnum<TStringEnum>
{
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value is null)
{
return ValidationResult.Success;
}
if (value is not string str)
{
return new ValidationResult($"{validationContext.DisplayName} must be a string.");
}
Result valid = StringEnum<TStringEnum>.ValidateValue(str);
if (!valid.IsSuccess)
{
return new ValidationResult(valid.MessagesToString());
}
return ValidationResult.Success;
}
}