Skip to content

Commit 9c19f93

Browse files
committed
Isolate the data mismatch rule into a separate class to allow clients to opt in
1 parent 3c5e279 commit 9c19f93

File tree

6 files changed

+128
-207
lines changed

6 files changed

+128
-207
lines changed

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Text.Json.Nodes;
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Validations.Rules
9+
{
10+
/// <summary>
11+
/// Defines a non-default set of rules for validating examples in header, media type and parameter objects against the schema
12+
/// </summary>
13+
public static class OpenApiNonDefaultRules
14+
{
15+
/// <summary>
16+
/// Validate the data matches with the given data type.
17+
/// </summary>
18+
public static ValidationRule<OpenApiHeader> HeaderMismatchedDataType =>
19+
new(nameof(HeaderMismatchedDataType),
20+
(context, header) =>
21+
{
22+
ValidateMismatchedDataType(context, nameof(HeaderMismatchedDataType), header.Example, header.Examples, header.Schema);
23+
});
24+
25+
/// <summary>
26+
/// Validate the data matches with the given data type.
27+
/// </summary>
28+
public static ValidationRule<OpenApiMediaType> MediaTypeMismatchedDataType =>
29+
new(nameof(MediaTypeMismatchedDataType),
30+
(context, mediaType) =>
31+
{
32+
ValidateMismatchedDataType(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Examples, mediaType.Schema);
33+
});
34+
35+
/// <summary>
36+
/// Validate the data matches with the given data type.
37+
/// </summary>
38+
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
39+
new(nameof(ParameterMismatchedDataType),
40+
(context, parameter) =>
41+
{
42+
ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema);
43+
});
44+
45+
/// <summary>
46+
/// Validate the data matches with the given data type.
47+
/// </summary>
48+
public static ValidationRule<OpenApiSchema> SchemaMismatchedDataType =>
49+
new(nameof(SchemaMismatchedDataType),
50+
(context, schema) =>
51+
{
52+
// default
53+
context.Enter("default");
54+
55+
if (schema.Default != null)
56+
{
57+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
58+
}
59+
60+
context.Exit();
61+
62+
// example
63+
context.Enter("example");
64+
65+
if (schema.Example != null)
66+
{
67+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
68+
}
69+
70+
context.Exit();
71+
72+
// enum
73+
context.Enter("enum");
74+
75+
if (schema.Enum != null)
76+
{
77+
for (var i = 0; i < schema.Enum.Count; i++)
78+
{
79+
context.Enter(i.ToString());
80+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
81+
context.Exit();
82+
}
83+
}
84+
85+
context.Exit();
86+
});
87+
88+
private static void ValidateMismatchedDataType(IValidationContext context,
89+
string ruleName,
90+
JsonNode example,
91+
IDictionary<string, OpenApiExample> examples,
92+
OpenApiSchema schema)
93+
{
94+
// example
95+
context.Enter("example");
96+
97+
if (example != null)
98+
{
99+
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema);
100+
}
101+
102+
context.Exit();
103+
104+
// enum
105+
context.Enter("examples");
106+
107+
if (examples != null)
108+
{
109+
foreach (var key in examples.Keys)
110+
{
111+
if (examples[key] != null)
112+
{
113+
context.Enter(key);
114+
context.Enter("value");
115+
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, examples[key]?.Value, schema);
116+
context.Exit();
117+
context.Exit();
118+
}
119+
}
120+
}
121+
122+
context.Exit();
123+
}
124+
}
125+
}

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,6 @@ public static class OpenApiParameterRules
5858
context.Exit();
5959
});
6060

61-
/// <summary>
62-
/// Validate the data matches with the given data type.
63-
/// </summary>
64-
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
65-
new(nameof(ParameterMismatchedDataType),
66-
(context, parameter) =>
67-
{
68-
// example
69-
context.Enter("example");
70-
71-
if (parameter.Example != null)
72-
{
73-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Schema);
74-
}
75-
76-
context.Exit();
77-
78-
// examples
79-
context.Enter("examples");
80-
81-
if (parameter.Examples != null)
82-
{
83-
foreach (var key in parameter.Examples.Keys)
84-
{
85-
if (parameter.Examples[key] != null)
86-
{
87-
context.Enter(key);
88-
context.Enter("value");
89-
RuleHelpers.ValidateDataTypeMismatch(context,
90-
nameof(ParameterMismatchedDataType), parameter.Examples[key]?.Value, parameter.Schema);
91-
context.Exit();
92-
context.Exit();
93-
}
94-
}
95-
}
96-
97-
context.Exit();
98-
});
99-
10061
/// <summary>
10162
/// Validate that a path parameter should always appear in the path
10263
/// </summary>

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,6 @@ namespace Microsoft.OpenApi.Validations.Rules
1313
[OpenApiRule]
1414
public static class OpenApiSchemaRules
1515
{
16-
/// <summary>
17-
/// Validate the data matches with the given data type.
18-
/// </summary>
19-
public static ValidationRule<OpenApiSchema> SchemaMismatchedDataType =>
20-
new(nameof(SchemaMismatchedDataType),
21-
(context, schema) =>
22-
{
23-
// default
24-
context.Enter("default");
25-
26-
if (schema.Default != null)
27-
{
28-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
29-
}
30-
31-
context.Exit();
32-
33-
// example
34-
context.Enter("example");
35-
36-
if (schema.Example != null)
37-
{
38-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
39-
}
40-
41-
context.Exit();
42-
43-
// enum
44-
context.Enter("enum");
45-
46-
if (schema.Enum != null)
47-
{
48-
for (var i = 0; i < schema.Enum.Count; i++)
49-
{
50-
context.Enter(i.ToString());
51-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
52-
context.Exit();
53-
}
54-
}
55-
56-
context.Exit();
57-
});
58-
5916
/// <summary>
6017
/// Validates Schema Discriminator
6118
/// </summary>

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT license.
44

@@ -329,17 +329,15 @@ internal static PropertyInfo[] GetValidationRuleTypes()
329329
..typeof(OpenApiExternalDocsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
330330
..typeof(OpenApiInfoRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
331331
..typeof(OpenApiLicenseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
332-
..typeof(OpenApiMediaTypeRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
333332
..typeof(OpenApiOAuthFlowRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
334333
..typeof(OpenApiServerRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
335334
..typeof(OpenApiResponseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
336335
..typeof(OpenApiResponsesRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
337336
..typeof(OpenApiSchemaRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
338-
..typeof(OpenApiHeaderRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
339337
..typeof(OpenApiTagRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
340338
..typeof(OpenApiPathsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
341-
..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
342-
];
339+
..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public)
340+
];
343341
}
344342
}
345343
}

0 commit comments

Comments
 (0)