forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiNonDefaultRules.cs
More file actions
112 lines (103 loc) · 4.52 KB
/
OpenApiNonDefaultRules.cs
File metadata and controls
112 lines (103 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi
{
/// <summary>
/// Defines a non-default set of rules for validating examples in header, media type and parameter objects against the schema
/// </summary>
public static class OpenApiNonDefaultRules
{
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<IOpenApiHeader> HeaderMismatchedDataType =>
new(nameof(HeaderMismatchedDataType),
(context, header) =>
{
ValidateMismatchedDataType(context, nameof(HeaderMismatchedDataType), header.Example, header.Examples, header.Schema);
});
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiMediaType> MediaTypeMismatchedDataType =>
new(nameof(MediaTypeMismatchedDataType),
(context, mediaType) =>
{
ValidateMismatchedDataType(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Examples, mediaType.Schema);
});
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
new(nameof(ParameterMismatchedDataType),
(context, parameter) =>
{
ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema);
});
/// <summary>
/// Validate the data matches with the given data type.
/// </summary>
public static ValidationRule<IOpenApiSchema> SchemaMismatchedDataType =>
new(nameof(SchemaMismatchedDataType),
(context, schema) =>
{
// default
if (schema.Default != null)
{
context.Enter("default");
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
context.Exit();
}
// example
if (schema.Example != null)
{
context.Enter("example");
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
context.Exit();
}
// enum
if (schema.Enum != null)
{
context.Enter("enum");
for (var i = 0; i < schema.Enum.Count; i++)
{
context.Enter(i.ToString());
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
context.Exit();
}
context.Exit();
}
});
private static void ValidateMismatchedDataType(IValidationContext context,
string ruleName,
JsonNode? example,
IDictionary<string, IOpenApiExample>? examples,
IOpenApiSchema? schema)
{
// example
if (example != null)
{
context.Enter("example");
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema);
context.Exit();
}
// enum
if (examples != null)
{
context.Enter("examples");
foreach (var key in examples.Keys.Where(k => examples[k] != null))
{
context.Enter(key);
context.Enter("value");
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, examples[key]?.Value, schema);
context.Exit();
context.Exit();
}
context.Exit();
}
}
}
}