diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-10-13 17:27:07 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2024-10-13 17:27:07 -0500 |
| commit | 8400410fbbc81cea8140da79b3bb991c3bacd87c (patch) | |
| tree | 6d9f6cb9aec47b08e4a672aec1540af77b693398 | |
| parent | 6f77ee1937bc55dc024fbbd58c8a51edcb68b2a0 (diff) | |
| download | slskd-8400410fbbc81cea8140da79b3bb991c3bacd87c.tar.gz | |
add array support to Enum attribute
| -rw-r--r-- | src/slskd/Common/Validation/EnumAttribute.cs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/slskd/Common/Validation/EnumAttribute.cs b/src/slskd/Common/Validation/EnumAttribute.cs index ab28a9f0..ac3e4ce7 100644 --- a/src/slskd/Common/Validation/EnumAttribute.cs +++ b/src/slskd/Common/Validation/EnumAttribute.cs @@ -18,7 +18,9 @@ namespace slskd.Validation { using System; + using System.Collections.Generic; using System.ComponentModel.DataAnnotations; + using System.Linq; /// <summary> /// Validates that the value is a valid member of the specified <see cref="Enum"/>. @@ -36,9 +38,24 @@ namespace slskd.Validation protected override ValidationResult IsValid(object value, ValidationContext validationContext) { - if (value != null && !Enum.TryParse(TargetType, value.ToString(), IgnoreCase, out _)) + if (value is IList<string> array) { - return new ValidationResult($"The {validationContext.DisplayName} field must be one of: {string.Join(", ", Enum.GetNames(TargetType))}. Case {(IgnoreCase ? "insensitive" : "sensitive")}."); + if (array.Any(x => string.IsNullOrEmpty(x))) + { + return new ValidationResult($"The {validationContext.DisplayName} field contains one or more null or empty values"); + } + + if (array.Any(x => !Enum.TryParse(TargetType, x, IgnoreCase, out _))) + { + return new ValidationResult($"The elements in the {validationContext.DisplayName} field must all be one of: {string.Join(", ", Enum.GetNames(TargetType))}. Case {(IgnoreCase ? "insensitive" : "sensitive")}."); + } + } + else + { + if (value != null && !Enum.TryParse(TargetType, value.ToString(), IgnoreCase, out _)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be one of: {string.Join(", ", Enum.GetNames(TargetType))}. Case {(IgnoreCase ? "insensitive" : "sensitive")}."); + } } return ValidationResult.Success; |
