Skip to content

Commit

Permalink
#12 #16 Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsolano committed Apr 1, 2022
1 parent 8c8f242 commit 8ef399c
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 147 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Validation library inspired by the concepts of ***Secure by Design***, by Dan Be

## Project's Code Health ##
### Overall ###
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=lsolano_triplex&metric=alert_status&branch=master)](https://sonarcloud.io/dashboard?id=lsolano_triplex)
[![Quality Gate](https://sonarcloud.io/api/project_badges/quality_gate?project=lsolano_triplex&branch=master)](https://sonarcloud.io/dashboard?id=lsolano_triplex)

### Ratings ###
[![SQALE Rating](https://sonarcloud.io/api/project_badges/measure?project=lsolano_triplex&metric=sqale_rating&branch=master)](https://sonarcloud.io/dashboard?id=lsolano_triplex) [![SQALE Index](https://sonarcloud.io/api/project_badges/measure?project=lsolano_triplex&metric=sqale_index&branch=master)](https://sonarcloud.io/dashboard?id=lsolano_triplex) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=lsolano_triplex&metric=reliability_rating&branch=master)](https://sonarcloud.io/dashboard?id=lsolano_triplex)
Expand Down
14 changes: 6 additions & 8 deletions src/Validations/Algorithms/Checksum/LuhnFormula.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Triplex.Validations.ArgumentsHelpers;
using Triplex.Validations.Utilities;

namespace Triplex.Validations.Algorithms.Checksum;
namespace Triplex.Validations.Algorithms.Checksum;

/// <summary>
/// Implementation of the Luhn algorithm in its two variants: as validation and as checksum generator.
Expand All @@ -25,7 +22,7 @@ public static class LuhnFormula
/// <exception cref="FormatException">
/// If <paramref name="fullDigits"/> contains elements not within range [0-9].
/// </exception>
public static bool IsValid([ValidatedNotNull] int[]? fullDigits)
public static bool IsValid([NotNull, ValidatedNotNull] int[]? fullDigits)
{
int[] validatedDigits = fullDigits.ValueOrThrowIfNullOrWithLessThanElements(MinimumElements,
nameof(fullDigits));
Expand Down Expand Up @@ -54,7 +51,7 @@ public static bool IsValid([ValidatedNotNull] int[]? fullDigits)
/// <exception cref="FormatException">
/// If <paramref name="fullDigits"/> contains characters other than digits.
/// </exception>
public static bool IsValid([ValidatedNotNull] string? fullDigits)
public static bool IsValid([NotNull, ValidatedNotNull] string? fullDigits)
{
string notNullDigits = ValidateDigitsAsString(fullDigits);

Expand All @@ -63,7 +60,8 @@ public static bool IsValid([ValidatedNotNull] string? fullDigits)
return DoDigitCheck(validatedDigits);
}

private static string ValidateDigitsAsString([ValidatedNotNull] string? fullDigits)
[return: NotNull]
private static string ValidateDigitsAsString([NotNull, ValidatedNotNull] string? fullDigits)
{
string notNullDigits = fullDigits.ValueOrThrowIfNullOrZeroLength(nameof(fullDigits));

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

Expand Down
162 changes: 92 additions & 70 deletions src/Validations/Arguments.cs

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions src/Validations/ArgumentsHelpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Triplex.Validations.Exceptions;
using Triplex.Validations.Utilities;

namespace Triplex.Validations.ArgumentsHelpers;
namespace Triplex.Validations.ArgumentsHelpers;

#pragma warning disable CA1303 // Do not pass literals as localized parameters
internal static class Extensions
{
internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, string paramName)
[return: NotNull]
internal static T ValueOrThrowIfNull<T>([NotNull, ValidatedNotNull] this T? value, string paramName)
{
if (value is not null)
{
Expand All @@ -16,7 +14,8 @@ internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, string
throw new ArgumentNullException(paramName);
}

internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, string paramName,
[return: NotNull]
internal static T ValueOrThrowIfNull<T>([NotNull, ValidatedNotNull] this T? value, string paramName,
string customMessage)
{
if (value is not null)
Expand All @@ -27,9 +26,11 @@ internal static T ValueOrThrowIfNull<T>([ValidatedNotNull] this T? value, string
throw new ArgumentNullException(paramName, customMessage);
}

[return: NotNull]
internal static string ValueOrThrowIfZeroLength(this string value, string paramName)
=> ValueOrThrowIfZeroLength(value, paramName, "Can not be empty (zero length).");

[return: NotNull]
internal static string ValueOrThrowIfZeroLength(this string value, string paramName, string customMessage)
{
if (value.Length is not 0)
Expand All @@ -40,9 +41,11 @@ internal static string ValueOrThrowIfZeroLength(this string value, string paramN
throw new ArgumentOutOfRangeException(paramName, value.Length, customMessage);
}

[return: NotNull]
internal static string ValueOrThrowIfWhiteSpaceOnly(this string value, string paramName)
=> ValueOrThrowIfWhiteSpaceOnly(value, paramName, "Can not be white-space only.");

[return: NotNull]
internal static string ValueOrThrowIfWhiteSpaceOnly(this string value, string paramName, string customMessage)
{
if (value.Any(ch => ch.IsNotWhiteSpace()))
Expand All @@ -53,22 +56,27 @@ internal static string ValueOrThrowIfWhiteSpaceOnly(this string value, string pa
throw new ArgumentFormatException(paramName: paramName, message: customMessage);
}

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

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

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

internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly([ValidatedNotNull] this string? value,
[return: NotNull]
internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly([NotNull, ValidatedNotNull] this string? value,
string paramName, string customMessage)
=> ValueOrThrowIfNull(value, paramName, customMessage)
.ValueOrThrowIfZeroLength(paramName, customMessage)
Expand Down Expand Up @@ -100,8 +108,9 @@ internal static TEnumType ValueOrThrowIfNotDefined<TEnumType>(this TEnumType val
throw new ArgumentOutOfRangeException(paramName, value, finalMessage);
}

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

Expand Down
36 changes: 21 additions & 15 deletions src/Validations/ArgumentsHelpers/NullAndEmptyChecks.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using Triplex.Validations.Utilities;

namespace Triplex.Validations.ArgumentsHelpers;
namespace Triplex.Validations.ArgumentsHelpers;

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

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

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

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

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

internal static string NotNullOrEmpty([ValidatedNotNull] string? value, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage)
[return: NotNull]
internal static string NotNullOrEmpty([NotNull, ValidatedNotNull] string? value,
[NotNull, ValidatedNotNull] string paramName, [NotNull, ValidatedNotNull] string customMessage)
=> value.ValueOrThrowIfNullOrZeroLength(
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)),
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(customMessage)));
Expand Down
68 changes: 38 additions & 30 deletions src/Validations/ArgumentsHelpers/OutOfRangeChecks.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
using Triplex.Validations.Utilities;

namespace Triplex.Validations.ArgumentsHelpers;
namespace Triplex.Validations.ArgumentsHelpers;

internal static class OutOfRangeChecks
{
internal static TComparable LessThan<TComparable>([ValidatedNotNull] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName)
[return: NotNull]
internal static TComparable LessThan<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] 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] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage) where TComparable : IComparable<TComparable>
[return: NotNull]
internal static TComparable LessThan<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName,
[NotNull, ValidatedNotNull] 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] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName)
[return: NotNull]
internal static TComparable LessThanOrEqualTo<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxInclusiveOnly(
Expand All @@ -33,9 +34,10 @@ internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] TC
return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage)
[return: NotNull]
internal static TComparable LessThanOrEqualTo<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName,
[NotNull, ValidatedNotNull] string customMessage)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMaxInclusiveOnly(
Expand All @@ -44,8 +46,9 @@ internal static TComparable LessThanOrEqualTo<TComparable>([ValidatedNotNull] TC
return CheckBoundaries(value, range, paramName, customMessage.ValueOrThrowIfNull(nameof(customMessage)));
}

internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName)
[return: NotNull]
internal static TComparable GreaterThan<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinExclusiveOnly(
Expand All @@ -54,18 +57,20 @@ internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] TCompara
return CheckBoundaries(value, range, paramName, null);
}

internal static TComparable GreaterThan<TComparable>([ValidatedNotNull] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage) where TComparable : IComparable<TComparable>
[return: NotNull]
internal static TComparable GreaterThan<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName,
[NotNull, ValidatedNotNull] 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] TComparable? value,
[ValidatedNotNull] TComparable? other, [ValidatedNotNull] string paramName)
[return: NotNull]
internal static TComparable GreaterThanOrEqualTo<TComparable>([NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? other, [NotNull, ValidatedNotNull] string paramName)
where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = ComparableRangeFactory.WithMinInclusiveOnly(
Expand All @@ -74,22 +79,24 @@ internal static TComparable GreaterThanOrEqualTo<TComparable>([ValidatedNotNull]
return CheckBoundaries(value, range, paramName, null);
}

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

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

[return: NotNull]
internal static TComparable Between<TComparable>(
[ValidatedNotNull] TComparable? value,
[ValidatedNotNull] TComparable? fromInclusive,
[ValidatedNotNull] TComparable? toInclusive,
[ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage) where TComparable : IComparable<TComparable>
[NotNull, ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? fromInclusive,
[NotNull, ValidatedNotNull] TComparable? toInclusive,
[NotNull, ValidatedNotNull] string paramName,
[NotNull, ValidatedNotNull] string customMessage) where TComparable : IComparable<TComparable>
{
ComparableRange<TComparable> range = new(
SimpleOption.SomeNotNull(fromInclusive.ValueOrThrowIfNull(nameof(fromInclusive))),
Expand All @@ -102,10 +109,11 @@ internal static TComparable Between<TComparable>(

}

[return: NotNull]
private static TComparable CheckBoundaries<TComparable>(
[ValidatedNotNull] TComparable? value,
[NotNull, ValidatedNotNull] TComparable? value,
ComparableRange<TComparable> range,
[ValidatedNotNull] string paramName,
[NotNull, ValidatedNotNull] string paramName,
string? customMessage) where TComparable : IComparable<TComparable>
{
return range.IsWithin(value.ValueOrThrowIfNull(nameof(value)), paramName.ValueOrThrowIfNull(nameof(paramName)),
Expand Down
4 changes: 4 additions & 0 deletions src/Validations/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
global using System;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.Linq;
global using System.Text.RegularExpressions;
global using Triplex.Validations.ArgumentsHelpers;
global using Triplex.Validations.Exceptions;
global using Triplex.Validations.Utilities;
2 changes: 1 addition & 1 deletion src/Validations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: System.CLSCompliant(true)]
[assembly: CLSCompliant(true)]
[assembly: System.Resources.NeutralResourcesLanguage("en")]
[assembly: System.Runtime.InteropServices.ComVisible(false)]

Expand Down
Loading

0 comments on commit 8ef399c

Please sign in to comment.