diff --git a/src/Lidarr.Api.V1/Artist/ArtistEditorController.cs b/src/Lidarr.Api.V1/Artist/ArtistEditorController.cs index e7c69ab20..c4eedb3d4 100644 --- a/src/Lidarr.Api.V1/Artist/ArtistEditorController.cs +++ b/src/Lidarr.Api.V1/Artist/ArtistEditorController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using FluentValidation; using Lidarr.Api.V1.Albums; using Lidarr.Http; using Microsoft.AspNetCore.Mvc; @@ -16,12 +17,14 @@ namespace Lidarr.Api.V1.Artist private readonly IArtistService _artistService; private readonly IAlbumService _albumService; private readonly IManageCommandQueue _commandQueueManager; + private readonly ArtistEditorValidator _artistEditorValidator; - public ArtistEditorController(IArtistService artistService, IAlbumService albumService, IManageCommandQueue commandQueueManager) + public ArtistEditorController(IArtistService artistService, IAlbumService albumService, IManageCommandQueue commandQueueManager, ArtistEditorValidator artistEditorValidator) { _artistService = artistService; _albumService = albumService; _commandQueueManager = commandQueueManager; + _artistEditorValidator = artistEditorValidator; } [HttpPut] @@ -80,6 +83,13 @@ namespace Lidarr.Api.V1.Artist break; } } + + var validationResult = _artistEditorValidator.Validate(artist); + + if (!validationResult.IsValid) + { + throw new ValidationException(validationResult.Errors); + } } if (artistToMove.Any()) diff --git a/src/Lidarr.Api.V1/Artist/ArtistEditorValidator.cs b/src/Lidarr.Api.V1/Artist/ArtistEditorValidator.cs new file mode 100644 index 000000000..05677a5c9 --- /dev/null +++ b/src/Lidarr.Api.V1/Artist/ArtistEditorValidator.cs @@ -0,0 +1,26 @@ +using FluentValidation; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Validation; +using NzbDrone.Core.Validation.Paths; + +namespace Lidarr.Api.V1.Artist +{ + public class ArtistEditorValidator : AbstractValidator + { + public ArtistEditorValidator(RootFolderExistsValidator rootFolderExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator, MetadataProfileExistsValidator metadataProfileExistsValidator) + { + RuleFor(a => a.RootFolderPath).Cascade(CascadeMode.Stop) + .IsValidPath() + .SetValidator(rootFolderExistsValidator) + .When(a => a.RootFolderPath.IsNotNullOrWhiteSpace()); + + RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(qualityProfileExistsValidator); + + RuleFor(c => c.MetadataProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(metadataProfileExistsValidator); + } + } +}