Dybal.Utils.Guards 1.0.0-pre-05

This is a prerelease version of Dybal.Utils.Guards.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Dybal.Utils.Guards --version 1.0.0-pre-05                
NuGet\Install-Package Dybal.Utils.Guards -Version 1.0.0-pre-05                
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.0-pre-05" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dybal.Utils.Guards --version 1.0.0-pre-05                
#r "nuget: Dybal.Utils.Guards, 1.0.0-pre-05"                
#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 Dybal.Utils.Guards as a Cake Addin
#addin nuget:?package=Dybal.Utils.Guards&version=1.0.0-pre-05&prerelease

// Install Dybal.Utils.Guards as a Cake Tool
#tool nuget:?package=Dybal.Utils.Guards&version=1.0.0-pre-05&prerelease                

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 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 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. 
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 4,630 11/28/2023
1.0.0 172 11/15/2023
1.0.0-pre-07 373 7/12/2023
1.0.0-pre-06 125 7/5/2023
1.0.0-pre-05 472 11/24/2022
1.0.0-pre-04 178 11/19/2022
1.0.0-pre-03 162 11/3/2022
1.0.0-pre-02 136 11/2/2022
1.0.0-pre-01 144 11/1/2022