-
Notifications
You must be signed in to change notification settings - Fork 281
Init implementation for the Open API validation #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
75 changes: 75 additions & 0 deletions
75
src/Microsoft.OpenApi/Validations/OpenApiElementValidator.cs
This file contains hidden or 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,75 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OpenApi.Interfaces; | ||
| using Microsoft.OpenApi.Validations.Rules; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations | ||
| { | ||
| /// <summary> | ||
| /// The public APIs to validate the Open API element. | ||
| /// </summary> | ||
| public static class OpenApiElementValidator | ||
| { | ||
| /// <summary> | ||
| /// Validate the Open API element. | ||
| /// </summary> | ||
| /// <typeparam name="T">The Open API element type.</typeparam> | ||
| /// <param name="element">The Open API element.</param> | ||
| /// <returns>True means no errors, otherwise with errors.</returns> | ||
| public static bool Validate<T>(this T element) where T : IOpenApiElement | ||
| { | ||
| IEnumerable<ValidationError> errors; | ||
| return element.Validate(out errors); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validate the Open API element. | ||
| /// </summary> | ||
| /// <typeparam name="T">The Open API element type.</typeparam> | ||
| /// <param name="element">The Open API element.</param> | ||
| /// <param name="errors">The output errors.</param> | ||
| /// <returns>True means no errors, otherwise with errors.</returns> | ||
| public static bool Validate<T>(this T element, out IEnumerable<ValidationError> errors) | ||
| where T : IOpenApiElement | ||
| { | ||
| ValidationRuleSet ruleSet = ValidationRuleSet.DefaultRuleSet; | ||
| return element.Validate(ruleSet, out errors); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validate the Open API element. | ||
| /// </summary> | ||
| /// <typeparam name="T">The Open API element type.</typeparam> | ||
| /// <param name="element">The Open API element.</param> | ||
| /// <param name="ruleSet">The input rule set.</param> | ||
| /// <param name="errors">The output errors.</param> | ||
| /// <returns>True means no errors, otherwise with errors.</returns> | ||
| public static bool Validate<T>(this T element, ValidationRuleSet ruleSet, out IEnumerable<ValidationError> errors) | ||
| where T : IOpenApiElement | ||
| { | ||
| errors = null; | ||
| if (element == null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (ruleSet == null) | ||
| { | ||
| ruleSet = ValidationRuleSet.DefaultRuleSet; | ||
| } | ||
|
|
||
| ValidationContext context = new ValidationContext(ruleSet); | ||
|
|
||
| context.Validate(element); | ||
|
|
||
| errors = context.Errors; | ||
|
|
||
| return !errors.Any(); | ||
| } | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs
This file contains hidden or 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,16 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="OpenApiComponents"/>. | ||
| /// </summary> | ||
| public static class OpenApiComponentsRules | ||
| { | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs
This file contains hidden or 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,52 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using Microsoft.OpenApi.Models; | ||
| using Microsoft.OpenApi.Properties; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="OpenApiContact"/>. | ||
| /// </summary> | ||
| internal static class OpenApiContactRules | ||
| { | ||
| /// <summary> | ||
| /// Email field MUST be email address. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiContact> EmailMustBeEmailFormat = | ||
| new ValidationRule<OpenApiContact>( | ||
| (context, item) => | ||
| { | ||
| context.Push("email"); | ||
| if (item != null && item.Email != null) | ||
| { | ||
| if (!item.Email.IsEmailAddress()) | ||
| { | ||
| ValidationError error = new ValidationError(ErrorReason.EmailFormat, context.PathString, | ||
| String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email)); | ||
| context.AddError(error); | ||
| } | ||
| } | ||
| context.Pop(); | ||
| }); | ||
|
|
||
| /// <summary> | ||
| /// Url field MUST be url format. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiContact> UrlMustBeUrlFormat = | ||
| new ValidationRule<OpenApiContact>( | ||
| (context, item) => | ||
| { | ||
| context.Push("url"); | ||
| if (item != null && item.Url != null) | ||
| { | ||
| // TODO: | ||
| } | ||
| context.Pop(); | ||
| }); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/Microsoft.OpenApi/Validations/Rules/OpenApiDocumentRules.cs
This file contains hidden or 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,41 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="OpenApiDocument"/>. | ||
| /// </summary> | ||
| internal static class OpenApiDocumentRules | ||
| { | ||
| /// <summary> | ||
| /// The Info field is required. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiDocument> InfoIsRequired = | ||
| new ValidationRule<OpenApiDocument>( | ||
| (context, item) => | ||
| { | ||
| if (item.Info == null) | ||
| { | ||
| //context.AddError(); | ||
| } | ||
| }); | ||
|
|
||
| /// <summary> | ||
| /// The Paths field is required. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiDocument> PathsIsRequired = | ||
| new ValidationRule<OpenApiDocument>( | ||
| (context, item) => | ||
| { | ||
| if (item.Paths == null) | ||
| { | ||
| //context.AddError(); | ||
| } | ||
| }); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs
This file contains hidden or 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,28 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using Microsoft.OpenApi.Interfaces; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="IOpenApiExtensible"/>. | ||
| /// </summary> | ||
| public static class OpenApiExtensibleRules | ||
| { | ||
| /// <summary> | ||
| /// Extension name MUST start with "x-". | ||
| /// </summary> | ||
| private static readonly ValidationRule<IOpenApiExtensible> ExtensionNameMustStartWithXDollar = | ||
| new ValidationRule<IOpenApiExtensible>( | ||
| (context, item) => | ||
| { | ||
| foreach (var extensible in item.Extensions) | ||
| { | ||
|
|
||
| } | ||
| }); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
src/Microsoft.OpenApi/Validations/Rules/OpenApiInfoRules.cs
This file contains hidden or 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,31 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="OpenApiInfo"/>. | ||
| /// </summary> | ||
| internal static class OpenApiInfoRules | ||
| { | ||
| /// <summary> | ||
| /// The title of the application is required. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiInfo> TitleIsRequired = | ||
| new ValidationRule<OpenApiInfo>( | ||
| (context, item) => | ||
| { | ||
| if (String.IsNullOrEmpty(item.Title)) | ||
| { | ||
| // add error. | ||
| } | ||
| }); | ||
|
|
||
| // add more rule. | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/Microsoft.OpenApi/Validations/Rules/OpenApiPathsRules.cs
This file contains hidden or 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,46 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using Microsoft.OpenApi.Models; | ||
| using Microsoft.OpenApi.Properties; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| /// <summary> | ||
| /// The validation rules for <see cref="OpenApiPaths"/>. | ||
| /// </summary> | ||
| public static class OpenApiPathsRules | ||
| { | ||
| /// <summary> | ||
| /// A relative path to an individual endpoint. The field name MUST begin with a slash. | ||
| /// </summary> | ||
| public static readonly ValidationRule<OpenApiPaths> PathNameMustBeginWithSlash = | ||
| new ValidationRule<OpenApiPaths>( | ||
| (context, item) => | ||
| { | ||
| foreach (var pathName in item.Keys) | ||
| { | ||
| context.Push(pathName); | ||
|
|
||
| if (string.IsNullOrEmpty(pathName)) | ||
| { | ||
| // Add the error message | ||
| // context.Add(...); | ||
| } | ||
|
|
||
| if (!pathName.StartsWith("/")) | ||
| { | ||
| ValidationError error = new ValidationError(ErrorReason.Format, context.PathString, | ||
| string.Format(SRResource.Validation_PathItemMustBeginWithSlash, pathName)); | ||
| context.AddError(error); | ||
| } | ||
|
|
||
| context.Pop(); | ||
| } | ||
| }); | ||
|
|
||
| // add more rules | ||
| } | ||
| } |
This file contains hidden or 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,40 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.OpenApi.Validations.Rules | ||
| { | ||
| internal static class RuleHelpers | ||
| { | ||
| /// <summary> | ||
| /// Input string must be in the format of an email address | ||
| /// </summary> | ||
| /// <param name="input">The input string.</param> | ||
| /// <returns></returns> | ||
| public static bool IsEmailAddress(this string input) | ||
| { | ||
| if (String.IsNullOrEmpty(input)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var splits = input.Split('@'); | ||
| if (splits.Length != 2) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (String.IsNullOrEmpty(splits[0]) || String.IsNullOrEmpty(splits[1])) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Add more rules. | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XDollar? Do you mean XDash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes.