EnumOfAttribute.cs 874 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if (value is null)
  21. {
  22. return true;
  23. }
  24. return Enum.IsDefined(Type, value);
  25. }
  26. }
  27. /// <summary>
  28. /// 非空值校验
  29. /// </summary>
  30. public class NotNullOrEmptyAttribute : ValidationAttribute
  31. {
  32. public override bool IsValid(object value)
  33. {
  34. if (value is null)
  35. {
  36. return false;
  37. }
  38. return !value.IsNullOrEmpty();
  39. }
  40. }