Fluxera.Guards 8.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Fluxera.Guards --version 8.0.2
NuGet\Install-Package Fluxera.Guards -Version 8.0.2
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="Fluxera.Guards" Version="8.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fluxera.Guards --version 8.0.2
#r "nuget: Fluxera.Guards, 8.0.2"
#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.
// Install Fluxera.Guards as a Cake Addin
#addin nuget:?package=Fluxera.Guards&version=8.0.2

// Install Fluxera.Guards as a Cake Tool
#tool nuget:?package=Fluxera.Guards&version=8.0.2

Build Status

Guards

An extendable guard implementation.

A guard clause helps to check inputs for validity and fails immediately if any invalid inputs are found. The guards are implemented as extension methods on the marker IGuard. This way custom guards can be added to the available default guard clauses.

Usage

Check inputs of methods and constructors and fail early. The guard will fail with an exception if the input is not valid.

public void UpdateCustomer(Customer customer)
{
    Guard.Against.Null(customer, nameof(customer));

    // Update the customer...
}

Check inputs constructors, fail early or assign the value. The guard will fail with an exception if the input is not valid or return the input value if it is valid.

public class Customer
{
    public Customer(string name, decimal credit)
    {
        this.Name = Guard.Against.NullOrWhiteSpace(name, nameof(name));
        this.Credit = Guard.Against.Negative(credit, nameof(credit));
    }

    public string Name { get; }

    public decimal Credit { get; }
}

Create your own guards by adding extension methods on IGuard. To create the exception to throw you can use one of the helpers from the ExceptionHelpers class. This helpers make sure that the correct constructors are used and the parameter name is set correctly.

// ReSharper disable once CheckNamespace
namespace Fluxera.Guards
{
    // Note: Using the namespace 'Fluxera.Guard' will ensure that your
    //       custom guard is available throughout your projects.

    using JetBrains.Annotations;
    using static ExceptionHelpers;

    public static class CustomGuardExtensions
    {
        public static void Hello(this IGuard guard, string input, string parameterName, string? message = null)
        {
            if(input.ToLower() == "hello")
            {
                throw CreateArgumentException(parameterName, message);
            }
        }
    }
}

Available Guards

  • Guard.Against.Null
    • Throws an ArgumentNullException if the input is null.
  • Guard.Against.Default
    • Throws an ArgumentException if the input is default.
  • Guard.Against.NullOrEmpty
    • Throws an ArgumentNullException if the input string is null.
    • Throws an ArgumentException if the input string is string.Empty.
    • Throws an ArgumentNullException if the input enumerable is null.
    • Throws an ArgumentException if the input enumerable is empty.
    • Throws an ArgumentNullException if the input nullable guid is null.
    • Throws an ArgumentException if the input nullable guid is Guid.Empty.
  • Guard.Against.NullOrWhiteSpace
    • Throws an ArgumentNullException if the input string is null.
    • Throws an ArgumentException if the input string is string.Empty or whitespace-only.
  • Guard.Against.Empty
    • Throws an ArgumentException if the input guid is Guid.Empty.
  • Guard.Against.Negative
    • Throws an ArgumentException if the input is < 0.
  • Guard.Against.Zero
    • Throws an ArgumentException if the input is == 0.
  • Guard.Against.NegativeOrZero
    • Throws an ArgumentException if the input is ⇐ 0.
  • Guard.Against.OutOfRange
    • Throws an ArgumentOutOfRangeException if the input is not within a given range.
    • Throws an InvalidEnumArgumentException if the input is not a defined enum value.
  • Guard.Against.False
    • Throws an ArgumentException if the input is false.
  • Guard.Against.True
    • Throws an ArgumentException if the input is true.
  • Guard.Against.InvalidInput
    • Throws an ArgumentException if the input doesn't satisfy a predicate.
  • Guard.Against.InvalidFormat
    • Throws an ArgumentException if the input doesn't match a regex pattern.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (20)

Showing the top 5 NuGet packages that depend on Fluxera.Guards:

Package Downloads
Fluxera.Utilities The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A collection of common utilities and extension methods.

Fluxera.Extensions.DependencyInjection.Abstractions The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

The abstractions for the custom extensions for dependency injection.

Fluxera.Extensions.DependencyInjection The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

The custom extensions for dependency injection.

Fluxera.Extensions.Hosting.Abstractions The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

The abstractions for the modular application host.

Fluxera.Extensions.Hosting The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A modular application host.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.2 9,649 4/13/2024
8.0.1 13,189 11/23/2023
8.0.0 1,257 11/15/2023
7.1.1 8,879 7/20/2023
7.1.0 17,478 1/18/2023
7.0.3 14,562 11/15/2022
7.0.2 8,674 11/12/2022
7.0.0 8,294 11/9/2022
6.1.0 141,138 6/7/2022
6.0.22 107,721 5/5/2022
6.0.13 22,459 4/20/2022
6.0.12 2,429 4/14/2022
6.0.11 36,545 3/24/2022
6.0.10 8,716 2/17/2022
6.0.7 4,565 12/17/2021
6.0.5 9,851 12/6/2021