FluentContracts

Argument validation that reads like the rule it enforces, and fails with a message that says what
was expected of which argument. Inspired by FluentAssertions.
dotnet add package FluentContracts
Why another validation library
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: three lines of if and throw for one
rule, a nameof to keep in sync, an exception type to pick, and a message to write — or, more often,
not write. FluentContracts makes the rule the whole statement, the way FluentAssertions does for a
test, and puts the argument name, the exception type and a readable message in for you.
A guard clause, before and after
public void AddOrder(Order myOrder)
{
if (myOrder == null) throw new ArgumentNullException(nameof(myOrder));
if (myOrder.Quantity < 5) throw new ArgumentOutOfRangeException(nameof(myOrder), "Quantity cannot be less than 5");
}
public void AddOrder(Order myOrder)
{
myOrder.Must().NotBeNull().Satisfy<Order>(o => o.Quantity >= 5, "Quantity cannot be less than 5");
}
Must() starts a chain on any value. Every check returns the same chain, so checks follow one
another with no glue; .And. between them is optional and purely for reading. The argument's name
is captured at the call site, so exceptions point at the right parameter without nameof.
What every check gives you
A failure message that names the argument, the expectation and the value.
port.Must().BeBetween(1, 65535);
// ArgumentOutOfRangeException: Expected port to be between 1 and 65535, but found 70000. (Parameter 'port')
email.Must().BeEmailAddress();
// ArgumentException: Expected email to be a valid email address, but found "not-an-email". (Parameter 'email')
pages.Must().BeInAscendingOrder();
// ArgumentException: Expected pages to be in ascending order, but 5 appears before 3. (Parameter 'pages')
Your own message when you want one. It is always the last parameter, it replaces the default, and
it may use {argument} and {value}:
port.Must().BeBetween(1, 65535, "{argument} must be a usable port, got {value}");
// ArgumentOutOfRangeException: port must be a usable port, got 70000 (Parameter 'port')
environment.Must("This should be prod").NotBe("test").NotBeEmpty();
// one message for every check in the chain; a check's own message still wins
The validated value back, so the guard and the read are one expression:
this.port = config.Port.Must().BeBetween(1, 65535).Value();
Value() unwraps a nullable and fails a null argument exactly as NotBeNull would.
The right exception type, without choosing it:
| Failure | Throws |
|---|
a null argument | ArgumentNullException |
| a comparison, range, sign or NaN check | ArgumentOutOfRangeException |
| everything else — equality, format, containment, type, your own rules | ArgumentException |
Or the exception you name: myOrder.Must().NotBeNull<OrderNullException>() and
Satisfy<Order, OrderQuantityException>(o => o.Quantity >= 5, "...") throw yours.
Frames you can read. The library hides its own frames from the stack trace, so a failure points
at the check you wrote.
What you can check
Every type gets BeNull/NotBeNull, Be/NotBe, BeAnyOf/NotBeAnyOf and Satisfy; then
each adds what makes sense for it. The full list is in
SupportedContracts.md.
Numbers — int, long, short, byte, their unsigned forms, float, double, decimal, and
on .NET 8+ any INumber<T> such as Half, Int128 or BigInteger:
quantity.Must().BePositive().BeLessOrEqualTo(100);
retries.Must().BeBetween(1, 5);
page.Must().BeEven();
ratio.Must().BeFinite(); // neither NaN nor infinity
A comparison never passes on NaN and never passes on null; BeNaN and BeFinite are how you
ask about NaN on purpose.
Text and characters — shape, format and content: