Skip to content

Commit

Permalink
Closes #7. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsolano authored Jun 7, 2021
1 parent 2eb34b0 commit d8669a9
Show file tree
Hide file tree
Showing 29 changed files with 241 additions and 175 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/nuget_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,32 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.202

- name: Clean Last Build
run: dotnet clean --framework net5.0 --configuration Release

- name: Clean old Packages
run: rm src/Validations/bin/Release/Triplex.Validations*.nupkg

- name: Restore dependencies
run: dotnet restore --framework net5.0 --configuration Release
run: dotnet restore

- name: Build
run: dotnet build --no-restore --framework net5.0 --configuration Release
run: dotnet build --framework net5.0 --configuration Release

- name: Test
run: dotnet test --no-build --verbosity normal --framework net5.0 --configuration Release

- name: Package
run: dotnet pack --configuration Release

- name: Move To Release Dir
run: cd src/Validations/bin/Release

- name: Deploy To Nuget.org
run: dotnet nuget push *.nupkg -k ${{secrets.NUGET_DEPLOYMENT_KEY}} -s https://api.nuget.org/v3/index.json
8 changes: 4 additions & 4 deletions src/Validations/Algorithms/Checksum/LuhnFormula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class LuhnFormula
/// <exception cref="System.ArgumentNullException">If <paramref name="fullDigits"/> is <see langword="null"/></exception>
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="fullDigits"/> has less than two(2) digits.</exception>
/// <exception cref="System.FormatException">If <paramref name="fullDigits"/> contains elements not within range [0-9].</exception>
public static bool IsValid([ValidatedNotNull] in int[] fullDigits)
public static bool IsValid([ValidatedNotNull] in int[]? fullDigits)
{
int[] validatedDigits = fullDigits.ValueOrThrowIfNullOrWithLessThanElements(MinimumElements, nameof(fullDigits));

Expand All @@ -43,7 +43,7 @@ public static bool IsValid([ValidatedNotNull] in int[] fullDigits)
/// <exception cref="System.ArgumentNullException">If <paramref name="fullDigits"/> is <see langword="null"/></exception>
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="fullDigits"/> has less than two(2) digits.</exception>
/// <exception cref="System.FormatException">If <paramref name="fullDigits"/> contains characters other than digits.</exception>
public static bool IsValid([ValidatedNotNull] in string fullDigits)
public static bool IsValid([ValidatedNotNull] in string? fullDigits)
{
string notNullDigits = ValidateDigitsAsString(fullDigits);

Expand All @@ -52,7 +52,7 @@ public static bool IsValid([ValidatedNotNull] in string fullDigits)
return DoDigitCheck(validatedDigits);
}

private static string ValidateDigitsAsString(in string fullDigits)
private static string ValidateDigitsAsString([ValidatedNotNull] in string? fullDigits)
{
string notNullDigits = fullDigits.ValueOrThrowIfNullOrZeroLength(nameof(fullDigits));

Expand Down Expand Up @@ -85,7 +85,7 @@ private static bool DoDigitCheck(in int[] sanitizedDigits)
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">If <paramref name="digitsWithoutCheck"/> is <see langword="null"/></exception>
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="digitsWithoutCheck"/> is has less than one digits.</exception>
public static int GetCheckDigit([ValidatedNotNull] in int[] digitsWithoutCheck)
public static int GetCheckDigit([ValidatedNotNull] in int[]? digitsWithoutCheck)
{
const int minimumElements = 1;

Expand Down
46 changes: 23 additions & 23 deletions src/Validations/Arguments.cs

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/Validations/ArgumentsHelpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Linq;
using Triplex.Validations.Exceptions;
using Triplex.Validations.Utilities;

namespace Triplex.Validations.ArgumentsHelpers
{
#pragma warning disable CA1303 // Do not pass literals as localized parameters
internal static class Extensions
{
internal static T ValueOrThrowIfNull<T>(this T value, in string paramName)
internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, in string paramName)
{
if (value is not null)
{
Expand All @@ -17,7 +18,7 @@ internal static T ValueOrThrowIfNull<T>(this T value, in string paramName)
throw new ArgumentNullException(paramName);
}

internal static T ValueOrThrowIfNull<T>(this T value, in string paramName, in string customMessage)
internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, in string paramName, in string customMessage)
{
if (value is not null)
{
Expand Down Expand Up @@ -53,20 +54,20 @@ internal static string ValueOrThrowIfWhiteSpaceOnly(this string value, in string
throw new ArgumentFormatException(paramName: paramName, message: customMessage);
}

internal static string ValueOrThrowIfNullOrZeroLength(this string value, in string paramName)
internal static string ValueOrThrowIfNullOrZeroLength([ValidatedNotNull] this string? value, in string paramName)
=> ValueOrThrowIfNull(value, paramName)
.ValueOrThrowIfZeroLength(paramName);

internal static string ValueOrThrowIfNullOrZeroLength(this string value, in string paramName, in string customMessage)
internal static string ValueOrThrowIfNullOrZeroLength([ValidatedNotNull] this string? value, in string paramName, in string customMessage)
=> ValueOrThrowIfNull(value, paramName, customMessage)
.ValueOrThrowIfZeroLength(paramName, customMessage);

internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(this string value, in string paramName)
internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly([ValidatedNotNull] this string? value, in string paramName)
=> ValueOrThrowIfNull(value, paramName)
.ValueOrThrowIfZeroLength(paramName)
.ValueOrThrowIfWhiteSpaceOnly(paramName);

internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(this string value, in string paramName, in string customMessage)
internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly([ValidatedNotNull] this string? value, in string paramName, in string customMessage)
=> ValueOrThrowIfNull(value, paramName, customMessage)
.ValueOrThrowIfZeroLength(paramName, customMessage)
.ValueOrThrowIfWhiteSpaceOnly(paramName, customMessage);
Expand Down Expand Up @@ -95,10 +96,10 @@ internal static TEnumType ValueOrThrowIfNotDefined<TEnumType>(this TEnumType val
throw new ArgumentOutOfRangeException(paramName, value, finalMessage);
}

internal static TType[] ValueOrThrowIfNullOrWithLessThanElements<TType>(this TType[] value, in int minimumElements, in string paramName) {
internal static TType[] ValueOrThrowIfNullOrWithLessThanElements<TType>([ValidatedNotNull] this TType[]? value, in int minimumElements, in string paramName) {
OutOfRangeChecks.GreaterThanOrEqualTo(ValueOrThrowIfNull(value, paramName).Length, minimumElements, paramName);

return value;
return value!;
}
}
#pragma warning restore CA1303 // Do not pass literals as localized parameters
Expand Down
12 changes: 6 additions & 6 deletions src/Validations/ArgumentsHelpers/NullAndEmptyChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ namespace Triplex.Validations.ArgumentsHelpers
{
internal static class NullAndEmptyChecks
{
internal static TParamType NotNull<TParamType>([ValidatedNotNull] in TParamType value, [ValidatedNotNull] in string paramName) where TParamType : class
internal static TParamType NotNull<TParamType>([ValidatedNotNull] in TParamType? value, [ValidatedNotNull] in string paramName) where TParamType : class
=> value.ValueOrThrowIfNull(paramName.ValueOrThrowIfNull(nameof(paramName)));

internal static TParamType NotNull<TParamType>([ValidatedNotNull] in TParamType value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage) where TParamType : class
internal static TParamType NotNull<TParamType>([ValidatedNotNull] in TParamType? value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage) where TParamType : class
=> value.ValueOrThrowIfNull(paramName.ValueOrThrowIfNull(nameof(paramName)), customMessage.ValueOrThrowIfNull(nameof(customMessage)));

internal static string NotNullEmptyOrWhiteSpaceOnly([ValidatedNotNull] in string value, [ValidatedNotNull] in string paramName)
internal static string NotNullEmptyOrWhiteSpaceOnly([ValidatedNotNull] in string? value, [ValidatedNotNull] in string paramName)
=> NotNullOrEmpty(value, paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)))
.ValueOrThrowIfWhiteSpaceOnly(paramName);

internal static string NotNullEmptyOrWhiteSpaceOnly([ValidatedNotNull] in string value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
internal static string NotNullEmptyOrWhiteSpaceOnly([ValidatedNotNull] in string? value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
=> NotNullOrEmpty(value, paramName, customMessage)
.ValueOrThrowIfWhiteSpaceOnly(paramName, customMessage);

internal static string NotNullOrEmpty([ValidatedNotNull] in string value, [ValidatedNotNull] in string paramName)
internal static string NotNullOrEmpty([ValidatedNotNull] in string? value, [ValidatedNotNull] in string paramName)
=> value.ValueOrThrowIfNullOrZeroLength(
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)));

internal static string NotNullOrEmpty([ValidatedNotNull] in string value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
internal static string NotNullOrEmpty([ValidatedNotNull] in string? value, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
=> value.ValueOrThrowIfNullOrZeroLength(
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)),
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(customMessage)));
Expand Down
30 changes: 15 additions & 15 deletions src/Validations/ArgumentsHelpers/OutOfRangeChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,63 @@ namespace Triplex.Validations.ArgumentsHelpers
{
internal static class OutOfRangeChecks
{
internal static TComparable LessThan<TComparable>([ValidatedNotNull] in TComparable value,
[ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName)
internal static TComparable LessThan<TComparable>([ValidatedNotNull] in TComparable? value,
[ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxExclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));
return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable LessThan<TComparable>([ValidatedNotNull] in TComparable value, [ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName,
internal static TComparable LessThan<TComparable>([ValidatedNotNull] in TComparable? value, [ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName,
[ValidatedNotNull] in string customMessage) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxExclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, customMessage.ValueOrThrowIfNull(nameof(customMessage)));
}

internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable value,
[ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName)
internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable? value,
[ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxInclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable value,
[ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable? value,
[ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName, [ValidatedNotNull] in string customMessage)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxInclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, customMessage.ValueOrThrowIfNull(nameof(customMessage)));
}

internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] in TComparable value, [ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName) where TComparable : IComparable<TComparable>
internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] in TComparable? value, [ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinExclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] in TComparable value, [ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName,
internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] in TComparable? value, [ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName,
[ValidatedNotNull] in string customMessage) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinExclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, customMessage.ValueOrThrowIfNull(nameof(customMessage)));
}

internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable value, [ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName) where TComparable : IComparable<TComparable>
internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable? value, [ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinInclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));

return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable value, [ValidatedNotNull] in TComparable other, [ValidatedNotNull] in string paramName,
internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull] in TComparable? value, [ValidatedNotNull] in TComparable? other, [ValidatedNotNull] in string paramName,
[ValidatedNotNull] in string customMessage) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinInclusiveOnly(SimpleOption.SomeNotNull(other.ValueOrThrowIfNull(nameof(other))));
Expand All @@ -70,9 +70,9 @@ internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull]
}

internal static TComparable Between<TComparable>(
[ValidatedNotNull] in TComparable value,
[ValidatedNotNull] in TComparable fromInclusive,
[ValidatedNotNull] in TComparable toInclusive,
[ValidatedNotNull] in TComparable? value,
[ValidatedNotNull] in TComparable? fromInclusive,
[ValidatedNotNull] in TComparable? toInclusive,
[ValidatedNotNull] in string paramName,
[ValidatedNotNull] in string customMessage) where TComparable : IComparable<TComparable>{
ComparableRange<TComparable> range = new ComparableRange<TComparable>(
Expand All @@ -87,7 +87,7 @@ internal static TComparable Between<TComparable>(
}

private static TComparable CheckBoundaries<TComparable>(
[ValidatedNotNull] in TComparable value,
[ValidatedNotNull] in TComparable? value,
in ComparableRange<TComparable> range,
[ValidatedNotNull] in string paramName,
in string? customMessage) where TComparable : IComparable<TComparable>
Expand Down
9 changes: 6 additions & 3 deletions src/Validations/Validations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>Preconditions;Postconditions;Invariants;DDD;Domain Driven Design</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Version>1.1.0</Version>
<Version>2.0.0</Version>
<Nullable>enable</Nullable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<PackageReleaseNotes>
New Features
- Support custom preconditions using Triplex.Validations.Arguments.CompliesWith(bool, string, string).
Breaking Changes
- Add declare all public parameters doing null-checks as TType? instead of TType to improve developers experience when working with Nullable Context (nullable reference types).

Notes:
- This version does not introduces compiler errors for 1.x usages but it could introduce (or remove) compiler warnings and that can be considered a breaking change for some teams. Tha's why we changed from 1.x to 2.x, but most users won't experience breaking changes in their builds.
</PackageReleaseNotes>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Rejects_Less_Than_One_Elements()
}

[Test]
public void Accepts_One_Or_More_Elements([Values(1, 2, 3, 5, 8, 13)] int length)
public void Accepts_One_Or_More_Elements([Values(1, 2, 3, 5, 8, 13)] in int length)
{
int[] digits = new int[length];

Expand Down Expand Up @@ -57,7 +57,7 @@ public void Accepts_One_Or_More_Elements([Values(1, 2, 3, 5, 8, 13)] int length)
[TestCase("1000000008", 2)]
[TestCase("1000000009", 0)]
[TestCase("1000000010", 8)]
public void Returns_True_For_Valid_Sequences(string rawDigits, int expectedCheckDigit)
public void Returns_True_For_Valid_Sequences(in string rawDigits, in int expectedCheckDigit)
{
int[] digits = rawDigits.Select(ch => int.Parse(ch.ToString())).ToArray();

Expand Down
Loading

0 comments on commit d8669a9

Please sign in to comment.