Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Commit 2bb104e

Browse files
authored
Merge pull request #15 from TechNobre/main
Deploy version 2.3.0
2 parents bd1fe48 + 1d4d648 commit 2bb104e

5 files changed

Lines changed: 131 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33

44

55

6+
## [2.3.0] - 2022-04-05
7+
[Full Changelog](https://github.com/TechNobre/PowerUtils.GuardClauses.Validations/compare/v2.2.1...v2.3.0)
8+
9+
10+
### New Features
11+
- Added Guard `Guard.Validate.IfLengthOutOfRange()`;
12+
13+
14+
15+
616
## [2.2.1] - 2022-04-04
717
[Full Changelog](https://github.com/TechNobre/PowerUtils.GuardClauses.Validations/compare/v2.2.0...v2.2.1)
818

919

10-
### Fixes
20+
### Fixed
1121
- Returned the same type as the input value for `Guard.Validate.IfNull()`;
1222
- Added again the the specific `Guard.Validate.IfNull()` for strings;
1323

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ dotnet add package PowerUtils.GuardClauses.Validations
9595
- `Guard.Validate.IfNullOrWhiteSpace()`;
9696
- `Guard.Validate.IfLongerThan()`;
9797
- `Guard.Validate.IfShorterThan()`;
98-
- `Guard.Validate.IfNotEmail()`;
98+
- `Guard.Validate.IfLengthOutOfRange()`;
9999
- `Guard.Validate.IfLengthEquals()`;
100100
- `Guard.Validate.IfLengthDifferent()`;
101101
- `Guard.Validate.IfEquals()`;
102102
- `Guard.Validate.IfDifferent()`;
103+
- `Guard.Validate.IfNotEmail()`;
103104
- __short, ushort, int, uint, long, ulong, float, double, decimal:__
104105
- `Guard.Validate.IfGreaterThan()`;
105106
- `Guard.Validate.IfLessThan()`;

src/GuardClauses/GuardValidationStringExtensions.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static string IfLongerThan(
144144
/// </summary>
145145
/// <param name="_"></param>
146146
/// <param name="value">Value to validate</param>
147-
/// <param name="minLength">Max length</param>
147+
/// <param name="minLength">Min length</param>
148148
/// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
149149
/// <exception cref="PropertyException">Exception thrown when the length of the value is less than</exception>
150150
[System.Obsolete("This method is deprecated. It will be removed on 2022/09/30. Use the new method 'string.IfShorterThan'")]
@@ -160,7 +160,7 @@ public static string IfLengthLessThan(
160160
/// </summary>
161161
/// <param name="_"></param>
162162
/// <param name="value">Value to validate</param>
163-
/// <param name="minLength">Max length</param>
163+
/// <param name="minLength">Min length</param>
164164
/// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
165165
/// <exception cref="PropertyException">Exception thrown when the value is shorter than</exception>
166166
public static string IfShorterThan(
@@ -363,5 +363,40 @@ public static string IfDifferent(
363363

364364
return value;
365365
}
366+
367+
/// <summary>
368+
/// Throws an <see cref="PropertyException" /> if <paramref name="value"/> has a length out of range. Error code 'MIN:{X}' or 'MAX:{X}'
369+
/// </summary>
370+
/// <param name="_"></param>
371+
/// <param name="value">Value to validate</param>
372+
/// <param name="minLength">Min length</param>
373+
/// <param name="maxLength">Max length</param>
374+
/// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
375+
/// <exception cref="PropertyException">Exception thrown when the length of the value is out of range</exception>
376+
public static string IfLengthOutOfRange(
377+
this IGuardValidationClause _,
378+
string value,
379+
int minLength,
380+
int maxLength,
381+
[CallerArgumentExpression("value")] string parameterName = null
382+
)
383+
{
384+
if(value == null)
385+
{
386+
return value;
387+
}
388+
389+
if(value.Length < minLength)
390+
{
391+
throw new PropertyException(parameterName, ErrorCodes.GetMinFormatted(minLength));
392+
}
393+
394+
if(value.Length > maxLength)
395+
{
396+
throw new PropertyException(parameterName, ErrorCodes.GetMaxFormatted(maxLength));
397+
}
398+
399+
return value;
400+
}
366401
}
367402
}

src/PowerUtils.GuardClauses.Validations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageId>PowerUtils.GuardClauses.Validations</PackageId>
1515
<title>PowerUtils.GuardClauses.Validations</title>
1616
<Product>PowerUtils.GuardClauses.Validations</Product>
17-
<Version>2.2.1</Version>
17+
<Version>2.3.0</Version>
1818

1919
<Authors>Nelson Nobre</Authors>
2020
<Company>TechNobre</Company>

tests/PowerUtils.GuardClauses.Validations.Tests/GuardClausesTests/GuardValidationStringExtensionsTests.cs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,11 @@ public void IfNotEmail_WithSpace_Exception()
979979

980980

981981
// Assert
982-
act.Validate<PropertyException>(HttpStatusCode.BadRequest, nameof(clientEmail), "INVALID");
982+
act.Validate<PropertyException>(
983+
HttpStatusCode.BadRequest,
984+
nameof(clientEmail),
985+
"INVALID"
986+
);
983987
}
984988

985989
[Fact]
@@ -994,7 +998,11 @@ public void IfNotEmail_FakeText_Exception()
994998

995999

9961000
// Assert
997-
act.Validate<PropertyException>(HttpStatusCode.BadRequest, nameof(clientEmail), "INVALID");
1001+
act.Validate<PropertyException>(
1002+
HttpStatusCode.BadRequest,
1003+
nameof(clientEmail),
1004+
"INVALID"
1005+
);
9981006
}
9991007

10001008
[Fact]
@@ -1012,4 +1020,74 @@ public void IfNotEmail_Email_Valid()
10121020
act.Should()
10131021
.Be(clientEmail);
10141022
}
1023+
1024+
[Fact]
1025+
public void IfLengthOutOfRange_Null_Valid()
1026+
{
1027+
// Arrange
1028+
string name = null;
1029+
1030+
1031+
// Act
1032+
var act = Record.Exception(() => Guard.Validate.IfLengthOutOfRange(name, 4, 7));
1033+
1034+
1035+
// Assert
1036+
act.Should()
1037+
.Be(name);
1038+
}
1039+
1040+
[Fact]
1041+
public void IfLengthOutOfRange_Short_Exception()
1042+
{
1043+
// Arrange
1044+
var name = "ola";
1045+
1046+
1047+
// Act
1048+
var act = Record.Exception(() => Guard.Validate.IfLengthOutOfRange(name, 4, 7));
1049+
1050+
1051+
// Assert
1052+
act.Validate<PropertyException>(
1053+
HttpStatusCode.BadRequest,
1054+
nameof(name),
1055+
ErrorCodes.GetMinFormatted(4)
1056+
);
1057+
}
1058+
1059+
[Fact]
1060+
public void IfLengthOutOfRange_Longer_Exception()
1061+
{
1062+
// Arrange
1063+
var name = "Vel ut gubergren est ut sed blandit ipsum";
1064+
1065+
1066+
// Act
1067+
var act = Record.Exception(() => Guard.Validate.IfLengthOutOfRange(name, 4, 7));
1068+
1069+
1070+
// Assert
1071+
act.Validate<PropertyException>(
1072+
HttpStatusCode.BadRequest,
1073+
nameof(name),
1074+
ErrorCodes.GetMaxFormatted(7)
1075+
);
1076+
}
1077+
1078+
[Fact]
1079+
public void IfLengthOutOfRange_Valid_SameValue()
1080+
{
1081+
// Arrange
1082+
var name = "Power";
1083+
1084+
1085+
// Act
1086+
var act = Guard.Validate.IfLengthOutOfRange(name, 4, 7);
1087+
1088+
1089+
// Assert
1090+
act.Should()
1091+
.Be(name);
1092+
}
10151093
}

0 commit comments

Comments
 (0)