From e5734588b0edf6ca345a277f9681e53b1c91df94 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Wed, 26 Jul 2023 11:04:39 +0300 Subject: [PATCH] Fixed: Ensure validation for Custom Format specifications Co-authored-by: Qstick Closes #3924 --- .../CustomFormats/CustomFormatController.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs b/src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs index 634e5b355..6885265db 100644 --- a/src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs +++ b/src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs @@ -1,12 +1,14 @@ using System.Collections.Generic; using System.Linq; using FluentValidation; +using FluentValidation.Results; using Lidarr.Http; using Lidarr.Http.REST; using Lidarr.Http.REST.Attributes; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.Extensions; using NzbDrone.Core.CustomFormats; +using NzbDrone.Core.Validation; namespace Lidarr.Api.V1.CustomFormats { @@ -50,6 +52,9 @@ namespace Lidarr.Api.V1.CustomFormats public ActionResult Create(CustomFormatResource customFormatResource) { var model = customFormatResource.ToModel(_specifications); + + Validate(model); + return Created(_formatService.Insert(model).Id); } @@ -58,6 +63,9 @@ namespace Lidarr.Api.V1.CustomFormats public ActionResult Update(CustomFormatResource resource) { var model = resource.ToModel(_specifications); + + Validate(model); + _formatService.Update(model); return Accepted(model.Id); @@ -91,6 +99,24 @@ namespace Lidarr.Api.V1.CustomFormats return schema; } + private void Validate(CustomFormat definition) + { + foreach (var validationResult in definition.Specifications.Select(spec => spec.Validate())) + { + VerifyValidationResult(validationResult); + } + } + + private void VerifyValidationResult(ValidationResult validationResult) + { + var result = new NzbDroneValidationResult(validationResult.Errors); + + if (!result.IsValid) + { + throw new ValidationException(result.Errors); + } + } + private IEnumerable GetPresets() { yield return new ReleaseTitleSpecification