Handle create and update validation for media picker. (#18537)

This commit is contained in:
Andy Butland
2025-03-04 14:08:08 +01:00
committed by GitHub
parent 5358c0346e
commit ca6da751f2
2 changed files with 46 additions and 18 deletions

View File

@@ -86,7 +86,7 @@ public class MediaPicker3PropertyEditor : DataEditor
var validators = new TypedJsonValidatorRunner<List<MediaWithCropsDto>, MediaPicker3Configuration>(
jsonSerializer,
new MinMaxValidator(localizedTextService),
new AllowedTypeValidator(localizedTextService, mediaTypeService),
new AllowedTypeValidator(localizedTextService, mediaTypeService, _mediaService),
new StartNodeValidator(localizedTextService, mediaNavigationQueryService));
Validators.Add(validators);
@@ -367,11 +367,13 @@ public class MediaPicker3PropertyEditor : DataEditor
{
private readonly ILocalizedTextService _localizedTextService;
private readonly IMediaTypeService _mediaTypeService;
private readonly IMediaService _mediaService;
public AllowedTypeValidator(ILocalizedTextService localizedTextService, IMediaTypeService mediaTypeService)
public AllowedTypeValidator(ILocalizedTextService localizedTextService, IMediaTypeService mediaTypeService, IMediaService mediaService)
{
_localizedTextService = localizedTextService;
_mediaTypeService = mediaTypeService;
_mediaService = mediaService;
}
public IEnumerable<ValidationResult> Validate(
@@ -393,7 +395,20 @@ public class MediaPicker3PropertyEditor : DataEditor
return [];
}
IEnumerable<string> distinctTypeAliases = value.DistinctBy(x => x.MediaTypeAlias).Select(x => x.MediaTypeAlias);
// We may or may not have explicit MediaTypeAlias values provided, depending on whether the operation is an update or a
// create. So let's make sure we have them all.
IEnumerable<string> providedTypeAliases = value
.Where(x => x.MediaTypeAlias.IsNullOrWhiteSpace() is false)
.Select(x => x.MediaTypeAlias);
IEnumerable<Guid> retrievedMediaKeys = value
.Where(x => x.MediaTypeAlias.IsNullOrWhiteSpace())
.Select(x => x.MediaKey);
IEnumerable<IMedia> retrievedMedia = _mediaService.GetByIds(retrievedMediaKeys);
IEnumerable<string> retrievedTypeAliases = retrievedMedia
.Select(x => x.ContentType.Alias);
IEnumerable<string> distinctTypeAliases = providedTypeAliases.Union(retrievedTypeAliases).Distinct();
foreach (var typeAlias in distinctTypeAliases)
{