Skip to content

Commit d6cfd92

Browse files
committed
fix: correct validation rules for v2
1 parent ba224eb commit d6cfd92

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/ByteBard.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public T ReadFragment<T>(JsonNode input, AsyncApiVersion version, out AsyncApiDi
139139
where T : IAsyncApiElement
140140
{
141141
diagnostic = new AsyncApiDiagnostic();
142+
diagnostic.SpecificationVersion = version;
142143
this.context ??= new ParsingContext(diagnostic, this.settings)
143144
{
144145
ExtensionParsers = this.settings.ExtensionParsers,
@@ -363,7 +364,10 @@ private IAsyncApiSerializable ResolveStreamReference(Stream stream, IAsyncApiRef
363364
}
364365
}
365366

366-
AsyncApiDiagnostic fragmentDiagnostic = new AsyncApiDiagnostic();
367+
AsyncApiDiagnostic fragmentDiagnostic = new AsyncApiDiagnostic
368+
{
369+
SpecificationVersion = diagnostic.SpecificationVersion,
370+
};
367371
IAsyncApiSerializable result = null;
368372
switch (reference.Reference.Type)
369373
{

src/ByteBard.AsyncAPI/Validation/Rules/AsyncApiServerRules.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[AsyncApiRule]
77
public static class AsyncApiServerRules
88
{
9+
[AsyncApiVersionRule(AsyncApiVersion.AsyncApi3_0)]
910
public static ValidationRule<AsyncApiServer> ServerRequiredFields =>
1011
new ValidationRule<AsyncApiServer>(
1112
(context, server) =>
@@ -28,6 +29,32 @@ public static class AsyncApiServerRules
2829
string.Format(Resource.Validation_FieldRequired, "protocol", "server"));
2930
}
3031

32+
context.Exit();
33+
});
34+
35+
[AsyncApiVersionRule(AsyncApiVersion.AsyncApi2_0)]
36+
public static ValidationRule<AsyncApiServer> V2ServerRequiredFields =>
37+
new ValidationRule<AsyncApiServer>(
38+
(context, server) =>
39+
{
40+
context.Enter("url");
41+
if (server.Host == null)
42+
{
43+
context.CreateError(
44+
nameof(V2ServerRequiredFields),
45+
string.Format(Resource.Validation_FieldRequired, "url", "server"));
46+
}
47+
48+
context.Exit();
49+
50+
context.Enter("protocol");
51+
if (server.Protocol == null)
52+
{
53+
context.CreateError(
54+
nameof(V2ServerRequiredFields),
55+
string.Format(Resource.Validation_FieldRequired, "protocol", "server"));
56+
}
57+
3158
context.Exit();
3259
});
3360
}

0 commit comments

Comments
 (0)