fixed input validation for indexers

This commit is contained in:
kay.one 2013-08-25 21:01:03 -07:00
parent 147bb5476b
commit 438e3199de
6 changed files with 48 additions and 37 deletions

View File

@ -64,7 +64,7 @@ namespace NzbDrone.Api.Indexers
indexer.InjectFrom(indexerResource); indexer.InjectFrom(indexerResource);
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields); indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
ValidateSetting(indexer.Settings); ValidateIndexer(indexer);
indexer = _indexerService.Update(indexer); indexer = _indexerService.Update(indexer);
@ -75,13 +75,16 @@ namespace NzbDrone.Api.Indexers
} }
private static void ValidateSetting(IIndexerSetting setting) private static void ValidateIndexer(Indexer indexer)
{ {
var validationResult = setting.Validate(); if (indexer.Enable)
if (!validationResult.IsValid)
{ {
throw new ValidationException(validationResult.Errors); var validationResult = indexer.Settings.Validate();
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
} }
} }
@ -100,7 +103,7 @@ namespace NzbDrone.Api.Indexers
indexer.InjectFrom(indexerResource); indexer.InjectFrom(indexerResource);
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields); indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
ValidateSetting(indexer.Settings); ValidateIndexer(indexer);
return indexer; return indexer;
} }

View File

@ -6,15 +6,4 @@ namespace NzbDrone.Core.Indexers
{ {
ValidationResult Validate(); ValidationResult Validate();
} }
public class NullSetting : IIndexerSetting
{
public static readonly NullSetting Instance = new NullSetting();
public ValidationResult Validate()
{
return new ValidationResult();
}
}
} }

View File

@ -7,12 +7,22 @@ using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Newznab namespace NzbDrone.Core.Indexers.Newznab
{ {
public class NewznabSettingsValidator : AbstractValidator<NewznabSettings>
{
public NewznabSettingsValidator()
{
RuleFor(c => c.Url).ValidRootUrl();
}
}
public class NewznabSettings : IIndexerSetting public class NewznabSettings : IIndexerSetting
{ {
private static readonly NewznabSettingsValidator Validator = new NewznabSettingsValidator();
public NewznabSettings() public NewznabSettings()
{ {
Categories = new[] { 5030, 5040 }; Categories = new[] { 5030, 5040 };
//RuleFor(c => c.Url).ValidRootUrl();
} }
[FieldDefinition(0, Label = "URL")] [FieldDefinition(0, Label = "URL")]
@ -23,18 +33,9 @@ namespace NzbDrone.Core.Indexers.Newznab
public IEnumerable<Int32> Categories { get; set; } public IEnumerable<Int32> Categories { get; set; }
public bool IsValid
{
get
{
return !string.IsNullOrWhiteSpace(Url);
}
}
public ValidationResult Validate() public ValidationResult Validate()
{ {
return new ValidationResult(); return Validator.Validate(this);
//return Validate(this);
} }
} }
} }

View File

@ -0,0 +1,14 @@
using FluentValidation.Results;
namespace NzbDrone.Core.Indexers
{
public class NullSetting : IIndexerSetting
{
public static readonly NullSetting Instance = new NullSetting();
public ValidationResult Validate()
{
return new ValidationResult();
}
}
}

View File

@ -1,18 +1,22 @@
using System; using System;
using FluentValidation; using FluentValidation;
using FluentValidation.Results; using FluentValidation.Results;
using Newtonsoft.Json;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
namespace NzbDrone.Core.Indexers.Omgwtfnzbs namespace NzbDrone.Core.Indexers.Omgwtfnzbs
{ {
public class OmgwtfnzbsSettingsValidator : AbstractValidator<OmgwtfnzbsSettings>
{
public OmgwtfnzbsSettingsValidator()
{
RuleFor(c => c.Username).NotEmpty();
RuleFor(c => c.ApiKey).NotEmpty();
}
}
public class OmgwtfnzbsSettings : IIndexerSetting public class OmgwtfnzbsSettings : IIndexerSetting
{ {
// public OmgwtfnzbsSettings() private static readonly OmgwtfnzbsSettingsValidator Validator = new OmgwtfnzbsSettingsValidator();
// {
// RuleFor(c => c.Username).NotEmpty();
// RuleFor(c => c.ApiKey).NotEmpty();
// }
[FieldDefinition(0, Label = "Username")] [FieldDefinition(0, Label = "Username")]
public String Username { get; set; } public String Username { get; set; }
@ -22,8 +26,7 @@ namespace NzbDrone.Core.Indexers.Omgwtfnzbs
public ValidationResult Validate() public ValidationResult Validate()
{ {
return new ValidationResult(); return Validator.Validate(this);
//return Validate(this);
} }
} }
} }

View File

@ -210,6 +210,7 @@
<Compile Include="Indexers\IndexerSettingUpdatedEvent.cs" /> <Compile Include="Indexers\IndexerSettingUpdatedEvent.cs" />
<Compile Include="Indexers\IndexerWithSetting.cs" /> <Compile Include="Indexers\IndexerWithSetting.cs" />
<Compile Include="Indexers\Newznab\SizeParsingException.cs" /> <Compile Include="Indexers\Newznab\SizeParsingException.cs" />
<Compile Include="Indexers\NullSetting.cs" />
<Compile Include="Indexers\RssSyncCommand.cs" /> <Compile Include="Indexers\RssSyncCommand.cs" />
<Compile Include="Indexers\XElementExtensions.cs" /> <Compile Include="Indexers\XElementExtensions.cs" />
<Compile Include="Instrumentation\Commands\ClearLogCommand.cs" /> <Compile Include="Instrumentation\Commands\ClearLogCommand.cs" />