Only validate invariant properties when strictly necessary (#18729)

This commit is contained in:
Kenn Jacobsen
2025-03-25 12:58:01 +01:00
committed by GitHub
parent 80e092069b
commit 2711ac07ac
6 changed files with 225 additions and 15 deletions

View File

@@ -1,6 +1,8 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Models;
@@ -17,6 +19,8 @@ public class PropertyValidationService : IPropertyValidationService
private readonly PropertyEditorCollection _propertyEditors;
private readonly IValueEditorCache _valueEditorCache;
private readonly ICultureDictionary _cultureDictionary;
private readonly ILanguageService _languageService;
private readonly ContentSettings _contentSettings;
[Obsolete("Use the constructor that accepts ICultureDictionary. Will be removed in V15.")]
public PropertyValidationService(
@@ -28,18 +32,40 @@ public class PropertyValidationService : IPropertyValidationService
{
}
[Obsolete("Use the constructor that accepts ILanguageService and ContentSettings options. Will be removed in V17.")]
public PropertyValidationService(
PropertyEditorCollection propertyEditors,
IDataTypeService dataTypeService,
ILocalizedTextService textService,
IValueEditorCache valueEditorCache,
ICultureDictionary cultureDictionary)
: this(
propertyEditors,
dataTypeService,
textService,
valueEditorCache,
cultureDictionary,
StaticServiceProvider.Instance.GetRequiredService<ILanguageService>(),
StaticServiceProvider.Instance.GetRequiredService<IOptions<ContentSettings>>())
{
}
public PropertyValidationService(
PropertyEditorCollection propertyEditors,
IDataTypeService dataTypeService,
ILocalizedTextService textService,
IValueEditorCache valueEditorCache,
ICultureDictionary cultureDictionary,
ILanguageService languageService,
IOptions<ContentSettings> contentSettings)
{
_propertyEditors = propertyEditors;
_dataTypeService = dataTypeService;
_textService = textService;
_valueEditorCache = valueEditorCache;
_cultureDictionary = cultureDictionary;
_languageService = languageService;
_contentSettings = contentSettings.Value;
}
/// <inheritdoc />
@@ -66,6 +92,19 @@ public class PropertyValidationService : IPropertyValidationService
propertyType.PropertyEditorAlias);
}
// only validate culture invariant properties if
// - AllowEditInvariantFromNonDefault is true, or
// - the default language is being validated, or
// - the underlying data editor supports partial property value merging (e.g. block level variance)
var defaultCulture = _languageService.GetDefaultIsoCodeAsync().GetAwaiter().GetResult();
if (propertyType.VariesByCulture() is false
&& _contentSettings.AllowEditInvariantFromNonDefault is false
&& validationContext.CulturesBeingValidated.InvariantContains(defaultCulture) is false
&& dataEditor.CanMergePartialPropertyValues(propertyType) is false)
{
return [];
}
return ValidatePropertyValue(dataEditor, dataType, postedValue, propertyType.Mandatory, propertyType.ValidationRegExp, propertyType.MandatoryMessage, propertyType.ValidationRegExpMessage, validationContext);
}