MaxFileCountAttribute.cs 842 B

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