Dybal.Utils.Guards 1.0.1

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

Dybal.Utils.Guards

The primary purpose of Dybal.Utils.Guards is to simplify writing input data checks to the object.

Make your code readable as much as you can

Foo(Guid id, string bar)
{
    if(id == Guid.Empty)
    {
        throw new ArgumentException("Value cannot be an empty GUID.", nameof(id));
    }

    if(string.NotNullOrWhiteSpace(bar))
    {
        throw new ArgumentException("Value cannot be null or white space string.", nameof(bar));
    }
    if (bar < 5)
    {
        throw new ArgumentException($"The length of '{nameof(bar)}' must be 5 characters or more. Parameter {bar.Length} has characters.", nameof(bar));
    }
    if (bar > 20)
    {
        throw new ArgumentException($"The length of '{nameof(bar)}' must be 20 characters or fewer. Parameter {bar.Length} has characters.", nameof(bar));
    }

    Id = id;
    Bar = bar;
}

Less boilerplate and more clarity to your code

Foo(Guid id, string bar)
{
    Id = Guard.Argument(id).NotDefault();
    Bar = Guard.Argument(bar).NotNullOrWhiteSpace().MinLength(5).MaxLength(20);
}

If you like shortcodes, you can also use this.

using static Dybal.Utils.Guards.GuardProvider;

...

Foo(Guid? id, string bar)
{
    Id = Guard(id).NotDefault();
    Bar = Guard(bar).NotNullOrWhiteSpace().MinLength(5).MaxLength(20);
}

Or an Extension method from Dybal.Utils.Guards.ObjectExtensions package.

Foo(Guid? id, string bar)
{
    Id = id.Guard().NotDefault();
    Bar = bar.Guard().NotNullOrWhiteSpace().MinLength(5).MaxLength(20);
}

Documentation

Documentation written in C# may be harder to read, but it never lies and it's always up to date.

https://github.com/martindybal/Dybal.Utils/tree/main/src/Utils/Dybal.Utils.Guards

Many examples of use can be found in the tests

https://github.com/martindybal/Dybal.Utils/tree/main/src/Tests/Tests.Dybal.Utils.Guards

Extensible

As you can see, I primarily use extension methods. So if you're missing a Guard, it's easy to write one. If you think it's generic enough, send a PR. I'm in!

public static class CustomGuardExtensions
{
    public static ArgumentGuard<int> IsNotFive(this ArgumentGuard<int> guard)
    {
        if (guard.Argument.Value == 5)
        {
            ThrowHelper.Throw<ArgumentException>(guard, "Value cannot be five.");
        }

        return guard;
    }
}
Product Compatible and additional computed target framework versions.
.NET 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 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.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dybal.Utils.Guards:

Package Downloads
Dybal.Utils.Guards.ObjectExtensions

Extension method to facilitate the creation of a Guard from a variable.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 13,193 11/28/2023
1.0.0 310 11/15/2023
1.0.0-pre-07 465 7/12/2023
1.0.0-pre-06 225 7/5/2023
1.0.0-pre-05 596 11/24/2022
1.0.0-pre-04 312 11/19/2022
1.0.0-pre-03 288 11/3/2022
1.0.0-pre-02 269 11/2/2022
1.0.0-pre-01 266 11/1/2022