Skip to content

deadtoadroad/arg-ensure

Repository files navigation

ArgEnsure

Easy to write, easy to read guard clauses for your .NET methods. Available as a NuGet package.

Common Usage

Guard against nulls.

Ensure.Arg(nameof(myArg), myArg).IsNotNull();

Guard against empty strings or whitespace.

Ensure.Arg(nameof(myArg), myArg).IsNotEmpty();
Ensure.Arg(nameof(myOtherArg), myOtherArg).IsNotWhitespace();

Guard against anything.

// Ensure myArg contains a number.
Ensure.Arg(nameof(myArg), myArg).Is(a => a.Any(char.IsDigit));

Chain any of them together.

Ensure.Arg(nameof(myArg), myArg).IsNotNull().And.IsNotWhitespace();

Extensions

Creating your own guard clauses is easy to do.

public static class ArgExtensions
{
    public static Andable<Arg<int>> IsGreaterThanZero(this Arg<int> arg)
    {
        Ensure.Arg(nameof(arg), arg).IsNotNull();

        return Ensure.Arg(arg.Name, arg.Value).Is(a => a > 0);
    }
}

Using them is too.

Ensure.Arg(nameof(myArg), myArg).IsGreaterThanZero();

Exceptions

ArgEnsure contains methods to throw any of the three built-in argument exceptions in .NET. The ability to throw custom exceptions is coming soon.

Exception Methods Extension Methods
System.ArgumentNullException IsNotNull()
System.ArgumentException Is() IsNot()
IsNotEmpty()
IsNotWhitespace()
System.ArgumentOutOfRangeException IsInRange() IsNotInRange()

Building

Building the source from scratch can be done a number of ways.

Docker

The easiest method is by using Docker. If you have Bash, Git and Docker installed you can use the included scripts to kick off a build immediately. No other tools are required.

cd build
./build.sh restore
./build.sh test
./build.sh clean # or
./build.sh clean-repo

Build Tools

If you have sufficient build tools installed, the standard dotnet CLI commands will also work.

Ubuntu 18.04

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published