From d16a3f0bf74ae2f21642c4e27ea1c461f23c8264 Mon Sep 17 00:00:00 2001 From: BEN ABT Date: Sun, 18 Feb 2024 22:59:05 +0100 Subject: [PATCH] add fix --- .../StrongStringValidators.cs | 2 +- .../StrongStringValidatorsTests.cs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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() {