Skip to content

Commit 620b0f2

Browse files
committed
add the rules and modify the header copyright
1 parent 0988b1b commit 620b0f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+635
-234
lines changed

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

Lines changed: 36 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,21 @@
180180
<data name="UnknownVisitorType" xml:space="preserve">
181181
<value>Can not find visitor type registered for type '{0}'.</value>
182182
</data>
183+
<data name="Validation_ComponentsKeyMustMatchRegularExpr" xml:space="preserve">
184+
<value>The key '{0}' in '{1}' of components MUST match the regular expression '{2}'.</value>
185+
</data>
186+
<data name="Validation_ExtensionNameMustBeginWithXDash" xml:space="preserve">
187+
<value>The extension name '{0}' in '{1}' object MUST begin with 'x-'.</value>
188+
</data>
189+
<data name="Validation_FieldIsRequired" xml:space="preserve">
190+
<value>The field '{0}' in '{1}' object is REQUIRED.</value>
191+
</data>
183192
<data name="Validation_PathItemMustBeginWithSlash" xml:space="preserve">
184193
<value>The path item name '{0}' MUST begin with a slash.</value>
185194
</data>
195+
<data name="Validation_RuleAddTwice" xml:space="preserve">
196+
<value>The same rule cannot be in the same rule set twice.</value>
197+
</data>
186198
<data name="Validation_StringMustBeEmailAddress" xml:space="preserve">
187199
<value>The string '{0}' MUST be in the format of an email address.</value>
188200
</data>

src/Microsoft.OpenApi/Validations/OpenApiElementValidator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

64
using System.Collections.Generic;
75
using System.Linq;
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

4+
using System.Collections.Generic;
5+
using System.Text.RegularExpressions;
66
using Microsoft.OpenApi.Models;
7+
using Microsoft.OpenApi.Properties;
78

89
namespace Microsoft.OpenApi.Validations.Rules
910
{
@@ -12,5 +13,54 @@ namespace Microsoft.OpenApi.Validations.Rules
1213
/// </summary>
1314
public static class OpenApiComponentsRules
1415
{
16+
/// <summary>
17+
/// The key regex.
18+
/// </summary>
19+
public static Regex KeyRegex = new Regex(@"^[a-zA-Z0-9\.\-_]+$");
20+
21+
/// <summary>
22+
/// All the fixed fields declared above are objects
23+
/// that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$.
24+
/// </summary>
25+
public static readonly ValidationRule<OpenApiComponents> KeyMustBeRegularExpression =
26+
new ValidationRule<OpenApiComponents>(
27+
(context, components) =>
28+
{
29+
ValidateKeys(context, components.Schemas?.Keys, "schemas");
30+
31+
ValidateKeys(context, components.Responses?.Keys, "responses");
32+
33+
ValidateKeys(context, components.Parameters?.Keys, "parameters");
34+
35+
ValidateKeys(context, components.Examples?.Keys, "examples");
36+
37+
ValidateKeys(context, components.RequestBodies?.Keys, "requestBodies");
38+
39+
ValidateKeys(context, components.Headers?.Keys, "headers");
40+
41+
ValidateKeys(context, components.SecuritySchemes?.Keys, "securitySchemes");
42+
43+
ValidateKeys(context, components.Links?.Keys, "links");
44+
45+
ValidateKeys(context, components.Callbacks?.Keys, "callbacks");
46+
});
47+
48+
private static void ValidateKeys(ValidationContext context, IEnumerable<string> keys, string component)
49+
{
50+
if (keys == null)
51+
{
52+
return;
53+
}
54+
55+
foreach (var key in keys)
56+
{
57+
if (!KeyRegex.IsMatch(key))
58+
{
59+
ValidationError error = new ValidationError(ErrorReason.Format, context.PathString,
60+
string.Format(SRResource.Validation_ComponentsKeyMustMatchRegularExpr, key, component, KeyRegex.ToString()));
61+
context.AddError(error);
62+
}
63+
}
64+
}
1565
}
1666
}

src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

