A high-performance, extensible argument validation library.
A high-performance, extensible argument validation library.
This project is no longer actively maintained.
I apologise to everyone who depended on Guard for my three-year-long silence. Guard was my passion project for the longest time, helping me get better at C#, automated builds, Visual Studio extensibility, documentation, and how to be part of a community regardless of how small we've been. It also kickstarted my journey of creating terrible gifs.
But my life has changed - I moved to a different country, changed jobs, lost someone very dear to me, and started spending less and less time coding outside my work hours. I always believed I'd be back and release v2 with all my bright ideas, but I just didn't know when. I only noticed today that I've been avoiding my GitHub notifications out of guilt for over a year, and I figured it was time for closure.
I can no longer find the time to maintain an open-source project, as I want to spend most of my free time with my family and on other hobbies.
Guard has always been in an awkward position:
Library authors avoid adding unnecessary dependencies to their packages, and Guard didn't bring enough value to be worth being a runtime dependency. I wanted to experiment with IL weaving and source generators to make Guard a compile-time dependency that would replace call sites with simple if-then-throw statements, but I never got the chance.
App developers didn't need Guard, as their validation scenarios don't involve throwing argument exceptions. For example, a web API would use something like DataAnnotation attributes or FluentValidation to validate its requests. So as a library that specifically targeted library authors, Guard has mostly been irrelevant to app developers.
New style with using static and [CallerArgumentExpression]:
// using static Dawn.Validation.Argument;
Require(firstName).NotNullOrWhiteSpace();Light validations with [CallerArgumentExpression]:
…c# public Person(string name, int age) { if (name == null) throw new ArgumentNullException(nameof(name), "Name cannot be null.");
if (name.Length == 0)
throw new ArgumentException("Name cannot be empty.", nameof(name));
if (age 7.2` or later added in your .csproj file..NET Standard 1.0 and above are supported. Microsoft Docs lists the following platform versions as .NET Standard 1.0 compliant but keep in mind that currently, the unit tests are only targeting .NET Core 3.0.
| Platform | Version |
|---|---|
| .NET Core | 1.0 |
| .NET Framework | 4.5 |
| Mono | 4.6 |
| Xamarin.iOS | 10.0 |
| Xamarin.Mac | 3.0 |
| Xamarin.Android | 7.0 |
| Universal Windows Platform | 10.0 |
| Windows | 8.0 |
| Windows Phone | 8.1 |
| Windows Phone Silverlight | 8.0 |
| Unity | 2018.1 |
The default branch (dev) is the development branch, so it may contain changes/features that are not published to NuGet yet. See the master branch for the latest published version.
Click here for a list of the validations that are included in the library.
Click here for the document that explains the motives behind the Guard's API design and more advanced features.
Click here to see how to add custom validations to Guard by writing simple extension methods.
Code snippets can be found in the snippets folder. Currently, only the Visual Studio is supported.
No open issues yet, or sync has not completed.