aqua.tool.Validation 3.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package aqua.tool.Validation --version 3.1.0                
NuGet\Install-Package aqua.tool.Validation -Version 3.1.0                
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="aqua.tool.Validation" Version="3.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add aqua.tool.Validation --version 3.1.0                
#r "nuget: aqua.tool.Validation, 3.1.0"                
#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 aqua.tool.Validation as a Cake Addin
#addin nuget:?package=aqua.tool.Validation&version=3.1.0

// Install aqua.tool.Validation as a Cake Tool
#tool nuget:?package=aqua.tool.Validation&version=3.1.0                

What is it? 🚀

aqua.tool.Validation provides C# source generated extension methods for argument validation.

This package is designed to work for various target frameworks and makes use of PolySharp to integrate with different C# language versions.

How to use

public void SampleMethod(string text)
{
  // Throw an ArgumentNullException if text is null.
  // Throw an ArgumentException if text is empty.
  text.AssertNotNullOrEmpty();
}

public void SampleMethod(IReadOnlyList<string> text)
{
  // Throw an ArgumentNullException if text is null.
  // Throw an ArgumentException if any element in text is null or empty.
  text.AssertItemsNotNullOrEmpty();
}

public void SampleMethod(MyType myValue)
{
  // Throw an ArgumentNullException if myValue is null.
  this.nonNullValue = myValue.CheckNotNull();
}

Assert vs. Check

Most validation methods exists in two flavors. While assert simply verifies the input (e.g. AssertNotNull), check also returns the input value to allow for fluent API style code (e.g. CheckNotNull).

Migrate validation code

Existing code can be migrated using regex find and replace in Visual Studio.

Replace throw new ArgumentNullException()

Replace someArgument ?? throw new ArgumentNullException(nameof(someArgument)) by someArgument.CheckNotNull():

  • Search regex pattern:

    (?<pre>[\s\.\(])(?<field>.+)\s\?\?\sthrow\snew\sArgumentNullException\(((nameof\(\k<field>\))|(\"\k<field>\"))\)
    
  • Replace regex pattern:

    ${pre}${field}.CheckNotNull()
    

Migrate validation code for version 2.2.0 and later

Starting with aqua.tool.Validation v2.2.0 the name argument can be omitted as it's atomatically injected by the compiler using the CallerArgumentExpressionAttribute.

Replace someArgument.CheckNotNull(nameof(someArgument)) by someArgument.CheckNotNull():

  • Search regex pattern:

    \.(?<method>((Assert)|(Check))(Items)?NotNull(OrEmpty)?)\(((nameof\([^\)]+\))|([^\)]+))\)
    
  • Replace regex pattern:

    .${method}()
    

    migrate validation code

Options

Code generation can be configured through some MSBuild properties to set in consuming projects.

Property Value Description
AquaToolValidationDisable true|false Disable compilation of generated source code.
AquaToolValidationPublic true|false Declare generated source code as public. By default, generated source code has internal visibility.
AquaToolValidationNullableDisable true|false Suppress nullable attributes.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on aqua.tool.Validation:

Repository Stars
6bee/Remote.Linq
Simply LINQ your remote resources...
Version Downloads Last updated
3.2.0 49 11/24/2024
3.1.0 220 10/15/2024
3.0.1 1,152 12/14/2023
3.0.0 313 12/12/2023
2.2.9 457 8/17/2023
2.2.8 224 8/15/2023
2.2.7 228 8/14/2023
2.2.4 211 8/8/2023
2.2.3 298 8/8/2023
2.2.1 284 8/3/2023
2.2.0 240 8/3/2023
2.1.1 207 7/31/2023
2.0.1 1,135 8/22/2022
2.0.0 854 12/1/2021
1.4.0 861 8/26/2021
1.3.0 277 8/24/2021
1.1.0 639 6/17/2021
1.0.1 378 5/6/2021
1.0.0 451 4/13/2021