Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@
<data name="SourceExpressionHasInvalidFormat" xml:space="preserve">
<value>The source expression '{0}' has invalid format.</value>
</data>
<data name="Validation_PathItemMustBeginWithSlash" xml:space="preserve">
<value>The path item name '{0}' MUST begin with a slash.</value>
</data>
<data name="Validation_StringMustBeEmailAddress" xml:space="preserve">
<value>The string '{0}' MUST be in the format of an email address.</value>
</data>
</root>
75 changes: 75 additions & 0 deletions src/Microsoft.OpenApi/Validations/OpenApiElementValidator.cs
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 src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs
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 src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs
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 src/Microsoft.OpenApi/Validations/Rules/OpenApiDocumentRules.cs
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 src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs
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 =

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes.

new ValidationRule<IOpenApiExtensible>(
(context, item) =>
{
foreach (var extensible in item.Extensions)
{

}
});
}
}
31 changes: 31 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiInfoRules.cs
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 src/Microsoft.OpenApi/Validations/Rules/OpenApiPathsRules.cs
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
}
}
40 changes: 40 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
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;
}
}
}
Loading