Skip to content

Commit 322d78c

Browse files
committed
Add minimum length validator.
1 parent 54d6f64 commit 322d78c

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using CodingFlow.FluentValidation.Validators;
2+
using FluentAssertions;
3+
using static CodingFlow.FluentValidation.Validations;
4+
5+
namespace CodingFlow.FluentValidation.UnitTests;
6+
7+
public class MinimumLengthValidatorTests
8+
{
9+
[TestCase("short")]
10+
[TestCase("long string")]
11+
public void MinimumLength_Valid(string input)
12+
{
13+
var result = RuleFor(input)
14+
.MinimumLength(5)
15+
.Result();
16+
17+
result.Should().BeEquivalentTo(new ValidationResult
18+
{
19+
IsValid = true,
20+
Errors = []
21+
});
22+
}
23+
24+
[TestCase("four")]
25+
public void MinimumLength_Invalid(string input)
26+
{
27+
var result = RuleFor(input)
28+
.MinimumLength(5)
29+
.Result();
30+
31+
result.Should().BeEquivalentTo(new ValidationResult
32+
{
33+
IsValid = false,
34+
Errors = [new() { Message = $"Value '{input}' has a length less than 5." }]
35+
});
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace CodingFlow.FluentValidation.Validators;
2+
3+
public static class MinimumLengthValidator
4+
{
5+
/// <summary>
6+
/// Ensures the string has a length that is at least as long as a minimum.
7+
/// </summary>
8+
/// <typeparam name="T">Input type.</typeparam>
9+
/// <param name="validation"></param>
10+
/// <param name="input">String to check length.</param>
11+
/// <param name="minimum">Minimum length of the string.</param>
12+
/// <returns></returns>
13+
public static FluentValidation<string> MinimumLength(this FluentValidation<string> validation, int minimum)
14+
{
15+
Validate(validation, minimum);
16+
17+
return validation;
18+
}
19+
20+
private static void Validate(FluentValidation<string> validation, int minimum)
21+
{
22+
validation.Validate(
23+
validation => validation.Input.Length >= minimum,
24+
new ValidationError($"Value '{validation.Input}' has a length less than {minimum}.")
25+
);
26+
}
27+
}

Examples/MinimumLengthExample.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using CodingFlow.FluentValidation.Validators;
2+
using static CodingFlow.FluentValidation.Validations;
3+
4+
namespace Examples;
5+
6+
internal class MinimumLengthExample
7+
{
8+
public void Run()
9+
{
10+
var input = "some string";
11+
12+
// begin-snippet: MinimumLengthExample
13+
RuleFor(input)
14+
.MinimumLength(5) // Must be at least 5 characters long.
15+
.Result();
16+
// end-snippet
17+
}
18+
}

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ RuleFor(input)
106106
<sup><a href='/Examples/EqualBasicExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-EqualBasicExample' title='Start of snippet'>anchor</a></sup>
107107
<!-- endSnippet -->
108108

109+
## MinimumLength Validator
110+
111+
Ensures the string has a minimum length.
112+
113+
<!-- snippet: MinimumLengthExample -->
114+
<a id='snippet-MinimumLengthExample'></a>
115+
```cs
116+
RuleFor(input)
117+
.MinimumLength(5) // Must be at least 5 characters long.
118+
.Result();
119+
```
120+
<sup><a href='/Examples/MinimumLengthExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MinimumLengthExample' title='Start of snippet'>anchor</a></sup>
121+
<!-- endSnippet -->
122+
109123
## Regular Expression Validator
110124

111125
Aka `Matches`, ensure the string passes a regular expression test.

README.source.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ Ensures the input is considered equal to the provided value. For reference types
4747

4848
snippet: EqualBasicExample
4949

50+
## MinimumLength Validator
51+
52+
Ensures the string has a minimum length.
53+
54+
snippet: MinimumLengthExample
55+
5056
## Regular Expression Validator
5157

5258
Aka `Matches`, ensure the string passes a regular expression test.

0 commit comments

Comments
 (0)