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 <leekelleher@gmail.com> Co-authored-by: kjac <kja@umbraco.dk>
This commit is contained in:
@@ -86,6 +86,7 @@ public class MigrateDataTypeConfigurations : MigrationBase
|
||||
updated |= dataTypeDto.EditorAlias switch
|
||||
{
|
||||
PropertyEditorAliases.Boolean => HandleBoolean(ref configurationData),
|
||||
PropertyEditorAliases.CheckBoxList => HandleCheckBoxList(ref configurationData),
|
||||
PropertyEditorAliases.ColorPicker => HandleColorPicker(ref configurationData),
|
||||
PropertyEditorAliases.ContentPicker => HandleContentPicker(ref configurationData),
|
||||
PropertyEditorAliases.DateTime => HandleDateTime(ref configurationData),
|
||||
@@ -126,6 +127,10 @@ public class MigrateDataTypeConfigurations : MigrationBase
|
||||
private bool HandleBoolean(ref Dictionary<string, object> configurationData)
|
||||
=> ReplaceIntegerStringWithBoolean(ref configurationData, "default");
|
||||
|
||||
// translate "selectable items" from old "value list" format to string array
|
||||
private bool HandleCheckBoxList(ref Dictionary<string, object> configurationData)
|
||||
=> ReplaceValueListArrayWithStringArray(ref configurationData, "items");
|
||||
|
||||
// translate "allowed colors" configuration from multiple old formats
|
||||
private bool HandleColorPicker(ref Dictionary<string, object> configurationData)
|
||||
{
|
||||
|
||||
@@ -19,25 +19,27 @@ public class ValueListUniqueValueValidator : IValueValidator
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
var stringValue = value?.ToString();
|
||||
if (stringValue.IsNullOrWhiteSpace())
|
||||
if (value is null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
string[]? items = null;
|
||||
try
|
||||
var items = value as IEnumerable<string>;
|
||||
if (items is null)
|
||||
{
|
||||
items = _configurationEditorJsonSerializer.Deserialize<string[]>(stringValue);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// swallow and report error below
|
||||
try
|
||||
{
|
||||
items = _configurationEditorJsonSerializer.Deserialize<string[]>(value.ToString() ?? string.Empty);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// swallow and report error below
|
||||
}
|
||||
}
|
||||
|
||||
if (items is null)
|
||||
{
|
||||
yield return new ValidationResult($"The configuration value {stringValue} is not a valid value list configuration", new[] { "items" });
|
||||
yield return new ValidationResult($"The configuration value {value} is not a valid value list configuration", ["items"]);
|
||||
yield break;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,4 +73,55 @@ public class EnsureUniqueValuesValidatorTest
|
||||
null);
|
||||
Assert.AreEqual(2, result.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Handles_Null()
|
||||
{
|
||||
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
|
||||
var result =
|
||||
validator.Validate(
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
Assert.AreEqual(0, result.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Handles_IEnumerable_Of_String()
|
||||
{
|
||||
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
|
||||
IEnumerable<string> value = new[] { "one", "two", "three" };
|
||||
var result =
|
||||
validator.Validate(
|
||||
value,
|
||||
null,
|
||||
null);
|
||||
Assert.AreEqual(0, result.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Handles_Array_Of_String()
|
||||
{
|
||||
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
|
||||
string[] value = { "one", "two", "three" };
|
||||
var result =
|
||||
validator.Validate(
|
||||
value,
|
||||
null,
|
||||
null);
|
||||
Assert.AreEqual(0, result.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Handles_List_Of_String()
|
||||
{
|
||||
var validator = new ValueListUniqueValueValidator(ConfigurationEditorJsonSerializer());
|
||||
var value = new List<string> { "one", "two", "three" };
|
||||
var result =
|
||||
validator.Validate(
|
||||
value,
|
||||
null,
|
||||
null);
|
||||
Assert.AreEqual(0, result.Count());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user