https://github.com/fluentcontracts/fluentcontracts
Fluent argument validation for fluent software development.
https://github.com/fluentcontracts/fluentcontracts
arguments contracts fluent-api fluent-interface validation
Last synced: 29 days ago
JSON representation
Fluent argument validation for fluent software development.
- Host: GitHub
- URL: https://github.com/fluentcontracts/fluentcontracts
- Owner: FluentContracts
- License: mit
- Created: 2018-06-05T09:28:08.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2025-03-17T00:56:39.000Z (11 months ago)
- Last Synced: 2025-11-07T16:07:12.539Z (3 months ago)
- Topics: arguments, contracts, fluent-api, fluent-interface, validation
- Language: C#
- Homepage: https://fluentcontracts.github.io
- Size: 407 KB
- Stars: 40
- Watchers: 1
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
- Support: docs/SupportedContracts.md
Awesome Lists containing this project
README

# FluentContracts
[](https://www.nuget.org/packages/FluentContracts/)
[](https://www.nuget.org/packages/FluentContracts/)
API for defining argument validation contracts in a fluent manner.
Inspired by [FluentAssertions](https://github.com/fluentassertions/fluentassertions)
## Why another validation library
I am perfectly aware of the other libraries out there, that are doing the same stuff.
Libraries like [FluentValidation](https://github.com/FluentValidation/FluentValidation) and `Guard` from [.NET Community Toolkit](https://github.com/CommunityToolkit/dotnet) are awesome
and have tons of functionality, support and experience. If you like those and use them already, that is fine.
I did this one, because I don't really like how the other libraries require you to write, in order to achieve the validation.
It is pretty verbose for my taste. I wanted to make something more simple and "human-readable", the same way [FluentAssertions](https://github.com/fluentassertions/fluentassertions) does it for unit testing.
## Usage
The usage of the contracts is pretty simple. You can use the extension methods everywhere you want to do a validation of some variable.
Generally when we write methods, constructors, etc., we do validation like that:
```csharp
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` your code will look like this:
```csharp
public void AddOrder(Order myOrder)
{
myOrder
.Must()
.NotBeNull()
.And
.Satisfy(
o => o.Quantity >= 5,
"Order quantity cannot be less than 5");
...
}
```
or as simple as:
```csharp
public int Divide(int a, int b)
{
b.Must().NotBe(0);
return a / b;
}
```
### User defined exceptions
You can also throw your own exception like that:
```csharp
public void AddOrder(Order myOrder)
{
myOrder.Must().NotBeNull();
}
```
This will throw an instance of `OrderNullException` if `myOrder` is `null`.
## Supported contracts
You can find them [HERE](docs/SupportedContracts.md).
## Help needed 🙏
My goal for this project is to become as exhaustive, safe and stable as possible, so people can use it in production and on big projects.
So I need some help. If you are interested in helping out just send a pull request, open an issue, etc.
## Repository 🚧
### Builds
| Type | Status |
|:-------------:|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Dev Build | [](https://github.com/FluentContracts/FluentContracts/actions) |
| Dev Build | [](https://github.com/FluentContracts/FluentContracts/actions) |
| Dev Build | [](https://github.com/FluentContracts/FluentContracts/actions) |
| Code Coverage | [](https://coveralls.io/github/FluentContracts/FluentContracts) |
| Release | [](https://github.com/FluentContracts/FluentContracts/actions) |
### Status

### How to build locally
- Clone repos
- Run `build.cmd`
## Where to find me 🕵️
[](https://todorov.bg)
[](https://twitter.com/totollygeek)
[](https://www.linkedin.com/in/totollygeek)
[](https://infosec.exchange/@totollygeek)
[](https://www.threads.net/@totollygeek)
[](https://bsky.app/profile/totollygeek.com)
[](https://linktr.ee/totollygeek)
[](mailto://fluentcontracts@pm.me)
## Special thanks 🙇♂️
#### [Matthias Koch](https://twitter.com/matkoch87)
> The creator of [NUKE](https://nuke.build), because I cannot build any .NET project without it and because he helped me tremendously in setting up the repository and everything around this project. (_I have also copy-pasted, like his entire build and some markdown files_ 🤫)
#### [Dennis Doomen](https://twitter.com/ddoomen)
> The "[FluentAssertions](https://fluentassertions.com/)" guy. This whole project was inspired by how that library works and I might have copy-pasted also parts of his repo too 😏
## Technology Sponsors 💻

> Special thanks to [JetBrains](https://www.jetbrains.com/) for supplying a free license for [Rider](https://www.jetbrains.com/rider/), which is my primary IDE of choice for this project!
Icon made by [IconMonk](https://www.flaticon.com/authors/icon-monk) from [Flaticon](https://www.flaticon.com)