Browse Source

ThrowIfInvalid.DriveType added to domain validator

James 2 months ago
parent
commit
b46fac4ecb
1 changed files with 59 additions and 32 deletions
  1. 59 32
      RackPeek.Domain/Helpers/ThrowIfInvalid.cs

+ 59 - 32
RackPeek.Domain/Helpers/ThrowIfInvalid.cs

@@ -6,23 +6,18 @@ public static class ThrowIfInvalid
 {
 {
     public static void ResourceName(string name)
     public static void ResourceName(string name)
     {
     {
-        if (string.IsNullOrWhiteSpace(name))
-            throw new ValidationException("Name is required.");
+        if (string.IsNullOrWhiteSpace(name)) throw new ValidationException("Name is required.");
 
 
-        if (name.Length > 50)
-            throw new ValidationException("Name is too long.");
+        if (name.Length > 50) throw new ValidationException("Name is too long.");
     }
     }
 
 
     public static void RamGb(int? value)
     public static void RamGb(int? value)
     {
     {
-        if (value is null)
-            throw new ValidationException("RAM value must be specified.");
+        if (value is null) throw new ValidationException("RAM value must be specified.");
 
 
-        if (value < 0)
-            throw new ValidationException("RAM value must be a non negative number of gigabytes.");
+        if (value < 0) throw new ValidationException("RAM value must be a non negative number of gigabytes.");
     }
     }
 
 
-
     #region Nics
     #region Nics
 
 
     public static readonly string[] ValidNicTypes =
     public static readonly string[] ValidNicTypes =
@@ -46,8 +41,7 @@ public static class ThrowIfInvalid
         "osfp",
         "osfp",
 
 
         // Legacy / niche but still seen
         // Legacy / niche but still seen
-        "xfp",
-        "cx4",
+        "xfp", "cx4",
 
 
         // Management / special-purpose
         // Management / special-purpose
         "mgmt" // Dedicated management NIC (IPMI/BMC)
         "mgmt" // Dedicated management NIC (IPMI/BMC)
@@ -55,13 +49,11 @@ public static class ThrowIfInvalid
 
 
     public static void NicType(string nicType)
     public static void NicType(string nicType)
     {
     {
-        if (string.IsNullOrWhiteSpace(nicType))
-            throw new ValidationException("NIC type is required.");
+        if (string.IsNullOrWhiteSpace(nicType)) throw new ValidationException("NIC type is required.");
 
 
         var normalized = nicType.Trim().ToLowerInvariant();
         var normalized = nicType.Trim().ToLowerInvariant();
 
 
-        if (ValidNicTypes.Contains(normalized))
-            return;
+        if (ValidNicTypes.Contains(normalized)) return;
 
 
         var suggestions = GetNicTypeSuggestions(normalized).ToList();
         var suggestions = GetNicTypeSuggestions(normalized).ToList();
 
 
@@ -74,12 +66,7 @@ public static class ThrowIfInvalid
 
 
     private static IEnumerable<string> GetNicTypeSuggestions(string input)
     private static IEnumerable<string> GetNicTypeSuggestions(string input)
     {
     {
-        return ValidNicTypes
-            .Select(type => new
-            {
-                Type = type,
-                Score = SimilarityScore(input, type)
-            })
+        return ValidNicTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
             .Where(x => x.Score >= 0.5)
             .Where(x => x.Score >= 0.5)
             .OrderByDescending(x => x.Score)
             .OrderByDescending(x => x.Score)
             .Take(3)
             .Take(3)
@@ -88,11 +75,9 @@ public static class ThrowIfInvalid
 
 
     private static double SimilarityScore(string a, string b)
     private static double SimilarityScore(string a, string b)
     {
     {
-        if (a == b)
-            return 1.0;
+        if (a == b) return 1.0;
 
 
-        if (b.StartsWith(a) || a.StartsWith(b))
-            return 0.9;
+        if (b.StartsWith(a) || a.StartsWith(b)) return 0.9;
 
 
         var commonChars = a.Intersect(b).Count();
         var commonChars = a.Intersect(b).Count();
         return (double)commonChars / Math.Max(a.Length, b.Length);
         return (double)commonChars / Math.Max(a.Length, b.Length);
@@ -100,17 +85,59 @@ public static class ThrowIfInvalid
 
 
     public static void NicSpeed(int speed)
     public static void NicSpeed(int speed)
     {
     {
-        if (speed < 0)
-            throw new ValidationException(
-                "NIC speed must be a non negative number of gigabits per second.");
+        if (speed < 0) throw new ValidationException("NIC speed must be a non negative number of gigabits per second.");
     }
     }
 
 
-
     public static void NicPorts(int ports)
     public static void NicPorts(int ports)
     {
     {
-        if (ports < 0)
-            throw new ValidationException(
-                "NIC port count must be a non negative integer.");
+        if (ports < 0) throw new ValidationException("NIC port count must be a non negative integer.");
+    }
+
+    #endregion
+
+    #region Drives
+
+    public static readonly string[] ValidDriveTypes =
+    {
+        // Flash storage
+        "nvme", "ssd",
+        // Traditional spinning disks
+        "hdd",
+        // Enterprise interfaces
+        "sas", "sata",
+        // External / removable
+        "usb", "sdcard", "micro-sd"
+    };
+
+    public static void DriveType(string driveType)
+    {
+        if (string.IsNullOrWhiteSpace(driveType)) throw new ValidationException("Drive type is required.");
+
+        var normalized = driveType.Trim().ToLowerInvariant();
+
+        if (ValidDriveTypes.Contains(normalized)) return;
+
+        var suggestions = GetDriveTypeSuggestions(normalized).ToList();
+
+        var message = suggestions.Any()
+            ? $"Drive type '{driveType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
+            : $"Drive type '{driveType}' is not valid. Valid Drive types include nvme, ssd, hdd, sata, sas etc.";
+
+        throw new ValidationException(message);
+    }
+
+    private static IEnumerable<string> GetDriveTypeSuggestions(string input)
+    {
+        return ValidDriveTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
+            .Where(x => x.Score >= 0.5)
+            .OrderByDescending(x => x.Score)
+            .Take(3)
+            .Select(x => x.Type);
+    }
+
+    public static void DriveSize(int size)
+    {
+        if (size < 0) throw new ValidationException("Drive size value must be a non negative number of gigabytes.");
     }
     }
 
 
     #endregion
     #endregion