using System.Text.RegularExpressions; using FluentValidation; using FluentValidation.Validators; using NzbDrone.Core.Parser; namespace NzbDrone.Core.Validation { public static class RuleBuilderExtensions { public static IRuleBuilderOptions ValidId(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new GreaterThanValidator(0)); } public static IRuleBuilderOptions IsZero(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new EqualValidator(0)); } public static IRuleBuilderOptions HaveHttpProtocol(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new RegularExpressionValidator("^https?://", RegexOptions.IgnoreCase)).WithMessage("must start with http:// or https://"); } public static IRuleBuilderOptions ValidHost(this IRuleBuilder ruleBuilder) { ruleBuilder.SetValidator(new NotEmptyValidator(null)); return ruleBuilder.SetValidator(new RegularExpressionValidator("^[-a-z0-9.]+$", RegexOptions.IgnoreCase)).WithMessage("must be valid Host without http://"); } public static IRuleBuilderOptions ValidRootUrl(this IRuleBuilder ruleBuilder) { ruleBuilder.SetValidator(new NotEmptyValidator(null)); return ruleBuilder.SetValidator(new RegularExpressionValidator("^https?://[-a-z0-9.]+", RegexOptions.IgnoreCase)).WithMessage("must be valid URL that starts with http(s)://"); } public static IRuleBuilderOptions ValidUrlBase(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new RegularExpressionValidator(@"^(?!\/?https?://[-a-z0-9.]+)", RegexOptions.IgnoreCase)).WithMessage("Must be a valid URL path (ie: '/sonarr')"); } public static IRuleBuilderOptions ValidPort(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new InclusiveBetweenValidator(1, 65535)) .Must(x => { if (x <= 1024) { if (x == 80 || x == 443) { return true; } return false; } return true; }); } public static IRuleBuilderOptions ValidLanguage(this IRuleBuilder ruleBuilder) { return ruleBuilder.SetValidator(new LanguageValidator()); } public static IRuleBuilderOptions AsWarning(this IRuleBuilderOptions ruleBuilder) { return ruleBuilder.WithState(v => NzbDroneValidationState.Warning); } } }