Skip to content
Merged
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
72 changes: 72 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.

24 changes: 24 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<data name="IndentationLevelInvalid" xml:space="preserve">
<value>Indentation level cannot be lower than 0.</value>
</data>
<data name="InputItemShouldBeType" xml:space="preserve">
<value>The input item should be in type of '{0}'.</value>
</data>
<data name="ObjectScopeNeededForPropertyNameWriting" xml:space="preserve">
<value>The active scope must be an object scope for property name '{0}' to be written.</value>
</data>
Expand Down Expand Up @@ -177,4 +180,25 @@
<data name="SourceExpressionHasInvalidFormat" xml:space="preserve">
<value>The source expression '{0}' has invalid format.</value>
</data>
<data name="UnknownVisitorType" xml:space="preserve">
<value>Can not find visitor type registered for type '{0}'.</value>
</data>
<data name="Validation_ComponentsKeyMustMatchRegularExpr" xml:space="preserve">
<value>The key '{0}' in '{1}' of components MUST match the regular expression '{2}'.</value>
</data>
<data name="Validation_ExtensionNameMustBeginWithXDash" xml:space="preserve">
<value>The extension name '{0}' in '{1}' object MUST begin with 'x-'.</value>
</data>
<data name="Validation_FieldIsRequired" xml:space="preserve">
<value>The field '{0}' in '{1}' object is REQUIRED.</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_RuleAddTwice" xml:space="preserve">
<value>The same rule cannot be in the same rule set twice.</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>
73 changes: 73 additions & 0 deletions src/Microsoft.OpenApi/Validations/OpenApiElementValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

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 and output the whole validation errors.
/// </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 with the given rule set and output the whole validation errors.
/// </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();
}
}
}
67 changes: 67 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;

namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// The validation rules for <see cref="OpenApiComponents"/>.
/// </summary>
[OpenApiRule]
public static class OpenApiComponentsRules
{
/// <summary>
/// The key regex.
/// </summary>
public static Regex KeyRegex = new Regex(@"^[a-zA-Z0-9\.\-_]+$");

/// <summary>
/// All the fixed fields declared above are objects
/// that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$.
/// </summary>
public static ValidationRule<OpenApiComponents> KeyMustBeRegularExpression =>
new ValidationRule<OpenApiComponents>(
(context, components) =>
{
ValidateKeys(context, components.Schemas?.Keys, "schemas");

ValidateKeys(context, components.Responses?.Keys, "responses");

ValidateKeys(context, components.Parameters?.Keys, "parameters");

ValidateKeys(context, components.Examples?.Keys, "examples");

ValidateKeys(context, components.RequestBodies?.Keys, "requestBodies");

ValidateKeys(context, components.Headers?.Keys, "headers");

ValidateKeys(context, components.SecuritySchemes?.Keys, "securitySchemes");

ValidateKeys(context, components.Links?.Keys, "links");

ValidateKeys(context, components.Callbacks?.Keys, "callbacks");
});

private static void ValidateKeys(ValidationContext context, IEnumerable<string> keys, string component)
{
if (keys == null)
{
return;
}

foreach (var key in keys)
{
if (!KeyRegex.IsMatch(key))
{
ValidationError error = new ValidationError(ErrorReason.Format, context.PathString,
string.Format(SRResource.Validation_ComponentsKeyMustMatchRegularExpr, key, component, KeyRegex.ToString()));
context.AddError(error);
}
}
}
}
}
51 changes: 51 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;

namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// The validation rules for <see cref="OpenApiContact"/>.
/// </summary>
[OpenApiRule]
internal static class OpenApiContactRules
{
/// <summary>
/// Email field MUST be email address.
/// </summary>
public static 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.Format, context.PathString,
String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
context.AddError(error);
}
}
context.Pop();
});

/// <summary>
/// Url field MUST be url format.
/// </summary>
public static ValidationRule<OpenApiContact> UrlMustBeUrlFormat =>
new ValidationRule<OpenApiContact>(
(context, item) =>
{
context.Push("url");
if (item != null && item.Url != null)
{
// TODO:
}
context.Pop();
});
}
}
44 changes: 44 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiDocumentRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;

namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// The validation rules for <see cref="OpenApiDocument"/>.
/// </summary>
[OpenApiRule]
internal static class OpenApiDocumentRules
{
/// <summary>
/// The Info field is required.
/// </summary>
public static ValidationRule<OpenApiDocument> FieldIsRequired =>
new ValidationRule<OpenApiDocument>(
(context, item) =>
{
// info
context.Push("info");
if (item.Info == null)
{
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
String.Format(SRResource.Validation_FieldIsRequired, "info", "document"));
context.AddError(error);
}
context.Pop();

// paths
context.Push("paths");
if (item.Paths == null)
{
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
String.Format(SRResource.Validation_FieldIsRequired, "paths", "document"));
context.AddError(error);
}
context.Pop();
});
}
}
36 changes: 36 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Properties;

namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// The validation rules for <see cref="IOpenApiExtensible"/>.
/// </summary>
[OpenApiRule]
public static class OpenApiExtensibleRules
{
/// <summary>
/// Extension name MUST start with "x-".
/// </summary>
public static ValidationRule<IOpenApiExtensible> ExtensionNameMustStartWithXDash =>
new ValidationRule<IOpenApiExtensible>(
(context, item) =>
{
context.Push("extensions");
foreach (var extensible in item.Extensions)
{
if (!extensible.Key.StartsWith("x-"))
{
ValidationError error = new ValidationError(ErrorReason.Format, context.PathString,
String.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible.Key, context.PathString));
context.AddError(error);
}
}
context.Pop();
});
}
}
Loading