diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-10-26 18:32:35 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-26 18:32:35 -0500 |
| commit | e8238d19b3c7b29d2892caeafd66733b5622d1b2 (patch) | |
| tree | e8ced369b7631ef46d3043118aac5518dca76d35 /src/slskd/Common | |
| parent | 54590c78f2102634d982698608784e8d3c9043e0 (diff) | |
| parent | 334627cd06476df897cf3f3ec02ca70b51b7fec1 (diff) | |
| download | slskd-e8238d19b3c7b29d2892caeafd66733b5622d1b2.tar.gz | |
Merge pull request #1232 from slskd/scripts
Add the ability to run user-defined scripts when application events are raised
Diffstat (limited to 'src/slskd/Common')
| -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; |
