diff --git a/src/StrongOf.FluentValidation/StrongStringValidators.cs b/src/StrongOf.FluentValidation/StrongStringValidators.cs index d9d93aa..8b648df 100644 --- a/src/StrongOf.FluentValidation/StrongStringValidators.cs +++ b/src/StrongOf.FluentValidation/StrongStringValidators.cs @@ -46,7 +46,7 @@ public static class StrongStringValidators /// The rule builder options. public static IRuleBuilderOptions HasMaximumLength(this IRuleBuilder rule, int maxLength) where TStrong : StrongString - => rule.Must(content => content?.Value is not null && content.Value.Length <= maxLength); + => rule.Must(content => content?.Value is null ? true : content.Value.Length <= maxLength); /// /// Validates that the strong string matches a regular expression. diff --git a/tests/StrongOf.FluentValidation.UnitTests/StrongStringValidatorsTests.cs b/tests/StrongOf.FluentValidation.UnitTests/StrongStringValidatorsTests.cs index a5a940c..dea4393 100644 --- a/tests/StrongOf.FluentValidation.UnitTests/StrongStringValidatorsTests.cs +++ b/tests/StrongOf.FluentValidation.UnitTests/StrongStringValidatorsTests.cs @@ -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 result = _validator.TestValidate(model); + result.ShouldNotHaveValidationErrorFor(x => x.Strong); + } + [Fact] public void IsRegexMatch_ShouldFail_WhenValueDoesNotMatchRegex() {