aqua.tool.Validation
3.2.0
dotnet add package aqua.tool.Validation --version 3.2.0
NuGet\Install-Package aqua.tool.Validation -Version 3.2.0
<PackageReference Include="aqua.tool.Validation" Version="3.2.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add aqua.tool.Validation --version 3.2.0
#r "nuget: aqua.tool.Validation, 3.2.0"
// Install aqua.tool.Validation as a Cake Addin #addin nuget:?package=aqua.tool.Validation&version=3.2.0 // Install aqua.tool.Validation as a Cake Tool #tool nuget:?package=aqua.tool.Validation&version=3.2.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}()
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. |
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- PolySharp (>= 1.15.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.2.0 | 64 | 11/24/2024 |
3.1.0 | 222 | 10/15/2024 |
3.0.1 | 1,152 | 12/14/2023 |
3.0.0 | 315 | 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 |