PineGuard.GuardClauses 0.1.0-alpha.7

This is a prerelease version of PineGuard.GuardClauses.
dotnet add package PineGuard.GuardClauses --version 0.1.0-alpha.7
                    
NuGet\Install-Package PineGuard.GuardClauses -Version 0.1.0-alpha.7
                    
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="PineGuard.GuardClauses" Version="0.1.0-alpha.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PineGuard.GuardClauses" Version="0.1.0-alpha.7" />
                    
Directory.Packages.props
<PackageReference Include="PineGuard.GuardClauses" />
                    
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 PineGuard.GuardClauses --version 0.1.0-alpha.7
                    
#r "nuget: PineGuard.GuardClauses, 0.1.0-alpha.7"
                    
#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 PineGuard.GuardClauses@0.1.0-alpha.7
                    
#: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=PineGuard.GuardClauses&version=0.1.0-alpha.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=PineGuard.GuardClauses&version=0.1.0-alpha.7&prerelease
                    
Install as a Cake Tool

PineGuard.GuardClauses

Stop bad input at the door. Keep the parsed value. Throw your exceptions, not ArgumentException.

Guard.Against.NotHttpsUrl(url) takes a string and hands back a Uri. If the input wasn't a valid HTTPS URL, it throws — if it was, you get the parsed value with no second new Uri(...) call. Every guard in PineGuard works this way: bad input stops at the boundary, and the parsed, typed value flows on.

Guards fit anywhere invalid input should stop execution immediately — constructors, factory methods, service methods, API layer boundaries, and parameter checks on any public method. They're a perfect fit for Domain-Driven Design, where the always-valid-state principle means entities, value objects, and aggregates can't exist in an invalid state. Guards enforce that invariant right in the constructor, so bad data never crosses into your domain.

Where PineGuard goes further than any other guard library: your exception, not ArgumentException. Replace the default globally, per-scope, or per-call — so new Order(...) throws InvalidOrderException and new Checkout(...) throws CheckoutException. Other libraries stop at a per-call override; PineGuard's scoped and global policies let your whole domain speak one exception vocabulary without each call site opting in.

Install

dotnet add package PineGuard.GuardClauses

Targets net8.0, net10.0, and netstandard2.1. Depends on PineGuard.Core and PineGuard.MustClauses.

Examples

using PineGuard.GuardClauses;

// The four canonical rules — each throws on failure, returns the parsed value on success
string email       = Guard.Against.NotEmail(userEmail);
string strictEmail = Guard.Against.NotStrictEmail(userEmail);
string safe        = Guard.Against.OwaspUnsafe(userInput);
Uri    callbackUri = Guard.Against.NotHttpsUrl(callback);  // returns Uri, not string

// Compose at a service boundary
public EndpointConfiguration Create(string callback, string userEmail, string userInput)
{
    return new EndpointConfiguration(
        Guard.Against.NotHttpsUrl(callback),
        Guard.Against.NotEmail(userEmail),
        Guard.Against.OwaspUnsafe(userInput));
}

Your domain, your exception story

// Replace globally
GuardExceptionPolicy.ExceptionReplacer = ex => new DomainValidationException(ex.Message, ex);
GuardExceptionPolicy.ReplaceDefaultExceptions = true;

// Or just for a block
using (GuardExceptionPolicy.BeginScope(o =>
    o.ExceptionReplacer = ex => new CheckoutException(ex.Message, ex)))
{
    Guard.Against.NotNull(orderId);
}

// Or just for one call
Guard.Against.NotNull(orderId,
    exceptionCreator: () => new CheckoutException("Order id is required."));

Other layers, same rule library

See the full documentation for the complete Guard.Against.* catalog and exception-policy options.

License

MIT © Steve McCormack

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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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
0.1.0-alpha.7 193 4/20/2026
0.1.0-alpha.6 66 4/20/2026
0.1.0-alpha.5 65 4/18/2026