Fluxera.Guards
6.0.22
Prefix Reserved
See the version list below for details.
dotnet add package Fluxera.Guards --version 6.0.22
NuGet\Install-Package Fluxera.Guards -Version 6.0.22
<PackageReference Include="Fluxera.Guards" Version="6.0.22" />
paket add Fluxera.Guards --version 6.0.22
#r "nuget: Fluxera.Guards, 6.0.22"
// Install Fluxera.Guards as a Cake Addin #addin nuget:?package=Fluxera.Guards&version=6.0.22 // Install Fluxera.Guards as a Cake Tool #tool nuget:?package=Fluxera.Guards&version=6.0.22
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 isnull
.
- Throws an
Guard.Against.Default
- Throws an
ArgumentException
if the input isdefault
.
- Throws an
Guard.Against.NullOrEmpty
- Throws an
ArgumentNullException
if the input string isnull
. - Throws an
ArgumentException
if the input string isstring.Empty
. - Throws an
ArgumentNullException
if the input enumerable isnull
. - Throws an
ArgumentException
if the input enumerable is empty. - Throws an
ArgumentNullException
if the input nullable guid isnull
. - Throws an
ArgumentException
if the input nullable guid isGuid.Empty
.
- Throws an
Guard.Against.NullOrWhiteSpace
- Throws an
ArgumentNullException
if the input string isnull
. - Throws an
ArgumentException
if the input string isstring.Empty
or whitespace-only.
- Throws an
Guard.Against.Empty
- Throws an
ArgumentException
if the input guid isGuid.Empty
.
- Throws an
Guard.Against.Negative
- Throws an
ArgumentException
if the input is < 0.
- Throws an
Guard.Against.Zero
- Throws an
ArgumentException
if the input is == 0.
- Throws an
Guard.Against.NegativeOrZero
- Throws an
ArgumentException
if the input is ⇐ 0.
- Throws an
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.
- Throws an
Guard.Against.False
- Throws an
ArgumentException
if the input isfalse
.
- Throws an
Guard.Against.True
- Throws an
ArgumentException
if the input istrue
.
- Throws an
Guard.Against.InvalidInput
- Throws an
ArgumentException
if the input doesn't satisfy a predicate.
- Throws an
Guard.Against.InvalidFormat
- Throws an
ArgumentException
if the input doesn't match a regex pattern.
- Throws an
Product | Versions 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 was computed. 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. |
.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. |
-
.NETStandard 2.1
- JetBrains.Annotations (>= 2022.1.0)
NuGet packages (8)
Showing the top 5 NuGet packages that depend on Fluxera.Guards:
Package | Downloads |
---|---|
Fluxera.Extensions.Hosting.Modules.Configuration
A module that enables configuration. |
|
Fluxera.Repository.Abstractions
The abstractions for the generic repository implementation. |
|
Fluxera.Extensions.Hosting.Modules.AspNetCore
A module that enables ASP.NET Core. |
|
Fluxera.Temporal
A libary that provides temporal types. |
|
Fluxera.Repository.EntityFrameworkCore
An EntityFramework Core repository implementation. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
9.0.0 | 46 | 11/14/2024 | |
8.1.3 | 836 | 11/1/2024 | |
8.1.2 | 545 | 7/9/2024 | |
8.1.1 | 3,114 | 6/2/2024 | |
8.1.0 | 930 | 5/26/2024 | |
8.0.2 | 15,125 | 4/13/2024 | |
8.0.1 | 18,300 | 11/23/2023 | |
8.0.0 | 2,061 | 11/15/2023 | |
7.1.1 | 13,815 | 7/20/2023 | |
7.1.0 | 21,243 | 1/18/2023 | |
7.0.3 | 16,762 | 11/15/2022 | |
7.0.2 | 9,170 | 11/12/2022 | |
7.0.0 | 8,690 | 11/9/2022 | |
6.1.0 | 148,156 | 6/7/2022 | |
6.0.22 | 111,316 | 5/5/2022 | |
6.0.13 | 23,202 | 4/20/2022 | |
6.0.12 | 2,534 | 4/14/2022 | |
6.0.11 | 37,710 | 3/24/2022 | |
6.0.10 | 9,005 | 2/17/2022 | |
6.0.7 | 4,896 | 12/17/2021 | |
6.0.5 | 10,637 | 12/6/2021 |