indexer specific setting validation

pull/3113/head
kay.one 11 years ago
parent 227e13d858
commit e4fc0c57b3

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Internal;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.Mapping;
using NzbDrone.Api.REST;
using NzbDrone.Api.Validation;
using NzbDrone.Core.Indexers;
using Omu.ValueInjecter;
using FluentValidation;
@ -28,8 +26,6 @@ namespace NzbDrone.Api.Indexers
SharedValidator.RuleFor(c => c.Name).NotEmpty();
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
SharedValidator.RuleForField<string>(c=>c.Fields,"Url").NotEmpty();
PostValidator.RuleFor(c => c.Fields).NotEmpty();
}
@ -67,6 +63,9 @@ namespace NzbDrone.Api.Indexers
var indexer = _indexerService.Get(indexerResource.Id);
indexer.InjectFrom(indexerResource);
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
ValidateSetting(indexer.Settings);
indexer = _indexerService.Update(indexer);
var response = indexer.InjectTo<IndexerResource>();
@ -75,6 +74,17 @@ namespace NzbDrone.Api.Indexers
return response;
}
private static void ValidateSetting(IIndexerSetting setting)
{
var validationResult = setting.Validate();
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
}
private Indexer GetIndexer(IndexerResource indexerResource)
{
var indexer = _indexerService.Schema()
@ -90,6 +100,8 @@ namespace NzbDrone.Api.Indexers
indexer.InjectFrom(indexerResource);
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
ValidateSetting(indexer.Settings);
return indexer;
}

@ -1,4 +1,5 @@
using FluentValidation;
using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Validators;
namespace NzbDrone.Api.Validation
@ -14,5 +15,10 @@ namespace NzbDrone.Api.Validation
{
return ruleBuilder.SetValidator(new EqualValidator(0));
}
public static IRuleBuilderOptions<T, string> HaveHttpProtocol<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.SetValidator(new RegularExpressionValidator("^http(s)?://", RegexOptions.IgnoreCase)).WithMessage("must start with http:// or https://");
}
}
}

@ -55,6 +55,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FluentMigrator.1.1.1.0\tools\FluentMigrator.Runner.dll</HintPath>
</Reference>
<Reference Include="FluentValidation">
<HintPath>..\packages\FluentValidation.4.0.0.1\lib\Net40\FluentValidation.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

@ -4,6 +4,7 @@
<package id="CommonServiceLocator" version="1.0" targetFramework="net40" />
<package id="FluentAssertions" version="2.0.1" targetFramework="net40" />
<package id="FluentMigrator" version="1.1.1.0" targetFramework="net40" />
<package id="FluentValidation" version="4.0.0.1" targetFramework="net40" />
<package id="Moq" version="4.0.10827" />
<package id="NBuilder" version="3.0.1.1" />
<package id="NCrunch.Framework" version="1.46.0.9" targetFramework="net40" />

@ -1,26 +1,20 @@
namespace NzbDrone.Core.Indexers
using FluentValidation.Results;
namespace NzbDrone.Core.Indexers
{
public interface IIndexerSetting
{
bool IsValid { get; }
ValidationResult Validate();
}
public class NullSetting : IIndexerSetting
{
public static NullSetting Instance = new NullSetting();
private NullSetting()
{
}
public static readonly NullSetting Instance = new NullSetting();
public bool IsValid
public ValidationResult Validate()
{
get
{
return true;
}
return new ValidationResult();
}
}
}

@ -53,7 +53,7 @@ namespace NzbDrone.Core.Indexers
public List<IIndexer> GetAvailableIndexers()
{
return All().Where(c => c.Enable && c.Settings.IsValid).Select(c => c.Instance).ToList();
return All().Where(c => c.Enable && c.Settings.Validate().IsValid).Select(c => c.Instance).ToList();
}
public Indexer Get(int id)

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using FluentValidation;
using FluentValidation.Results;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Newznab
{
@ -8,7 +11,8 @@ namespace NzbDrone.Core.Indexers.Newznab
{
public NewznabSettings()
{
Categories = new [] { 5030, 5040 };
Categories = new[] { 5030, 5040 };
//RuleFor(c => c.Url).ValidRootUrl();
}
[FieldDefinition(0, Label = "URL")]
@ -26,5 +30,11 @@ namespace NzbDrone.Core.Indexers.Newznab
return !string.IsNullOrWhiteSpace(Url);
}
}
public ValidationResult Validate()
{
return new ValidationResult();
//return Validate(this);
}
}
}

@ -1,22 +1,28 @@
using System;
using FluentValidation;
using FluentValidation.Results;
using NzbDrone.Core.Annotations;
namespace NzbDrone.Core.Indexers.Omgwtfnzbs
{
public class OmgwtfnzbsSetting : IIndexerSetting
public class OmgwtfnzbsSetting : AbstractValidator<OmgwtfnzbsSetting>, IIndexerSetting
{
public OmgwtfnzbsSetting()
{
RuleFor(c => c.Username).NotEmpty();
RuleFor(c => c.ApiKey).NotEmpty();
}
[FieldDefinition(0, Label = "Username")]
public String Username { get; set; }
[FieldDefinition(1, Label = "API Key")]
public String ApiKey { get; set; }
public bool IsValid
public ValidationResult Validate()
{
get
{
return !string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(ApiKey);
}
return Validate(this);
}
}
}

@ -67,6 +67,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FluentMigrator.1.1.1.0\tools\FluentMigrator.Runner.dll</HintPath>
</Reference>
<Reference Include="FluentValidation, Version=4.0.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FluentValidation.4.0.0.1\lib\Net40\FluentValidation.dll</HintPath>
</Reference>
<Reference Include="Growl.Connector">
<HintPath>..\packages\Growl.0.6\lib\Growl.Connector.dll</HintPath>
</Reference>
@ -490,6 +494,7 @@
<Compile Include="Update\UpdatePackageProvider.cs" />
<Compile Include="Update\UpdatePackage.cs" />
<Compile Include="Update\UpdateCheckService.cs" />
<Compile Include="Validation\RuleBuilderExtensions.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">

@ -0,0 +1,29 @@
using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Validators;
namespace NzbDrone.Core.Validation
{
public static class RuleBuilderExtensions
{
public static IRuleBuilderOptions<T, int> ValidId<T>(this IRuleBuilder<T, int> ruleBuilder)
{
return ruleBuilder.SetValidator(new GreaterThanValidator(0));
}
public static IRuleBuilderOptions<T, int> IsZero<T>(this IRuleBuilder<T, int> ruleBuilder)
{
return ruleBuilder.SetValidator(new EqualValidator(0));
}
public static IRuleBuilderOptions<T, string> HaveHttpProtocol<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.SetValidator(new RegularExpressionValidator("^http(s)?://", RegexOptions.IgnoreCase)).WithMessage("must start with http:// or https://");
}
public static IRuleBuilderOptions<T, string> ValidRootUrl<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.SetValidator(new RegularExpressionValidator("^http(?:s)?://[a-z0-9-.]+", RegexOptions.IgnoreCase)).WithMessage("must be valid URL that");
}
}
}

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentMigrator" version="1.1.1.0" targetFramework="net40" />
<package id="FluentValidation" version="4.0.0.1" targetFramework="net40" />
<package id="Growl" version="0.6" />
<package id="MediaInfoNet" version="0.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net40" />

Loading…
Cancel
Save