ThrowIfInvalid.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.ComponentModel.DataAnnotations;
  2. using RackPeek.Domain.Resources.Models;
  3. using RackPeek.Domain.Resources.SystemResources;
  4. namespace RackPeek.Domain.Helpers;
  5. public static class ThrowIfInvalid
  6. {
  7. public static void ResourceName(string name)
  8. {
  9. if (string.IsNullOrWhiteSpace(name)) throw new ValidationException("Name is required.");
  10. if (name.Length > 50) throw new ValidationException("Name is too long.");
  11. }
  12. public static void AccessPointModelName(string name)
  13. {
  14. if (string.IsNullOrWhiteSpace(name))
  15. throw new ValidationException("Model name is required.");
  16. if (name.Length > 50)
  17. throw new ValidationException("Model name is too long.");
  18. }
  19. public static void RamGb(int? value)
  20. {
  21. if (value is null) throw new ValidationException("RAM value must be specified.");
  22. if (value < 0) throw new ValidationException("RAM value must be a non negative number of gigabytes.");
  23. }
  24. public static void SystemType(string systemType)
  25. {
  26. if (string.IsNullOrWhiteSpace(systemType)) throw new ValidationException("System type is required.");
  27. var normalized = systemType.Trim().ToLowerInvariant();
  28. if (SystemResource.ValidSystemTypes.Contains(normalized)) return;
  29. var suggestions = GetSystemTypeSuggestions(normalized).ToList();
  30. var message = suggestions.Any()
  31. ? $"System type '{systemType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
  32. : $"System type '{systemType}' is not valid. Valid System types include hypervisor, baremetal vm, container, other etc";
  33. throw new ValidationException(message);
  34. }
  35. private static IEnumerable<string> GetSystemTypeSuggestions(string input)
  36. {
  37. return SystemResource.ValidSystemTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
  38. .Where(x => x.Score >= 0.5)
  39. .OrderByDescending(x => x.Score)
  40. .Take(3)
  41. .Select(x => x.Type);
  42. }
  43. #region Nics
  44. public static void NicType(string nicType)
  45. {
  46. if (string.IsNullOrWhiteSpace(nicType)) throw new ValidationException("NIC type is required.");
  47. var normalized = nicType.Trim().ToLowerInvariant();
  48. if (Nic.ValidNicTypes.Contains(normalized)) return;
  49. var suggestions = GetNicTypeSuggestions(normalized).ToList();
  50. var message = suggestions.Any()
  51. ? $"NIC type '{nicType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
  52. : $"NIC type '{nicType}' is not valid. Valid NIC types include rj45, sfp, sfp+, qsfp+ etc";
  53. throw new ValidationException(message);
  54. }
  55. private static IEnumerable<string> GetNicTypeSuggestions(string input)
  56. {
  57. return Nic.ValidNicTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
  58. .Where(x => x.Score >= 0.5)
  59. .OrderByDescending(x => x.Score)
  60. .Take(3)
  61. .Select(x => x.Type);
  62. }
  63. private static double SimilarityScore(string a, string b)
  64. {
  65. if (a == b) return 1.0;
  66. if (b.StartsWith(a) || a.StartsWith(b)) return 0.9;
  67. var commonChars = a.Intersect(b).Count();
  68. return (double)commonChars / Math.Max(a.Length, b.Length);
  69. }
  70. public static void NicSpeed(double speed)
  71. {
  72. if (speed < 0) throw new ValidationException("NIC speed must be a non negative number of gigabits per second.");
  73. }
  74. public static void NetworkSpeed(double speed)
  75. {
  76. if (speed < 0)
  77. throw new ValidationException(
  78. "Network speed must be a non negative number of gigabits per second.");
  79. }
  80. public static void NicPorts(int ports)
  81. {
  82. if (ports < 0) throw new ValidationException("NIC port count must be a non negative integer.");
  83. }
  84. #endregion
  85. #region Drives
  86. public static void DriveType(string driveType)
  87. {
  88. if (string.IsNullOrWhiteSpace(driveType)) throw new ValidationException("Drive type is required.");
  89. var normalized = driveType.Trim().ToLowerInvariant();
  90. if (Drive.ValidDriveTypes.Contains(normalized)) return;
  91. var suggestions = GetDriveTypeSuggestions(normalized).ToList();
  92. var message = suggestions.Any()
  93. ? $"Drive type '{driveType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
  94. : $"Drive type '{driveType}' is not valid. Valid Drive types include nvme, ssd, hdd, sata, sas etc.";
  95. throw new ValidationException(message);
  96. }
  97. private static IEnumerable<string> GetDriveTypeSuggestions(string input)
  98. {
  99. return Drive.ValidDriveTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
  100. .Where(x => x.Score >= 0.5)
  101. .OrderByDescending(x => x.Score)
  102. .Take(3)
  103. .Select(x => x.Type);
  104. }
  105. public static void DriveSize(int size)
  106. {
  107. if (size < 0) throw new ValidationException("Drive size value must be a non negative number of gigabytes.");
  108. }
  109. #endregion
  110. }