Skip to content

Commit 3c5e279

Browse files
committed
Revert "remove depracated validation rule"
This reverts commit 9bf3f08.
1 parent ad3b65d commit 3c5e279

File tree

6 files changed

+453
-0
lines changed

6 files changed

+453
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.OpenApi.Models;
5+
6+
namespace Microsoft.OpenApi.Validations.Rules
7+
{
8+
/// <summary>
9+
/// The validation rules for <see cref="OpenApiHeader"/>.
10+
/// </summary>
11+
//Removed from Default Rules as this is not a MUST in OpenAPI
12+
[OpenApiRule]
13+
public static class OpenApiHeaderRules
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+
// example
23+
context.Enter("example");
24+
25+
if (header.Example != null)
26+
{
27+
RuleHelpers.ValidateDataTypeMismatch(context,
28+
nameof(HeaderMismatchedDataType), header.Example, header.Schema);
29+
}
30+
31+
context.Exit();
32+
33+
// examples
34+
context.Enter("examples");
35+
36+
if (header.Examples != null)
37+
{
38+
foreach (var key in header.Examples.Keys)
39+
{
40+
if (header.Examples[key] != null)
41+
{
42+
context.Enter(key);
43+
context.Enter("value");
44+
RuleHelpers.ValidateDataTypeMismatch(context,
45+
nameof(HeaderMismatchedDataType), header.Examples[key]?.Value, header.Schema);
46+
context.Exit();
47+
context.Exit();
48+
}
49+
}
50+
}
51+
52+
context.Exit();
53+
});
54+
55+
// add more rule.
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.OpenApi.Models;
5+
6+
namespace Microsoft.OpenApi.Validations.Rules
7+
{
8+
/// <summary>
9+
/// The validation rules for <see cref="OpenApiMediaType"/>.
10+
/// </summary>
11+
/// <remarks>
12+
/// Removed this in v1.3 as a default rule as the OpenAPI specification does not require that example
13+
/// values validate against the schema. Validating examples against the schema is particularly difficult
14+
/// as it requires parsing of the example using the schema as a guide. This is not possible when the schema
15+
/// is referenced. Even if we fix this issue, this rule should be treated as a warning, not an error
16+
/// Future versions of the validator should make that distinction.
17+
/// Future versions of the example parsers should not try an infer types.
18+
/// Example validation should be done as a separate post reading step so all schemas can be fully available.
19+
/// </remarks>
20+
[OpenApiRule]
21+
public static class OpenApiMediaTypeRules
22+
{
23+
/// <summary>
24+
/// Validate the data matches with the given data type.
25+
/// </summary>
26+
public static ValidationRule<OpenApiMediaType> MediaTypeMismatchedDataType =>
27+
new(nameof(MediaTypeMismatchedDataType),
28+
(context, mediaType) =>
29+
{
30+
// example
31+
context.Enter("example");
32+
33+
if (mediaType.Example != null)
34+
{
35+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Schema);
36+
}
37+
38+
context.Exit();
39+
40+
// enum
41+
context.Enter("examples");
42+
43+
if (mediaType.Examples != null)
44+
{
45+
foreach (var key in mediaType.Examples.Keys)
46+
{
47+
if (mediaType.Examples[key] != null)
48+
{
49+
context.Enter(key);
50+
context.Enter("value");
51+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(MediaTypeMismatchedDataType), mediaType.Examples[key]?.Value, mediaType.Schema);
52+
context.Exit();
53+
context.Exit();
54+
}
55+
}
56+
}
57+
58+
context.Exit();
59+
});
60+
61+
// add more rule.
62+
}
63+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,45 @@ 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+
61100
/// <summary>
62101
/// Validate that a path parameter should always appear in the path
63102
/// </summary>

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ 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+
1659
/// <summary>
1760
/// Validates Schema Discriminator
1861
/// </summary>

0 commit comments

Comments
 (0)