Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #326: Allow to have space before/after | for enums #327

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] = Regex.Replace(values[valueIndex], @"^\s*|\s*$", "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use .Trim() instead of regex. I think Trim does what you want and is also faster (not that this gets called so many times) than a regex

}

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
Loading