AzureFunctionsOpenApiFluentValidationExtensions 1.0.0

dotnet add package AzureFunctionsOpenApiFluentValidationExtensions --version 1.0.0
NuGet\Install-Package AzureFunctionsOpenApiFluentValidationExtensions -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureFunctionsOpenApiFluentValidationExtensions" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureFunctionsOpenApiFluentValidationExtensions --version 1.0.0
#r "nuget: AzureFunctionsOpenApiFluentValidationExtensions, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AzureFunctionsOpenApiFluentValidationExtensions as a Cake Addin
#addin nuget:?package=AzureFunctionsOpenApiFluentValidationExtensions&version=1.0.0

// Install AzureFunctionsOpenApiFluentValidationExtensions as a Cake Tool
#tool nuget:?package=AzureFunctionsOpenApiFluentValidationExtensions&version=1.0.0

AzureFunctionsOpenApiFluentValidationExtensions

Decorates the OpenApi Specification generated using Azure Functions OpenAPI Extension with FluentValidation rules loaded at runtime.

Motivation

The recommended library for generating the OpenAPI specification for Azure Functions is Azure Functions OpenAPI Extension. While the library can annotate the model when using Data Annotations it doesn't support FluentValidation.

As FluentValidation is a widely used library for building strongly-typed validation rules, this package fills the gap by decorating the OpenAPI Specification with the appropriate constraints for HTTP request models, based on the validation rules. It also allows to define rules for HTTP parameters in URLs (see Advanced Usage).

Limitations

Currently, only basic rules are supported and the regex dialect is not translated.

Quick Start

Add to your project:

<PackageReference Include="AzureFunctionsOpenApiFluentValidationExtensions.DependencyInjection" Version="1.0.0" />

Add to your OpenApi configuration:

DocumentFilters.AddAzureFunctionsOpenApiFluentValidationExtensions<Startup>();

Usage

Add to your project:

<PackageReference Include="AzureFunctionsOpenApiFluentValidationExtensions.DependencyInjection" Version="1.0.0" />

In your custom object to define the OpenApi configuration, use AddAzureFunctionsOpenApiFluentValidationExtensions on the DocumentFilters property.

public class OpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
    public OpenApiConfigurationOptions()
    {
        // Add all validators via reflection.
        DocumentFilters.AddAzureFunctionsOpenApiFluentValidationExtensions<Startup>(services =>
        {
            // Add dependencies required to build the validators.
            // Note the OpenAPI generator doesn't use the DI container.
            services.AddSingleton<IClock>(SystemClock.Instance);
        },
        options =>
        {
            // If any operation has query string parameters that you group into an object and use a validator,
            // add it here using the operation id specified for that function endpoint with [OpenApiOperation(operationId: nameof(MyOperation))]
            options.Operations.Add<MyRequestModel>(nameof(MyOperation));
        });
    }

    public override OpenApiInfo Info { get; set; } = new()
    {
        Version = "v1",
        Title = "NewDay 🥳 API"
        // something else
    };
}

Advanced Usage

Add to your project:

<PackageReference Include="AzureFunctionsOpenApiFluentValidationExtensions" Version="1.0.0" />

If you are not using DI in Azure Functions or using static Functions, you would need to manually register all the validators you are using.

public class OpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
    public OpenApiConfigurationOptions()
    {
        var builder = new FunctionsValidationDocumentFilterBuilder();

        // register a validator
        builder.AddValidator(new MyValidator());

        // register an operation which has query string parameters that you group into an object and use a validator,
        // add it here using the operation id specified for that function endpoint with [OpenApiOperation(operationId: nameof(MyOperation))]
        builder.AddOperationSchema<MyRequestModel>(nameof(MyOperation))

        var filter = builder.Build();

        DocumentFilters.Add(filter);
    }

    public override OpenApiInfo Info { get; set; } = new()
    {
        Version = "v1",
        Title = "NewDay 🥳 API"
        // something else
    };
}

Contributing

Handling new rules

  1. Implement a new rule in AzureFunctionsOpenApiFluentValidationExtensions.Rules that extend Rule and define the required properties. This represents an internal representation of a rule.

  2. Add a new case in AzureFunctionsOpenApiFluentValidationExtensions.ValidatorMapper that map from a FluentValidation type of a rule to the new modeled rule.

  3. Extend AzureFunctionsOpenApiFluentValidationExtensions.FunctionsValidationDocumentFilter:

    1. Add a new case in the ApplySchemas method for the new rule to set the necessary properties of a field of a schema, based on the rule.
    2. Add a new case in the ApplySchemasToOperations method for the new rule to set the necessary properties of a parameter of an operation, based on the rule.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AzureFunctionsOpenApiFluentValidationExtensions:

Package Downloads
AzureFunctionsOpenApiFluentValidationExtensions.DependencyInjection

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 185 12/19/2023
0.2.0-preview.2 54 12/19/2023
0.2.0-preview.1 49 12/19/2023