Skip to content

Commit 44ffd9b

Browse files
committed
Add maximum length validator.
1 parent da3c377 commit 44ffd9b

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using CodingFlow.FluentValidation.Validators;
2+
using FluentAssertions;
3+
using static CodingFlow.FluentValidation.Validations;
4+
5+
namespace CodingFlow.FluentValidation.UnitTests;
6+
7+
public class MaximumLengthValidatorTests
8+
{
9+
[TestCase("short")]
10+
public void MaximumLength_Valid(string input)
11+
{
12+
var result = RuleFor(input)
13+
.MaximumLength(5)
14+
.Result();
15+
16+
result.Should().BeEquivalentTo(new ValidationResult
17+
{
18+
IsValid = true,
19+
Errors = []
20+
});
21+
}
22+
23+
[TestCase("kitten")]
24+
public void MaximumLength_Invalid(string input)
25+
{
26+
var result = RuleFor(input)
27+
.MaximumLength(5)
28+
.Result();
29+
30+
result.Should().BeEquivalentTo(new ValidationResult
31+
{
32+
IsValid = false,
33+
Errors = [new() { Message = $"Value '{input}' has a length more than 5." }]
34+
});
35+
}
36+
}
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 MaximumLengthValidator
4+
{
5+
/// <summary>
6+
/// Ensures the string has a length that is at most a maximum.
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="maximum">Maximum length of the string.</param>
12+
/// <returns></returns>
13+
public static FluentValidation<string> MaximumLength(this FluentValidation<string> validation, int maximum)
14+
{
15+
Validate(validation, maximum);
16+
17+
return validation;
18+
}
19+
20+
private static void Validate(FluentValidation<string> validation, int maximum)
21+
{
22+
validation.Validate(
23+
validation => validation.Input.Length <= maximum,
24+
new ValidationError($"Value '{validation.Input}' has a length more than {maximum}.")
25+
);
26+
}
27+
}

Examples/MaximumLengthExample.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 MaximumLengthExample
7+
{
8+
public void Run()
9+
{
10+
var input = "some string";
11+
12+
// begin-snippet: MaximumLengthExample
13+
RuleFor(input)
14+
.MaximumLength(5) // Must be at most 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
@@ -120,6 +120,20 @@ RuleFor(input)
120120
<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>
121121
<!-- endSnippet -->
122122

123+
## MaximumLength Validator
124+
125+
Ensures the string has a maximum length.
126+
127+
<!-- snippet: MaximumLengthExample -->
128+
<a id='snippet-MaximumLengthExample'></a>
129+
```cs
130+
RuleFor(input)
131+
.MaximumLength(5) // Must be at most 5 characters long.
132+
.Result();
133+
```
134+
<sup><a href='/Examples/MinimumLengthExample - Copy.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MaximumLengthExample' title='Start of snippet'>anchor</a></sup>
135+
<!-- endSnippet -->
136+
123137
## Regular Expression Validator
124138

125139
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
@@ -53,6 +53,12 @@ Ensures the string has a minimum length.
5353

5454
snippet: MinimumLengthExample
5555

56+
## MaximumLength Validator
57+
58+
Ensures the string has a maximum length.
59+
60+
snippet: MaximumLengthExample
61+
5662
## Regular Expression Validator
5763

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

0 commit comments

Comments
 (0)