FluentContracts 3.4.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentContracts --version 3.4.0
                    
NuGet\Install-Package FluentContracts -Version 3.4.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="FluentContracts" Version="3.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentContracts" Version="3.4.0" />
                    
Directory.Packages.props
<PackageReference Include="FluentContracts" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentContracts --version 3.4.0
                    
#r "nuget: FluentContracts, 3.4.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.
#:package FluentContracts@3.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentContracts&version=3.4.0
                    
Install as a Cake Addin
#tool nuget:?package=FluentContracts&version=3.4.0
                    
Install as a Cake Tool

FluentContracts

NuGet Version NuGet Downloads License: MIT

Argument validation contracts, written the way you would say them out loud.

Inspired by FluentAssertions, and MIT licensed.

Why another validation library

Libraries like FluentValidation and Guard from the .NET Community Toolkit are excellent, and if you already use them, keep doing so.

This one exists because guard clauses tend to read as noise. FluentContracts aims for something simpler and more human-readable, the way FluentAssertions does for unit testing.

Getting started

dotnet add package FluentContracts

Targets netstandard2.0 and net8.0, so it runs on .NET Framework 4.6.1+, .NET Core, .NET 5 and later, Mono and Unity. It has no runtime dependencies.

Usage

Validation normally looks like this:

public void AddOrder(Order myOrder)
{
    if (myOrder == null) throw new ArgumentNullException(nameof(myOrder));
    if (myOrder.Quantity < 5) throw new OrderQuantityException("Order quantity cannot be less than 5");

    ...
}

With FluentContracts it reads like the rule it enforces:

public void AddOrder(Order myOrder)
{
    myOrder
        .Must()
        .NotBeNull()
        .And
        .Satisfy<OrderQuantityException>(
            o => o.Quantity >= 5,
            "Order quantity cannot be less than 5");

    ...
}

Or as simple as:

public int Divide(int a, int b)
{
    b.Must().NotBe(0);
    return a / b;
}

The argument name is captured automatically, so the exception points at the right parameter without you repeating it.

Your own exceptions

Any check can throw an exception of your choosing:

public void AddOrder(Order myOrder)
{
    myOrder.Must().NotBeNull<OrderNullException>();
}

This throws OrderNullException when myOrder is null.

What you can check

Contracts are available for the numeric types, string, char, bool, Guid, DateTime, TimeSpan, enums, collections, dictionaries, streams, FileInfo and DirectoryInfo — things like NotBeNullOrEmpty, BeBetween, HaveCountGreaterThan, BeEmailAddress, Exist and many more.

The full list lives in SupportedContracts.md.

Help needed

The goal is for this to be exhaustive, safe and stable enough for production use on large projects, and help is very welcome. Open a pull request, file an issue, or start a discussion.

License

MIT © Todor Todorov

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1 130 9/4/2026
4.0.0 115 9/2/2026
3.7.0 113 9/2/2026
3.6.1 110 9/1/2026
3.6.0 125 9/1/2026
3.5.0 110 9/1/2026
3.4.0 117 9/1/2026
3.3.0 115 9/1/2026
3.2.0 136 8/31/2026
3.1.0 117 8/31/2026
3.0.0 122 8/30/2026
2.1.0 316 8/23/2024
2.0.0 266 6/16/2024
1.4.0 213 5/20/2024
1.3.0 256 5/6/2024
1.2.0 226 4/29/2024
1.1.1 241 4/26/2024
1.0.1 227 4/23/2024
Loading failed