Skip to content

Commit

Permalink
Fix #326: Allow to have space before/after | for enums (#327)
Browse files Browse the repository at this point in the history
* Fix #326: Allow to have space before/after | for enums

* Using trim instead of Regex
  • Loading branch information
antoineatstariongroup authored Apr 9, 2024
1 parent b9e6963 commit c3b294e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,24 @@ public void VerifyEnumerationParameterTypeValidationAndCleanup()

result = this.enumerationParameterType.Validate(42, things, out _);
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid));

this.valueSet.Manual[0] = "low | medium";
result = this.enumerationParameterType.ValidateAndCleanup(this.valueSet, null, things);

Assert.Multiple(() =>
{
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid));
Assert.That(this.valueSet.Manual[0], Is.EqualTo("low|medium"));
});

this.valueSet.Manual[0] = "low | medium";
result = this.enumerationParameterType.ValidateAndCleanup(this.valueSet, null, things);

Assert.Multiple(() =>
{
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid));
Assert.That(this.valueSet.Manual[0], Is.EqualTo("low|medium"));
});
}

[Test]
Expand Down
18 changes: 18 additions & 0 deletions CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,24 @@ public void VerifyEnumerationParameterTypeValidationAndCleanup()

result = this.enumerationParameterType.Validate(42, things, out _);
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid));

this.valueSet.Manual[0] = "low | medium";
result = this.enumerationParameterType.ValidateAndCleanup(this.valueSet, null, things);

Assert.Multiple(() =>
{
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid));
Assert.That(this.valueSet.Manual[0], Is.EqualTo("low|medium"));
});

this.valueSet.Manual[0] = "low | medium";
result = this.enumerationParameterType.ValidateAndCleanup(this.valueSet, null, things);

Assert.Multiple(() =>
{
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid));
Assert.That(this.valueSet.Manual[0], Is.EqualTo("low|medium"));
});
}

[Test]
Expand Down
10 changes: 8 additions & 2 deletions CDP4Common/Validation/ObjectValueValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace CDP4Common.Validation
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

using CDP4Common.Helpers;
using CDP4Common.SiteDirectoryData;
Expand Down Expand Up @@ -254,7 +255,12 @@ public static ValidationResult ValidateEnumeration(this object value, IReadOnlyC
}

var values = castedString.Split(Constants.MultiValueEnumSeparator);


for (var valueIndex = 0; valueIndex < values.Length; valueIndex++)
{
values[valueIndex] = values[valueIndex].Trim();
}

if (!isMultiSelectAllowed && values.Length > 1)
{
cleanedValue = null;
Expand All @@ -281,7 +287,7 @@ public static ValidationResult ValidateEnumeration(this object value, IReadOnlyC

if (Array.Exists(values, enumerationValue => allowedValues.Any(x => x.Trim() == enumerationValue)))
{
cleanedValue = castedString;
cleanedValue = string.Join($"{Constants.MultiValueEnumSeparator}", values);
Logger.Debug("Enumeration {0} validated", castedString);
return ValidationResult.ValidResult();
}
Expand Down

0 comments on commit c3b294e

Please sign in to comment.