-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonValidationExceptionGenerator.cs
More file actions
55 lines (50 loc) · 1.96 KB
/
JsonValidationExceptionGenerator.cs
File metadata and controls
55 lines (50 loc) · 1.96 KB
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
namespace OpenAPI.WebApiGenerator.CodeGeneration;
internal sealed class JsonValidationExceptionGenerator(string @namespace)
{
internal const string ClassName = "JsonValidationException";
internal string CreateThrowJsonValidationExceptionInvocation(
string message,
string validationResultVariableName)
{
return
$"""throw new {@namespace}.{ClassName}("{message}", {validationResultVariableName})""";
}
internal SourceCode GenerateJsonValidationExceptionClass() =>
new($"{ClassName}.g.cs",
$$"""
#nullable enable
using Corvus.Json;
using System;
using System.Collections.Immutable;
using System.Text;
namespace {{@namespace}};
/// <summary>
/// Exception thrown when validation of json objects fail
/// </summary>
internal sealed class {{ClassName}} : Exception
{
/// <summary>
/// Create json validation exception
/// </summary>
internal {{ClassName}}(string message, ImmutableList<ValidationResult> validationResult) : base(
GetValidationMessage(message, validationResult))
{
ValidationResult = validationResult;
}
/// <summary>
/// The validation result
/// </summary>
internal ImmutableList<ValidationResult> ValidationResult { get; }
private static string GetValidationMessage(string message, ImmutableList<ValidationResult> validationResult)
{
return validationResult.IsEmpty
? message
: validationResult.Aggregate(
new StringBuilder($"{message}:").AppendLine(),
(builder, result) =>
builder.AppendLine($"- {result}")).ToString();
}
}
#nullable restore
""");
}