-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncApiValidator.cs
More file actions
224 lines (187 loc) · 8.09 KB
/
AsyncApiValidator.cs
File metadata and controls
224 lines (187 loc) · 8.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
namespace ByteBard.AsyncAPI.Validations
{
using System;
using System.Collections.Generic;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Services;
/// <summary>
/// Class containing dispatchers to execute validation rules on for AsyncApi document.
/// </summary>
public class AsyncApiValidator : AsyncApiVisitorBase, IValidationContext
{
private readonly ValidationRuleSet ruleSet;
private readonly IList<AsyncApiValidatorError> errors = new List<AsyncApiValidatorError>();
private readonly IList<AsyncApiValidatorWarning> warnings = new List<AsyncApiValidatorWarning>();
/// <summary>
/// Create a vistor that will validate an AsyncApiDocument.
/// </summary>
/// <param name="ruleSet"></param>
public AsyncApiValidator(ValidationRuleSet ruleSet, AsyncApiDocument rootDocument = null)
{
this.ruleSet = ruleSet;
this.RootDocument = rootDocument;
}
public AsyncApiDocument RootDocument { get; }
/// <summary>
/// Gets the validation errors.
/// </summary>
public IEnumerable<AsyncApiValidatorError> Errors
{
get
{
return this.errors;
}
}
/// <summary>
/// Gets the validation warnings.
/// </summary>
public IEnumerable<AsyncApiValidatorWarning> Warnings
{
get
{
return this.warnings;
}
}
/// <summary>
/// Register an error with the validation context.
/// </summary>
/// <param name="error">Error to register.</param>
public void AddError(AsyncApiValidatorError error)
{
if (error == null)
{
throw Error.ArgumentNull(nameof(error));
}
this.errors.Add(error);
}
/// <summary>
/// Register an error with the validation context.
/// </summary>
/// <param name="warning">Error to register.</param>
public void AddWarning(AsyncApiValidatorWarning warning)
{
if (warning == null)
{
throw Error.ArgumentNull(nameof(warning));
}
this.warnings.Add(warning);
}
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiDocument"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiDocument item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiInfo"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiInfo item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiContact"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiContact item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiComponents"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiComponents item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiLicense"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiLicense item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiOAuthFlow"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiOAuthFlow item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiTag"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiTag item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiParameter"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiParameter item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiJsonSchema"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiJsonSchema item) => this.Validate(item);
public override void Visit(AsyncApiAvroSchema item) => this.Validate(item);
public override void Visit(IAsyncApiSchema item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiServer"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiServer item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="AsyncApiOperation"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiOperation item) => this.Validate(item);
public override void Visit(IServerBinding item) => this.Validate(item);
public override void Visit(IChannelBinding item) => this.Validate(item);
public override void Visit(IOperationBinding item) => this.Validate(item);
public override void Visit(IMessageBinding item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="IAsyncApiExtensible"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(IAsyncApiExtensible item) => this.Validate(item);
/// <summary>
/// Execute validation rules against an <see cref="IAsyncApiExtension"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(IAsyncApiExtension item) => this.Validate(item, item.GetType());
/// <summary>
/// Execute validation rules against a list of <see cref="AsyncApiExample"/>.
/// </summary>
/// <param name="items">The object to be validated.</param>
public override void Visit(IList<AsyncApiMessageExample> items) => this.Validate(items, items.GetType());
private void Validate<T>(T item)
{
var type = typeof(T);
this.Validate(item, type);
}
/// <summary>
/// This overload allows applying rules based on actual object type, rather than matched interface. This is
/// needed for validating extensions.
/// </summary>
private void Validate(object item, Type type)
{
if (item == null)
{
return; // Required fields should be checked by higher level objects
}
// Validate unresolved references as references
var potentialReference = item as IAsyncApiReferenceable;
if (potentialReference != null)
{
type = typeof(IAsyncApiReferenceable);
}
var version = this.GetDocumentVersion();
var rules = this.ruleSet.FindRules(type, version);
foreach (var rule in rules)
{
rule.Evaluate(this as IValidationContext, item);
}
}
private AsyncApiVersion? GetDocumentVersion()
{
if (this.RootDocument?.Asyncapi?.StartsWith("2") == true)
{
return AsyncApiVersion.AsyncApi2_0;
}
if (this.RootDocument?.Asyncapi?.StartsWith("3") == true)
{
return AsyncApiVersion.AsyncApi3_0;
}
return null;
}
}
}