MaxFileCountAttribute.cs 944 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections;
  2. using System.ComponentModel.DataAnnotations;
  3. namespace SharedKernel.Attributes
  4. {
  5. /*
  6. * 파일 개수 제한
  7. */
  8. [AttributeUsage(AttributeTargets.Property)]
  9. public class MaxFileCountAttribute : ValidationAttribute
  10. {
  11. private readonly int _maxCount;
  12. public MaxFileCountAttribute(int maxCount)
  13. {
  14. _maxCount = maxCount;
  15. }
  16. public override bool IsValid(object? value)
  17. {
  18. if (value is IEnumerable files)
  19. {
  20. return files.Cast<object>().Count() <= _maxCount;
  21. }
  22. return true;
  23. }
  24. public override string FormatErrorMessage(string name)
  25. {
  26. var template = string.IsNullOrWhiteSpace(ErrorMessage) ? "{0}은(는) 최대 {1}개까지 등록 가능합니다." : ErrorMessage;
  27. return string.Format(template, name, _maxCount);
  28. }
  29. }
  30. }