-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1888 from microsoft/mk/remove-validation-rule
Remove DataTypeMismatch validation rule from the default ruleset
- Loading branch information
Showing
13 changed files
with
151 additions
and
341 deletions.
There are no files selected for viewing
57 changes: 0 additions & 57 deletions
57
src/Microsoft.OpenApi/Validations/Rules/OpenApiHeaderRules.cs
This file was deleted.
Oops, something went wrong.
63 changes: 0 additions & 63 deletions
63
src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs
This file was deleted.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
src/Microsoft.OpenApi/Validations/Rules/OpenApiNonDefaultRules.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Nodes; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.OpenApi.Validations.Rules | ||
{ | ||
/// <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<OpenApiHeader> 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<OpenApiSchema> SchemaMismatchedDataType => | ||
new(nameof(SchemaMismatchedDataType), | ||
(context, schema) => | ||
{ | ||
// default | ||
context.Enter("default"); | ||
if (schema.Default != null) | ||
{ | ||
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema); | ||
} | ||
context.Exit(); | ||
// example | ||
context.Enter("example"); | ||
if (schema.Example != null) | ||
{ | ||
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema); | ||
} | ||
context.Exit(); | ||
// enum | ||
context.Enter("enum"); | ||
if (schema.Enum != null) | ||
{ | ||
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, OpenApiExample> examples, | ||
OpenApiSchema schema) | ||
{ | ||
// example | ||
context.Enter("example"); | ||
|
||
if (example != null) | ||
{ | ||
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema); | ||
} | ||
|
||
context.Exit(); | ||
|
||
// enum | ||
context.Enter("examples"); | ||
|
||
if (examples != null) | ||
{ | ||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.