MiniValidation 0.7.4

.NET 6.0 .NET Standard 2.0
dotnet add package MiniValidation --version 0.7.4
NuGet\Install-Package MiniValidation -Version 0.7.4
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="MiniValidation" Version="0.7.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MiniValidation --version 0.7.4
#r "nuget: MiniValidation, 0.7.4"
#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 MiniValidation as a Cake Addin
#addin nuget:?package=MiniValidation&version=0.7.4

// Install MiniValidation as a Cake Tool
#tool nuget:?package=MiniValidation&version=0.7.4

MiniValidation

A minimalistic validation library built atop the existing features in .NET's System.ComponentModel.DataAnnotations namespace. Adds support for single-line validation calls and recursion with cycle detection.

Supports .NET Standard 2.0 compliant runtimes.

Installation

Nuget

Install the library from NuGet:

❯ dotnet add package MiniValidation

ASP.NET Core 6+ Projects

If installing into an ASP.NET Core 6+ project, consider using the MinimalApis.Extensions package instead, which adds extensions specific to ASP.NET Core, including a validation endpoint filter for .NET 7 apps:

❯ dotnet add package MinimalApis.Extensions --prerelease

Example usage

Console app

using System.ComponentModel.DataAnnotations;
using MiniValidation;

var title = args.Length > 0 ? args[0] : "";

var widgets = new List<Widget>
{
    new Widget { Name = title },
    new WidgetWithCustomValidation { Name = title }
};

foreach (var widget in widgets)
{
    if (!MiniValidator.TryValidate(widget, out var errors))
    {
        Console.WriteLine($"{nameof(Widget)} has errors!");
        foreach (var entry in errors)
        {
            Console.WriteLine($"  {entry.Key}:");
            foreach (var error in entry.Value)
            {
                Console.WriteLine($"  - {error}");
            }
        }
    }
    else
    {
        Console.WriteLine($"{nameof(Widget)} '{widget}' is valid!");
    }
}

class Widget
{
    [Required, MinLength(3)]
    public string Name { get; set; }

    public override string ToString() => Name;
}

class WidgetWithCustomValidation : Widget, IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.Equals(Name, "Widget", StringComparison.OrdinalIgnoreCase))
        {
            yield return new($"Cannot name a widget '{Name}'.", new[] { nameof(Name) });
        }
    }
}
❯ widget.exe
Widget 'widget' is valid!
Widget has errors!
  Name:
  - Cannot name a widget 'widget'.

❯ widget.exe Ok
Widget has errors!
  Name:
  - The field Name must be a string or array type with a minimum length of '3'.
Widget has errors!
  Name:
  - The field Name must be a string or array type with a minimum length of '3'.

❯ widget.exe Widget
Widget 'Widget' is valid!
Widget has errors!
  Name:
  - Cannot name a widget 'Widget'.

❯ widget.exe MiniValidation
Widget 'MiniValidation' is valid!
Widget 'MiniValidation' is valid!

Web app (.NET 6)

using System.ComponentModel.DataAnnotations;
using MiniValidation;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World");

app.MapGet("/widgets", () =>
    new[] {
        new Widget { Name = "Shinerizer" },
        new Widget { Name = "Sparklizer" }
    });

app.MapGet("/widgets/{name}", (string name) =>
    new Widget { Name = name });

app.MapPost("/widgets", (Widget widget) =>
    !MiniValidator.TryValidate(widget, out var errors)
        ? Results.ValidationProblem(errors)
        : Results.Created($"/widgets/{widget.Name}", widget));

app.MapPost("/widgets/custom-validation", (WidgetWithCustomValidation widget) =>
    !MiniValidator.TryValidate(widget, out var errors)
        ? Results.ValidationProblem(errors)
        : Results.Created($"/widgets/{widget.Name}", widget));

app.Run();

class Widget
{
    [Required, MinLength(3)]
    public string? Name { get; set; }

    public override string? ToString() => Name;
}

class WidgetWithCustomValidation : Widget, IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.Equals(Name, "Widget", StringComparison.OrdinalIgnoreCase))
        {
            yield return new($"Cannot name a widget '{Name}'.", new[] { nameof(Name) });
        }
    }
}

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on MiniValidation:

Package Downloads
Indice.AspNetCore

Package Description

MinimalApis.Extensions

A set of extensions and helpers for working with ASP.NET Core Minimal APIs.

MinApiLib.Validation

Data annotations validation filter for Minimal API

Indice.Scalefin.Server

Package Description

IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.MinimalApi

ASP.NET Core types for hosting App Store Connect adapter APIs.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on MiniValidation:

Repository Stars
davidfowl/TodoApi
Todo application with ASP.NET Core Blazor WASM, Minimal APIs and Authentication
DamianEdwards/MinimalApis.Extensions
A set of extensions and helpers for working with ASP.NET Core Minimal APIs.
tinglesoftware/dependabot-azure-devops
Tools for updating dependencies in Azure DevOps repositories using https://dependabot.com
Version Downloads Last updated
0.7.4 596 3/17/2023
0.7.3 332 3/14/2023
0.7.2 11,166 12/21/2022
0.7.1 434 12/20/2022
0.7.0 6,618 11/29/2022
0.6.0-pre.20220527.55 32,328 5/27/2022
0.5.1-pre.20220429.53 3,885 4/29/2022
0.5.0-pre.20220315.50 14,025 3/15/2022
0.4.2-pre.20220306.48 1,691 3/6/2022
0.4.1-pre.20220107.41 7,270 1/7/2022
0.4.0-pre.20211110.38 6,344 11/10/2021
0.3.0-pre.20210927201159.35 1,462 9/27/2021
0.2.0-pre.20210920234953.30 1,740 9/20/2021