aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Validation/ValidateAttribute.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-11-04 08:21:43 -0600
committerGitHub <noreply@github.com>2024-11-04 08:21:43 -0600
commite7aefd66f1992dcecd9454df0771e1f29fae2b1d (patch)
tree14da14b514ab3755d03e025f95903eda47c789e0 /src/slskd/Common/Validation/ValidateAttribute.cs
parent5ebb679f4f7a24649e5117931e06138a9375110f (diff)
parent7f9a694da61bb046343d9f98ec2b5cd8b444e56b (diff)
downloadslskd-e7aefd66f1992dcecd9454df0771e1f29fae2b1d.tar.gz
Merge pull request #1238 from slskd/webhooks
Add the ability to call HTTP endpoints (webhooks) when application events are raised
Diffstat (limited to 'src/slskd/Common/Validation/ValidateAttribute.cs')
-rw-r--r--src/slskd/Common/Validation/ValidateAttribute.cs36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/slskd/Common/Validation/ValidateAttribute.cs b/src/slskd/Common/Validation/ValidateAttribute.cs
index c7efaa92..540e61bc 100644
--- a/src/slskd/Common/Validation/ValidateAttribute.cs
+++ b/src/slskd/Common/Validation/ValidateAttribute.cs
@@ -17,6 +17,7 @@
namespace slskd.Validation
{
+ using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
@@ -34,22 +35,43 @@ namespace slskd.Validation
return ValidationResult.Success;
}
+ // note: here's a great place to log the type and value if there are issues:
+ // Console.WriteLine($"{value.GetType()}: {value.ToJson()}");
var results = new List<ValidationResult>();
var context = new ValidationContext(value, null, null);
- if (value.IsDictionary())
+ // we should only be using dictionaries and arrays (e.g. []), not IEnumerable, because the YAML library
+ // somehow has nondeterministic behavior, sometimes turning YAML arrays into arrays, and sometimes List
+ // this could be handled by checking for List<T> but let's just keep it simple and remember to use arrays
+ if (value.IsDictionary() || value is Array)
{
var compositeResults = new CompositeValidationResult(validationContext.DisplayName);
- // given the context in this application, key type should never be anything but a string
- foreach (string key in ((IDictionary)value).Keys)
+ if (value.IsDictionary())
{
- var val = ((IDictionary)value)[key];
- var result = IsValid(val, new ValidationContext(val, null, null));
+ // given the context in this application, key type should never be anything but a string
+ foreach (string key in ((IDictionary)value).Keys)
+ {
+ var val = ((IDictionary)value)[key];
+ var result = IsValid(val, new ValidationContext(val, null, null));
- if (result != ValidationResult.Success)
+ if (result != ValidationResult.Success)
+ {
+ compositeResults.AddResult(new CompositeValidationResult(key, new[] { result }.ToList()));
+ }
+ }
+ }
+ else if (value is Array array)
+ {
+ for (int i = 0; i < array.Length; i++)
{
- compositeResults.AddResult(new CompositeValidationResult(key, new[] { result }.ToList()));
+ var val = array.GetValue(i);
+ var result = IsValid(val, new ValidationContext(val, null, null));
+
+ if (result != ValidationResult.Success)
+ {
+ compositeResults.AddResult(new CompositeValidationResult(i.ToString(), new[] { result }.ToList()));
+ }
}
}