EnumOfAttribute.cs 765 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. namespace Masuit.Tools.Core.Validator;
  4. /// <summary>
  5. /// 枚举值校验
  6. /// </summary>
  7. public class EnumOfAttribute : ValidationAttribute
  8. {
  9. private Type Type { get; set; }
  10. /// <summary>
  11. /// 枚举类型
  12. /// </summary>
  13. /// <param name="value"></param>
  14. public EnumOfAttribute(Type value)
  15. {
  16. Type = value;
  17. }
  18. public override bool IsValid(object value)
  19. {
  20. return value is null || Enum.IsDefined(Type, value);
  21. }
  22. }
  23. /// <summary>
  24. /// 非空值校验
  25. /// </summary>
  26. public class NotNullOrEmptyAttribute : ValidationAttribute
  27. {
  28. public override bool IsValid(object value)
  29. {
  30. return value is not null && !value.IsNullOrEmpty();
  31. }
  32. }