Skip to content

Commit ad1d5d7

Browse files
committed
simplify code
1 parent 8ebab68 commit ad1d5d7

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public static void ValidateDataTypeMismatch(
5252
}
5353

5454
// convert value to JsonElement and access the ValueKind property to determine the type.
55-
var jsonElement = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement;
55+
var valueKind = value.GetValueKind();
5656

5757
var type = schema.Type.ToIdentifier();
5858
var format = schema.Format;
5959
var nullable = schema.Nullable;
6060

6161
// Before checking the type, check first if the schema allows null.
6262
// If so and the data given is also null, this is allowed for any type.
63-
if (nullable && jsonElement.ValueKind is JsonValueKind.Null)
63+
if (nullable && valueKind is JsonValueKind.Null)
6464
{
6565
return;
6666
}
@@ -70,7 +70,7 @@ public static void ValidateDataTypeMismatch(
7070
// It is not against the spec to have a string representing an object value.
7171
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
7272
// a string value can contain the example with escaping where necessary
73-
if (jsonElement.ValueKind is JsonValueKind.String)
73+
if (valueKind is JsonValueKind.String)
7474
{
7575
return;
7676
}
@@ -110,7 +110,7 @@ public static void ValidateDataTypeMismatch(
110110
// It is not against the spec to have a string representing an array value.
111111
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
112112
// a string value can contain the example with escaping where necessary
113-
if (jsonElement.ValueKind is JsonValueKind.String)
113+
if (valueKind is JsonValueKind.String)
114114
{
115115
return;
116116
}
@@ -138,7 +138,7 @@ public static void ValidateDataTypeMismatch(
138138

139139
if (type is "integer" or "number" && format is "int32")
140140
{
141-
if (jsonElement.ValueKind is not JsonValueKind.Number)
141+
if (valueKind is not JsonValueKind.Number)
142142
{
143143
context.CreateWarning(
144144
ruleName,
@@ -150,7 +150,7 @@ public static void ValidateDataTypeMismatch(
150150

151151
if (type is "integer" or "number" && format is "int64")
152152
{
153-
if (jsonElement.ValueKind is not JsonValueKind.Number)
153+
if (valueKind is not JsonValueKind.Number)
154154
{
155155
context.CreateWarning(
156156
ruleName,
@@ -162,7 +162,7 @@ public static void ValidateDataTypeMismatch(
162162

163163
if (type is "integer")
164164
{
165-
if (jsonElement.ValueKind is not JsonValueKind.Number)
165+
if (valueKind is not JsonValueKind.Number)
166166
{
167167
context.CreateWarning(
168168
ruleName,
@@ -174,7 +174,7 @@ public static void ValidateDataTypeMismatch(
174174

175175
if (type is "number" && format is "float")
176176
{
177-
if (jsonElement.ValueKind is not JsonValueKind.Number)
177+
if (valueKind is not JsonValueKind.Number)
178178
{
179179
context.CreateWarning(
180180
ruleName,
@@ -186,7 +186,7 @@ public static void ValidateDataTypeMismatch(
186186

187187
if (type is "number" && format is "double")
188188
{
189-
if (jsonElement.ValueKind is not JsonValueKind.Number)
189+
if (valueKind is not JsonValueKind.Number)
190190
{
191191
context.CreateWarning(
192192
ruleName,
@@ -198,7 +198,7 @@ public static void ValidateDataTypeMismatch(
198198

199199
if (type is "number")
200200
{
201-
if (jsonElement.ValueKind is not JsonValueKind.Number)
201+
if (valueKind is not JsonValueKind.Number)
202202
{
203203
context.CreateWarning(
204204
ruleName,
@@ -210,7 +210,7 @@ public static void ValidateDataTypeMismatch(
210210

211211
if (type is "string" && format is "byte")
212212
{
213-
if (jsonElement.ValueKind is not JsonValueKind.String)
213+
if (valueKind is not JsonValueKind.String)
214214
{
215215
context.CreateWarning(
216216
ruleName,
@@ -222,7 +222,7 @@ public static void ValidateDataTypeMismatch(
222222

223223
if (type is "string" && format is "date")
224224
{
225-
if (jsonElement.ValueKind is not JsonValueKind.String)
225+
if (valueKind is not JsonValueKind.String)
226226
{
227227
context.CreateWarning(
228228
ruleName,
@@ -234,7 +234,7 @@ public static void ValidateDataTypeMismatch(
234234

235235
if (type is "string" && format is "date-time")
236236
{
237-
if (jsonElement.ValueKind is not JsonValueKind.String)
237+
if (valueKind is not JsonValueKind.String)
238238
{
239239
context.CreateWarning(
240240
ruleName,
@@ -246,7 +246,7 @@ public static void ValidateDataTypeMismatch(
246246

247247
if (type is "string" && format is "password")
248248
{
249-
if (jsonElement.ValueKind is not JsonValueKind.String)
249+
if (valueKind is not JsonValueKind.String)
250250
{
251251
context.CreateWarning(
252252
ruleName,
@@ -258,7 +258,7 @@ public static void ValidateDataTypeMismatch(
258258

259259
if (type is "string")
260260
{
261-
if (jsonElement.ValueKind is not JsonValueKind.String)
261+
if (valueKind is not JsonValueKind.String)
262262
{
263263
context.CreateWarning(
264264
ruleName,
@@ -270,7 +270,7 @@ public static void ValidateDataTypeMismatch(
270270

271271
if (type is "boolean")
272272
{
273-
if (jsonElement.ValueKind is not JsonValueKind.True && jsonElement.ValueKind is not JsonValueKind.False)
273+
if (valueKind is not JsonValueKind.True && valueKind is not JsonValueKind.False)
274274
{
275275
context.CreateWarning(
276276
ruleName,

0 commit comments

Comments
 (0)