Adding invalid prop group aliases as ModelState errors, so we don't introduce breaking changes

This commit is contained in:
Elitsa Marinovska
2021-10-14 09:57:13 +02:00
parent 3f3262eb01
commit ccd9013d30

View File

@@ -454,10 +454,12 @@ namespace Umbraco.Web.Editors
var validateAttempt = service.ValidateComposition(composition);
if (validateAttempt == false)
{
//if it's not successful then we need to return some model state for the property aliases that
// are duplicated
var invalidPropertyAliases = validateAttempt.Result.Distinct();
AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(contentTypeSave, invalidPropertyAliases);
// if it's not successful then we need to return some model state for the property type and property group
// aliases that are duplicated
var invalidPropertyTypeAliases = validateAttempt.Result.Distinct();
var invalidPropertyGroupAliases = (validateAttempt.Exception as InvalidCompositionException)?.PropertyGroupAliases ?? Array.Empty<string>();
AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(contentTypeSave, invalidPropertyTypeAliases, invalidPropertyGroupAliases);
var display = Mapper.Map<TContentTypeDisplay>(composition);
//map the 'save' data on top
@@ -472,22 +474,32 @@ namespace Umbraco.Web.Editors
/// Adds errors to the model state if any invalid aliases are found then throws an error response if there are errors
/// </summary>
/// <param name="contentTypeSave"></param>
/// <param name="invalidPropertyAliases"></param>
/// <param name="invalidPropertyTypeAliases"></param>
/// <param name="invalidPropertyGroupAliases"></param>
/// <returns></returns>
private void AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(TContentTypeSave contentTypeSave, IEnumerable<string> invalidPropertyAliases)
private void AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(TContentTypeSave contentTypeSave, IEnumerable<string> invalidPropertyTypeAliases, IEnumerable<string> invalidPropertyGroupAliases)
where TContentTypeSave : ContentTypeSave<TPropertyType>
where TPropertyType : PropertyTypeBasic
{
foreach (var propertyAlias in invalidPropertyAliases)
foreach (var propertyTypeAlias in invalidPropertyTypeAliases)
{
// Find the property relating to these
var property = contentTypeSave.Groups.SelectMany(x => x.Properties).Single(x => x.Alias == propertyAlias);
// Find the property type relating to these
var property = contentTypeSave.Groups.SelectMany(x => x.Properties).Single(x => x.Alias == propertyTypeAlias);
var group = contentTypeSave.Groups.Single(x => x.Properties.Contains(property));
var propertyIndex = group.Properties.IndexOf(property);
var groupIndex = contentTypeSave.Groups.IndexOf(group);
var key = $"Groups[{groupIndex}].Properties[{propertyIndex}].Alias";
ModelState.AddModelError(key, "Duplicate property aliases not allowed between compositions");
ModelState.AddModelError(key, "Duplicate property aliases are not allowed between compositions");
}
foreach (var propertyGroupAlias in invalidPropertyGroupAliases)
{
// Find the property group relating to these
var group = contentTypeSave.Groups.Single(x => x.Alias == propertyGroupAlias);
var groupIndex = contentTypeSave.Groups.IndexOf(group);
var key = $"Groups[{groupIndex}].Name";
ModelState.AddModelError(key, "Duplicate property group aliases are not allowed between compositions");
}
}
@@ -519,7 +531,7 @@ namespace Umbraco.Web.Editors
}
if (invalidCompositionException != null)
{
AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(contentTypeSave, invalidCompositionException.PropertyTypeAliases);
AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(contentTypeSave, invalidCompositionException.PropertyTypeAliases, invalidCompositionException.PropertyGroupAliases);
return CreateModelStateValidationException<TContentTypeSave, TContentTypeDisplay>(ctId, contentTypeSave, ct);
}
return null;