1
0
mirror of https://github.com/bitwarden/server synced 2025-12-06 00:03:34 +00:00
Files
server/src/Api/Utilities/EnumMatchesAttribute.cs
2025-07-08 10:25:59 -04:00

30 lines
677 B
C#

// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Utilities;
public class EnumMatchesAttribute<T>(params T[] accepted) : ValidationAttribute
where T : Enum
{
public override bool IsValid(object value)
{
if (value == null || accepted == null || accepted.Length == 0)
{
return false;
}
var success = Enum.TryParse(typeof(T), value.ToString(), out var result);
if (!success)
{
return false;
}
var typed = (T)result;
return accepted.Contains(typed);
}
}