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 (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 | 208 | 11/24/2024 |
3.1.0 | 239 | 10/15/2024 |
3.0.1 | 1,164 | 12/14/2023 |
3.0.0 | 324 | 12/12/2023 |
2.2.9 | 465 | 8/17/2023 |
2.2.8 | 228 | 8/15/2023 |
2.2.7 | 232 | 8/14/2023 |
2.2.4 | 215 | 8/8/2023 |
2.2.3 | 304 | 8/8/2023 |
2.2.1 | 288 | 8/3/2023 |
2.2.0 | 244 | 8/3/2023 |
2.1.1 | 211 | 7/31/2023 |
2.0.1 | 1,139 | 8/22/2022 |
2.0.0 | 860 | 12/1/2021 |
1.4.0 | 867 | 8/26/2021 |
1.3.0 | 283 | 8/24/2021 |
1.1.0 | 645 | 6/17/2021 |
1.0.1 | 384 | 5/6/2021 |
1.0.0 | 457 | 4/13/2021 |