New: Improve validation errors for Custom Formats

Fixes #4733
This commit is contained in:
Qstick 2021-01-31 00:11:18 -05:00
parent e923b2fc6c
commit 8a84975954
1 changed files with 26 additions and 1 deletions

View File

@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.CustomFormats;
using Radarr.Http;
using Radarr.Http.REST;
@ -24,7 +27,29 @@ namespace Radarr.Api.V3.CustomFormats
SharedValidator.RuleFor(c => c.Name).NotEmpty();
SharedValidator.RuleFor(c => c.Name)
.Must((v, c) => !_formatService.All().Any(f => f.Name == c && f.Id != v.Id)).WithMessage("Must be unique.");
SharedValidator.RuleFor(c => c.Specifications).NotEmpty();
SharedValidator.RuleFor(c => c).Custom((customFormat, context) =>
{
if (!customFormat.Specifications.Any())
{
context.AddFailure("Must contain at least one Condition");
}
if (customFormat.Specifications.Any(s => s.Name.IsNullOrWhiteSpace()))
{
context.AddFailure("Condition name(s) cannot be empty or consist of only spaces");
}
foreach (var specification in customFormat.Specifications)
{
foreach (var field in specification.Fields.Where(f => (FieldType)Enum.Parse(typeof(FieldType), f.Type, true) == FieldType.Textbox))
{
if (specification.Name.IsNotNullOrWhiteSpace() && ((string)field.Value).IsNullOrWhiteSpace())
{
context.AddFailure(string.Format("'{0}' of condition '{1}' cannot be empty or consist of only spaces", field.Label, specification.Name));
}
}
}
});
}
protected override CustomFormatResource GetResourceById(int id)