64
using System;
75
using Microsoft.OpenApi.Models;
@@ -26,7 +24,7 @@ internal static class OpenApiContactRules
2624
{
2725
if (!item.Email.IsEmailAddress())
2826
{
29-
ValidationError error = new ValidationError(ErrorReason.EmailFormat, context.PathString,
27+
ValidationError error = new ValidationError(ErrorReason.Format, context.PathString,
3028
String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
3129
context.AddError(error);
3230
}
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

4+
using System;
65
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
77

88
namespace Microsoft.OpenApi.Validations.Rules
99
{
@@ -15,27 +15,29 @@ internal static class OpenApiDocumentRules
1515
/// <summary>
1616
/// The Info field is required.
1717
/// </summary>
18-
public static readonly ValidationRule<OpenApiDocument> InfoIsRequired =
18+
public static readonly ValidationRule<OpenApiDocument> FieldIsRequired =
1919
new ValidationRule<OpenApiDocument>(
2020
(context, item) =>
2121
{
22+
// info
23+
context.Push("info");
2224
if (item.Info == null)
2325
{
24-
//context.AddError();
26+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
27+
String.Format(SRResource.Validation_FieldIsRequired, "info", "document"));
28+
context.AddError(error);
2529
}
26-
});
30+
context.Pop();
2731

28-
/// <summary>
29-
/// The Paths field is required.
30-
/// </summary>
31-
public static readonly ValidationRule<OpenApiDocument> PathsIsRequired =
32-
new ValidationRule<OpenApiDocument>(
33-
(context, item) =>
34-
{
32+
// paths
33+
context.Push("paths");
3534
if (item.Paths == null)
3635
{
37-
//context.AddError();
36+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
37+
String.Format(SRResource.Validation_FieldIsRequired, "paths", "document"));
38+
context.AddError(error);
3839
}
40+
context.Pop();
3941
});
4042
}
4143
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

4+
using System;
65
using Microsoft.OpenApi.Interfaces;
6+
using Microsoft.OpenApi.Properties;
77

88
namespace Microsoft.OpenApi.Validations.Rules
99
{
@@ -15,14 +15,21 @@ public static class OpenApiExtensibleRules
1515
/// <summary>
1616
/// Extension name MUST start with "x-".
1717
/// </summary>
18-
private static readonly ValidationRule<IOpenApiExtensible> ExtensionNameMustStartWithXDollar =
18+
public static readonly ValidationRule<IOpenApiExtensible> ExtensionNameMustStartWithXDash =
1919
new ValidationRule<IOpenApiExtensible>(
2020
(context, item) =>
2121
{
22+
context.Push("extensions");
2223
foreach (var extensible in item.Extensions)
2324
{
24-
25+
if (!extensible.Key.StartsWith("x-"))
26+
{
27+
ValidationError error = new ValidationError(ErrorReason.Format, context.PathString,
28+
String.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible.Key, context.PathString));
29+
context.AddError(error);
30+
}
2531
}
32+
context.Pop();
2633
});
2734
}
28-
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
7+
8+
namespace Microsoft.OpenApi.Validations.Rules
9+
{
10+
/// <summary>
11+
/// The validation rules for <see cref="OpenApiExternalDocs"/>.
12+
/// </summary>
13+
internal static class OpenApiExternalDocsRules
14+
{
15+
/// <summary>
16+
/// Validate the field is required.
17+
/// </summary>
18+
public static readonly ValidationRule<OpenApiExternalDocs> FieldIsRequired =
19+
new ValidationRule<OpenApiExternalDocs>(
20+
(context, item) =>
21+
{
22+
// title
23+
context.Push("url");
24+
if (item.Url == null)
25+
{
26+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
27+
String.Format(SRResource.Validation_FieldIsRequired, "url", "External Documentation"));
28+
context.AddError(error);
29+
}
30+
context.Pop();
31+
});
32+
33+
// add more rule.
34+
}
35+
}
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
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-
// ------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
53

64
using System;
75
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
87

98
namespace Microsoft.OpenApi.Validations.Rules
109
{
@@ -14,18 +13,33 @@ namespace Microsoft.OpenApi.Validations.Rules
1413
internal static class OpenApiInfoRules
1514
{
1615
/// <summary>
17-
/// The title of the application is required.
16+
/// Validate the field is required.
1817
/// </summary>
19-
public static readonly ValidationRule<OpenApiInfo> TitleIsRequired =
18+
public static readonly ValidationRule<OpenApiInfo> FieldIsRequired =
2019
new ValidationRule<OpenApiInfo>(
2120
(context, item) =>
2221
{
22+
// title
23+
context.Push("title");
2324
if (String.IsNullOrEmpty(item.Title))
2425
{
25-
// add error.
26+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
27+
String.Format(SRResource.Validation_FieldIsRequired, "url", "info"));
28+
context.AddError(error);
2629
}
30+
context.Pop();
31+
32+
// version
33+
context.Push("version");
34+
if (String.IsNullOrEmpty(item.Version))
35+
{
36+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
37+
String.Format(SRResource.Validation_FieldIsRequired, "version", "info"));
38+
context.AddError(error);
39+
}
40+
context.Pop();
2741
});
2842

2943
// add more rule.
3044
}
31-
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
7+
8+
namespace Microsoft.OpenApi.Validations.Rules
9+
{
10+
/// <summary>
11+
/// The validation rules for <see cref="OpenApiLicense"/>.
12+
/// </summary>
13+
public static class OpenApiLicenseRules
14+
{
15+
/// <summary>
16+
/// REQUIRED.
17+
/// </summary>
18+
public static readonly ValidationRule<OpenApiLicense> FieldIsRequired =
19+
new ValidationRule<OpenApiLicense>(
20+
(context, license) =>
21+
{
22+
context.Push("name");
23+
if (String.IsNullOrEmpty(license.Name))
24+
{
25+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
26+
String.Format(SRResource.Validation_FieldIsRequired, "name", "license"));
27+
context.AddError(error);
28+
}
29+
context.Pop();
30+
});
31+
32+
// add more rules
33+
}
34+
}

0 commit comments

Comments
 (0)