Skip to content

Commit da6aa77

Browse files
iOvergaardleekelleherkjac
authored
V14: add checkboxlist to list of migrations (#15844)
* add checkboxlist to the list of migrations that should be converted * Adds string-array type-checking to the `ValueListUniqueValueValidator`. As `value?.ToString()` would give you a literal string of the object-type, e.g. `"System.Collections.Generic.List`1[System.String]"`. * Clean up and add tests --------- Co-authored-by: leekelleher <[email protected]> Co-authored-by: kjac <[email protected]>
1 parent 0440005 commit da6aa77

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

src/Umbraco.Infrastructure/Migrations/Upgrade/V_14_0_0/MigrateDataTypeConfigurations.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected override void Migrate()
8686
updated |= dataTypeDto.EditorAlias switch
8787
{
8888
PropertyEditorAliases.Boolean => HandleBoolean(ref configurationData),
89+
PropertyEditorAliases.CheckBoxList => HandleCheckBoxList(ref configurationData),
8990
PropertyEditorAliases.ColorPicker => HandleColorPicker(ref configurationData),
9091
PropertyEditorAliases.ContentPicker => HandleContentPicker(ref configurationData),
9192
PropertyEditorAliases.DateTime => HandleDateTime(ref configurationData),
@@ -126,6 +127,10 @@ protected override void Migrate()
126127
private bool HandleBoolean(ref Dictionary<string, object> configurationData)
127128
=> ReplaceIntegerStringWithBoolean(ref configurationData, "default");
128129

130+
// translate "selectable items" from old "value list" format to string array
131+
private bool HandleCheckBoxList(ref Dictionary<string, object> configurationData)
132+
=> ReplaceValueListArrayWithStringArray(ref configurationData, "items");
133+
129134
// translate "allowed colors" configuration from multiple old formats
130135
private bool HandleColorPicker(ref Dictionary<string, object> configurationData)
131136
{

src/Umbraco.Infrastructure/PropertyEditors/ValueListUniqueValueValidator.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ public ValueListUniqueValueValidator(IConfigurationEditorJsonSerializer configur
1919

2020
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
2121
{
22-
var stringValue = value?.ToString();
23-
if (stringValue.IsNullOrWhiteSpace())
22+
if (value is null)
2423
{
2524
yield break;
2625
}
2726

28-
string[]? items = null;
29-
try
30-
{
31-
items = _configurationEditorJsonSerializer.Deserialize<string[]>(stringValue);
32-
}
33-
catch
27+
var items = value as IEnumerable<string>;
28+
if (items is null)
3429
{
35-
// swallow and report error below
30+
try
31+
{
32+
items = _configurationEditorJsonSerializer.Deserialize<string[]>(value.ToString() ?? string.Empty);
33+
}
34+
catch
35+
{
36+
// swallow and report error below
37+
}
3638
}
3739

3840
if (items is null)
3941
{
40-
yield return new ValidationResult($"The configuration value {stringValue} is not a valid value list configuration", new[] { "items" });
42+
yield return new ValidationResult($"The configuration value {value} is not a valid value list configuration", ["items"]);
4143
yield break;
4244
}
4345

tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/EnsureUniqueValuesValidatorTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,55 @@ public void Validates_Multiple_Duplicate_Values()
7373
null);
7474
Assert.AreEqual(2, result.Count());
7575
}
76+
77+
[Test]
78+
public void Handles_Null()
79+
{
80+
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
81+
var result =
82+
validator.Validate(
83+
null,
84+
null,
85+
null);
86+
Assert.AreEqual(0, result.Count());
87+
}
88+
89+
[Test]
90+
public void Handles_IEnumerable_Of_String()
91+
{
92+
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
93+
IEnumerable<string> value = new[] { "one", "two", "three" };
94+
var result =
95+
validator.Validate(
96+
value,
97+
null,
98+
null);
99+
Assert.AreEqual(0, result.Count());
100+
}
101+
102+
[Test]
103+
public void Handles_Array_Of_String()
104+
{
105+
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
106+
string[] value = { "one", "two", "three" };
107+
var result =
108+
validator.Validate(
109+
value,
110+
null,
111+
null);
112+
Assert.AreEqual(0, result.Count());
113+
}
114+
115+
[Test]
116+
public void Handles_List_Of_String()
117+
{
118+
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
119+
var value = new List<string> { "one", "two", "three" };
120+
var result =
121+
validator.Validate(
122+
value,
123+
null,
124+
null);
125+
Assert.AreEqual(0, result.Count());
126+
}
76127
}

0 commit comments

Comments
 (0)