blob: f0186e763d317cba2b5ce49ce8cc7c4b9b4ae9c4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
namespace slskd.Validation
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using slskd.Configuration;
public static class ValidationExtensions
{
public static string GetResultView(this ValidationResult result, string message)
{
var view = result.GetResultView(0).ToList();
if (view.Count() > 0 && !string.IsNullOrEmpty(message))
{
view[0] = message;
}
return string.Join("\n", view);
}
private static IEnumerable<string> GetResultView(this ValidationResult result, int depth = 0)
{
var indent = new string(' ', depth * 2);
if (result is CompositeValidationResult composite)
{
var lines = new[] { indent + result + ":" }.ToList();
foreach (var child in composite.Results)
{
lines.AddRange(child.GetResultView(depth + 1));
}
return lines;
}
else
{
return new[] { indent + result };
}
}
public static bool TryValidate(this Options options, out CompositeValidationResult result)
{
result = null;
var results = new List<ValidationResult>();
if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true))
{
result = new CompositeValidationResult("Invalid configuration", results);
return false;
}
return true;
}
}
}
|