Fixed: Ensure validation for Auto Tagging specifications

This commit is contained in:
Bogdan 2023-07-26 09:25:24 +03:00
parent 430ea81937
commit d5c1f58839
1 changed files with 26 additions and 0 deletions

View File

@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Validation;
using Radarr.Http;
using Radarr.Http.REST;
using Radarr.Http.REST.Attributes;
@ -52,6 +54,9 @@ namespace Radarr.Api.V3.AutoTagging
public ActionResult<AutoTaggingResource> Create(AutoTaggingResource autoTagResource)
{
var model = autoTagResource.ToModel(_specifications);
Validate(model);
return Created(_autoTaggingService.Insert(model).Id);
}
@ -60,6 +65,9 @@ namespace Radarr.Api.V3.AutoTagging
public ActionResult<AutoTaggingResource> Update(AutoTaggingResource resource)
{
var model = resource.ToModel(_specifications);
Validate(model);
_autoTaggingService.Update(model);
return Accepted(model.Id);
@ -85,5 +93,23 @@ namespace Radarr.Api.V3.AutoTagging
return schema;
}
private void Validate(AutoTag 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);
}
}
}
}