Skip to content

Commit 0988b1b

Browse files
committed
add the whole visitor classes for validation
1 parent 7316184 commit 0988b1b

33 files changed

+787
-42
lines changed

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@
177177
<data name="SourceExpressionHasInvalidFormat" xml:space="preserve">
178178
<value>The source expression '{0}' has invalid format.</value>
179179
</data>
180+
<data name="UnknownVisitorType" xml:space="preserve">
181+
<value>Can not find visitor type registered for type '{0}'.</value>
182+
</data>
180183
<data name="Validation_PathItemMustBeginWithSlash" xml:space="preserve">
181184
<value>The path item name '{0}' MUST begin with a slash.</value>
182185
</data>

src/Microsoft.OpenApi/Validations/ValidationContextExtensions.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System.Collections.Generic;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Validations.Visitors;
8-
using System.Collections.Generic;
99

1010
namespace Microsoft.OpenApi.Validations
1111
{
@@ -56,5 +56,28 @@ public static void ValidateCollection<T>(this ValidationContext context, IEnumer
5656
}
5757
}
5858
}
59+
60+
/// <summary>
61+
/// Validate the map of Open API element.
62+
/// </summary>
63+
/// <typeparam name="T">The Open API element.</typeparam>
64+
/// <param name="context">The validation cotext.</param>
65+
/// <param name="elements">The map of Open API element.</param>
66+
public static void ValidateMap<T>(this ValidationContext context, IDictionary<string, T> elements)
67+
where T : IOpenApiElement
68+
{
69+
if (context == null)
70+
{
71+
throw Error.ArgumentNull(nameof(context));
72+
}
73+
74+
if (elements != null)
75+
{
76+
foreach (var element in elements)
77+
{
78+
context.Validate(element.Value);
79+
}
80+
}
81+
}
5982
}
6083
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Diagnostics;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.Validations.Visitors
10+
{
11+
/// <summary>
12+
/// Visit <see cref="OpenApiCallback"/>.
13+
/// </summary>
14+
internal class CallbackVisitor : VisitorBase<OpenApiCallback>
15+
{
16+
/// <summary>
17+
/// Visit the children of the <see cref="OpenApiCallback"/>.
18+
/// </summary>
19+
/// <param name="context">The validation context.</param>
20+
/// <param name="callback">The <see cref="OpenApiCallback"/>.</param>
21+
protected override void Next(ValidationContext context, OpenApiCallback callback)
22+
{
23+
Debug.Assert(context != null);
24+
Debug.Assert(callback != null);
25+
26+
foreach (var pathItem in callback.PathItems)
27+
{
28+
context.Validate(pathItem.Value);
29+
}
30+
31+
base.Next(context, callback);
32+
}
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Diagnostics;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.Validations.Visitors
10+
{
11+
/// <summary>
12+
/// Visit <see cref="OpenApiComponents"/>.
13+
/// </summary>
14+
internal class ComponentsVisitor : VisitorBase<OpenApiComponents>
15+
{
16+
/// <summary>
17+
/// Visit the children of the <see cref="OpenApiComponents"/>.
18+
/// </summary>
19+
/// <param name="context">The validation context.</param>
20+
/// <param name="components">The <see cref="OpenApiComponents"/>.</param>
21+
protected override void Next(ValidationContext context, OpenApiComponents components)
22+
{
23+
Debug.Assert(context != null);
24+
Debug.Assert(components != null);
25+
26+
context.ValidateMap(components.Schemas);
27+
context.ValidateMap(components.Responses);
28+
context.ValidateMap(components.Parameters);
29+
context.ValidateMap(components.Examples);
30+
context.ValidateMap(components.RequestBodies);
31+
context.ValidateMap(components.Headers);
32+
context.ValidateMap(components.SecuritySchemes);
33+
context.ValidateMap(components.Links);
34+
context.ValidateMap(components.Callbacks);
35+
base.Next(context, components);
36+
}
37+
}
38+
}

src/Microsoft.OpenApi/Validations/Visitors/ContactVisitor.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,5 @@ namespace Microsoft.OpenApi.Validations.Visitors
1212
/// </summary>
1313
internal class ContactVisitor : VisitorBase<OpenApiContact>
1414
{
15-
/// <summary>
16-
/// Visit the children in <see cref="OpenApiContact"/>.
17-
/// </summary>
18-
/// <param name="context">The validation context.</param>
19-
/// <param name="contact">The <see cref="OpenApiContact"/>.</param>
20-
protected override void Next(ValidationContext context, OpenApiContact contact)
21-
{
22-
}
2315
}
2416
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Validations.Visitors
9+
{
10+
/// <summary>
11+
/// Visit <see cref="OpenApiDiscriminator"/>.
12+
/// </summary>
13+
internal class DiscriminatorVisitor : VisitorBase<OpenApiDiscriminator>
14+
{
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Diagnostics;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.Validations.Visitors
10+
{
11+
/// <summary>
12+
/// Visit <see cref="OpenApiEncoding"/>.
13+
/// </summary>
14+
internal class EncodingVisitor : VisitorBase<OpenApiEncoding>
15+
{
16+
/// <summary>
17+
/// Visit the children in <see cref="OpenApiEncoding"/>.
18+
/// </summary>
19+
/// <param name="context">The validation context.</param>
20+
/// <param name="encoding">The <see cref="OpenApiEncoding"/>.</param>
21+
protected override void Next(ValidationContext context, OpenApiEncoding encoding)
22+
{
23+
Debug.Assert(context != null);
24+
Debug.Assert(encoding != null);
25+
26+
context.ValidateMap(encoding.Headers);
27+
28+
base.Next(context, encoding);
29+
}
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Validations.Visitors
9+
{
10+
/// <summary>
11+
/// Visit <see cref="OpenApiExample"/>.
12+
/// </summary>
13+
internal class ExampleVisitor : VisitorBase<OpenApiExample>
14+
{
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Validations.Visitors
9+
{
10+
/// <summary>
11+
/// Visit <see cref="OpenApiExternalDocs"/>.
12+
/// </summary>
13+
internal class ExternalDocsVisitor : VisitorBase<OpenApiExternalDocs>
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)