Validation for Custom Format specifications
Co-authored-by: Qstick <qstick@gmail.com>
This commit is contained in:
parent
1a4403e0ab
commit
3d6cf24d7c
|
@ -1,9 +1,11 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
using FluentValidation.Results;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Core.CustomFormats;
|
using NzbDrone.Core.CustomFormats;
|
||||||
|
using NzbDrone.Core.Validation;
|
||||||
using Sonarr.Http;
|
using Sonarr.Http;
|
||||||
using Sonarr.Http.REST;
|
using Sonarr.Http.REST;
|
||||||
using Sonarr.Http.REST.Attributes;
|
using Sonarr.Http.REST.Attributes;
|
||||||
|
@ -50,6 +52,9 @@ namespace Sonarr.Api.V3.CustomFormats
|
||||||
public ActionResult<CustomFormatResource> Create(CustomFormatResource customFormatResource)
|
public ActionResult<CustomFormatResource> Create(CustomFormatResource customFormatResource)
|
||||||
{
|
{
|
||||||
var model = customFormatResource.ToModel(_specifications);
|
var model = customFormatResource.ToModel(_specifications);
|
||||||
|
|
||||||
|
Validate(model);
|
||||||
|
|
||||||
return Created(_formatService.Insert(model).Id);
|
return Created(_formatService.Insert(model).Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +63,9 @@ namespace Sonarr.Api.V3.CustomFormats
|
||||||
public ActionResult<CustomFormatResource> Update(CustomFormatResource resource)
|
public ActionResult<CustomFormatResource> Update(CustomFormatResource resource)
|
||||||
{
|
{
|
||||||
var model = resource.ToModel(_specifications);
|
var model = resource.ToModel(_specifications);
|
||||||
|
|
||||||
|
Validate(model);
|
||||||
|
|
||||||
_formatService.Update(model);
|
_formatService.Update(model);
|
||||||
|
|
||||||
return Accepted(model.Id);
|
return Accepted(model.Id);
|
||||||
|
@ -91,6 +99,24 @@ namespace Sonarr.Api.V3.CustomFormats
|
||||||
return schema;
|
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<ICustomFormatSpecification> GetPresets()
|
private IEnumerable<ICustomFormatSpecification> GetPresets()
|
||||||
{
|
{
|
||||||
yield return new ReleaseTitleSpecification
|
yield return new ReleaseTitleSpecification
|
||||||
|
|
Loading…
Reference in New Issue