-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncApiOperationRules.cs
More file actions
97 lines (82 loc) · 4.09 KB
/
AsyncApiOperationRules.cs
File metadata and controls
97 lines (82 loc) · 4.09 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections.Generic;
using System.Linq;
namespace ByteBard.AsyncAPI.Validation.Rules
{
using System;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Validations;
[AsyncApiRule]
public static class AsyncApiOperationRules
{
[AsyncApiVersionRule(AsyncApiVersion.AsyncApi3_0)]
public static ValidationRule<AsyncApiOperation> OperationRequiredFields =>
new ValidationRule<AsyncApiOperation>(
(context, operation) =>
{
context.Enter("action");
if (!Enum.IsDefined(typeof(AsyncApiAction), operation.Action))
{
context.CreateError(
nameof(OperationRequiredFields),
string.Format(Resource.Validation_FieldRequired, "action", "operation"));
}
context.Exit();
context.Enter("channel");
if (operation.Channel is null)
{
context.CreateError(
nameof(OperationRequiredFields),
string.Format(Resource.Validation_FieldRequired, "channel", "operation"));
}
context.Exit();
});
[AsyncApiVersionRule(AsyncApiVersion.AsyncApi3_0)]
public static ValidationRule<AsyncApiOperation> OperationChannelReference =>
new ValidationRule<AsyncApiOperation>(
(context, operation) =>
{
if (context.RootDocument?.Operations.Values.FirstOrDefault(op => op == operation) is null)
{
return;
}
var channels =
context.RootDocument.Channels.Values.Where(channel => operation.Channel.Equals(channel));
var referencedChannel = channels.FirstOrDefault(c => operation.Channel.Equals(c));
if (referencedChannel == null)
{
context.CreateError(
"OperationChannelRef",
string.Format(Resource.Validation_OperationMustReferenceValidChannel, operation.Title));
}
});
[AsyncApiVersionRule(AsyncApiVersion.AsyncApi3_0)]
public static ValidationRule<AsyncApiOperation> OperationMessages =>
new ValidationRule<AsyncApiOperation>(
(context, operation) =>
{
if (context.RootDocument?.Operations.Values.FirstOrDefault(op => op == operation) is null)
{
return;
}
var channels =
context.RootDocument.Channels.Values.Where(channel => operation.Channel.Equals(channel));
var referencedChannel = channels.FirstOrDefault(c => operation.Channel.Equals(c));
if (referencedChannel == null)
{
return;
}
if (!AllOperationsMessagesReferencesChannelMessages(operation.Messages, referencedChannel.Messages.Values))
{
context.CreateError(
"OperationChannelRef",
string.Format(Resource.Validation_OperationMessagesMustReferenceOperationChannel, operation.Title));
}
});
private static bool AllOperationsMessagesReferencesChannelMessages(
IList<AsyncApiMessageReference> operationMessages, ICollection<AsyncApiMessage> channelMessages) =>
operationMessages.All(opMessage => OperationMessageReferencesAnyChannelMessage(opMessage, channelMessages));
private static bool OperationMessageReferencesAnyChannelMessage(
AsyncApiMessageReference operationMessage, ICollection<AsyncApiMessage> channelMessages) =>
channelMessages.Any(channelMessage => channelMessage.Equals(operationMessage));
}
}