Skip to content

Commit

Permalink
Merge pull request #11 from BenjaminAbt/feature/fix-length-validation
Browse files Browse the repository at this point in the history
add fix for max length when value is null
  • Loading branch information
BenjaminAbt committed Feb 18, 2024
2 parents 2ce0cb5 + d16a3f0 commit bd71758
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StrongOf.FluentValidation/StrongStringValidators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static class StrongStringValidators
/// <returns>The rule builder options.</returns>
public static IRuleBuilderOptions<T, TStrong?> HasMaximumLength<T, TStrong>(this IRuleBuilder<T, TStrong?> rule, int maxLength)
where TStrong : StrongString<TStrong>
=> rule.Must(content => content?.Value is not null && content.Value.Length <= maxLength);
=> rule.Must(content => content?.Value is null ? true : content.Value.Length <= maxLength);

/// <summary>
/// Validates that the strong string matches a regular expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void HasMaximumLength_ShouldFail_WhenLengthIsGreaterThanMaxLength()
result.ShouldHaveValidationErrorFor(x => x.Strong);
}

[Fact]
public void HasMaximumLength_ShouldNotFail_WhenValueIsNull()
{
_validator.RuleFor(x => x.Strong).HasMaximumLength(5);

TestModel model = new() { Strong = null };
TestValidationResult<TestModel> result = _validator.TestValidate(model);
result.ShouldNotHaveValidationErrorFor(x => x.Strong);
}

[Fact]
public void IsRegexMatch_ShouldFail_WhenValueDoesNotMatchRegex()
{
Expand Down

0 comments on commit bd71758

Please sign in to comment.