Skip to content

Commit

Permalink
Added min/max validator for mediapicker3
Browse files Browse the repository at this point in the history
  • Loading branch information
Migaroez committed Dec 9, 2024
1 parent f048cfe commit f569553
Showing 1 changed file with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
Expand Down Expand Up @@ -66,7 +69,8 @@ public MediaPicker3PropertyValueEditor(
ITemporaryFileService temporaryFileService,
IScopeProvider scopeProvider,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDataTypeConfigurationCache dataTypeReadCache)
IDataTypeConfigurationCache dataTypeReadCache,
ILocalizedTextService localizedTextService)
: base(shortStringHelper, jsonSerializer, ioHelper, attribute)
{
_jsonSerializer = jsonSerializer;
Expand All @@ -76,6 +80,34 @@ public MediaPicker3PropertyValueEditor(
_scopeProvider = scopeProvider;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_dataTypeReadCache = dataTypeReadCache;
Validators.Add(new MinMaxValidator(jsonSerializer, localizedTextService));
}

Check notice on line 84 in src/Umbraco.Infrastructure/PropertyEditors/MediaPicker3PropertyEditor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

ℹ Getting worse: Constructor Over-Injection

MediaPicker3PropertyValueEditor increases from 10 to 11 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

[Obsolete("Use non obsoleted constructor instead. Scheduled for removal in v17")]
public MediaPicker3PropertyValueEditor(
IShortStringHelper shortStringHelper,
IJsonSerializer jsonSerializer,
IIOHelper ioHelper,
DataEditorAttribute attribute,
IMediaImportService mediaImportService,
IMediaService mediaService,
ITemporaryFileService temporaryFileService,
IScopeProvider scopeProvider,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDataTypeConfigurationCache dataTypeReadCache)
: this(
shortStringHelper,
jsonSerializer,
ioHelper,
attribute,
mediaImportService,
mediaService,
temporaryFileService,
scopeProvider,
backOfficeSecurityAccessor,
dataTypeReadCache,
StaticServiceProvider.Instance.GetRequiredService<ILocalizedTextService>())
{

Check notice on line 110 in src/Umbraco.Infrastructure/PropertyEditors/MediaPicker3PropertyEditor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

ℹ New issue: Constructor Over-Injection

MediaPicker3PropertyValueEditor has 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
}

/// <remarks>
Expand Down Expand Up @@ -294,5 +326,58 @@ public void ApplyConfiguration(MediaPicker3Configuration? configuration)
}
}
}

private class MinMaxValidator : IValueValidator
{
private readonly IJsonSerializer _jsonSerializer;
private readonly ILocalizedTextService _localizedTextService;

public MinMaxValidator(IJsonSerializer jsonSerializer, ILocalizedTextService localizedTextService)
{
_jsonSerializer = jsonSerializer;
_localizedTextService = localizedTextService;
}

public IEnumerable<ValidationResult> Validate(object? value, string? valueType,
object? dataTypeConfiguration)
{
var validationResults = new List<ValidationResult>();

if (dataTypeConfiguration is not MediaPicker3Configuration mediaPickerConfiguration)
{
return validationResults;
}

if (value is null ||
_jsonSerializer.TryDeserialize(value, out List<MediaWithCropsDto>? mediaWithCropsDtos) is false)
{
return validationResults;
}

if (mediaPickerConfiguration.ValidationLimit.Min is not null
&& mediaWithCropsDtos.Count < mediaPickerConfiguration.ValidationLimit.Min)
{
validationResults.Add(new ValidationResult(
_localizedTextService.Localize(
"validation",
"entriesShort",
new[] { mediaPickerConfiguration.ValidationLimit.Min.ToString(), (mediaPickerConfiguration.ValidationLimit.Min - mediaWithCropsDtos.Count).ToString(), }),
new[] { "validationLimit" }));
}

if (mediaPickerConfiguration.ValidationLimit.Max is not null
&& mediaWithCropsDtos.Count > mediaPickerConfiguration.ValidationLimit.Max)
{
validationResults.Add(new ValidationResult(
_localizedTextService.Localize(
"validation",
"entriesExceed",
new[] { mediaPickerConfiguration.ValidationLimit.Max.ToString(), (mediaWithCropsDtos.Count - mediaPickerConfiguration.ValidationLimit.Max).ToString(), }),
new[] { "validationLimit" }));
}

return validationResults;
}
}
}
}

0 comments on commit f569553

Please sign in to comment.