Skip to content

Commit

Permalink
Merge pull request andrebaltieri#99 from gunn3r71/main
Browse files Browse the repository at this point in the history
Adding method to validate if the length of a string is within the defined range
  • Loading branch information
andrebaltieri authored Jul 11, 2022
2 parents a7816df + ba9c5a6 commit 38f159b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Flunt.Tests/StringValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,26 @@ public void AreNotEqualsLen()
Assert.AreEqual(false, contract.IsValid);
Assert.AreEqual(contract.Notifications.Count, 2);
}

[TestCategory("String Validation")]
[TestMethod("requires string len to be in a range")]
public void IsBetween()
{
var contract = new Contract<SampleEntity>()
.Requires()

.IsBetween(_entity.StringEmpty, 5, 10, "String", "Custom error message")

.IsBetween(_entity.StringFiveCharsLong, 6, 10, "String", "Custom error message")

.IsBetween(_entity.StringNull, 5, 10, "String", "Custom error message")

.IsBetween(_entity.StringWhiteSpace, 5, 10, "String", "Custom error message")

.IsBetween(_entity.StringNotEmpty, 3, 10, "String", "Custom error message");

Assert.AreEqual(false, contract.IsValid);
Assert.AreEqual(contract.Notifications.Count, 2);
}
}
}
24 changes: 24 additions & 0 deletions Flunt/Validations/StringValidationContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,29 @@ public Contract<T> IsLowerOrEqualsThan(string val, int comparer, string key, str

return this;
}


/// <summary>
/// Requires a string len is between
/// </summary>
/// <param name="val"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="key"></param>
/// <param name="message"></param>
/// <returns></returns>
public Contract<T> IsBetween(string val, int min, int max, string key, string message)
{
if (val == null)
return this;

if (string.IsNullOrEmpty(val) || string.IsNullOrWhiteSpace(val))
return this;

if (val.Length < min || val.Length > max)
AddNotification(key, message);

return this;
}
}
}

0 comments on commit 38f159b

Please sign in to comment.