Removed custom validation of configuration enum values, reverting to framework based validation when binding direct to the enum values.

This commit is contained in:
Andy Butland
2020-09-21 21:20:46 +02:00
parent 1bd22d72c2
commit fe158ec7d9
28 changed files with 30 additions and 251 deletions

View File

@@ -20,27 +20,7 @@ namespace Umbraco.Core.Configuration.Models
public string PreviewBadge { get; set; } = DefaultPreviewBadge;
// We could bind the enum MacroErrorsBehaviour directly from configuration, but we're doing so
// via this string to allow for validation to occur on start-up that the configured value does
// match one of the enum values.
// Without this it'll fail on first use, and we'd have less control over the error message.
internal string MacroErrors { get; set; } = MacroErrorBehaviour.Inline.ToString();
public MacroErrorBehaviour MacroErrorsBehaviour
{
get
{
if (Enum.TryParse<MacroErrorBehaviour>(MacroErrors, true, out var value))
{
return value;
}
// We need to return somethhing valid here as this property is evalulated during start-up, and if there's an error
// in the configured value it won't be parsed to the enum.
// At run-time though this default won't be used, as an invalid value will be picked up by ContentSettingsValidator.
return MacroErrorBehaviour.Inline;
}
}
public MacroErrorBehaviour MacroErrors { get; set; } = MacroErrorBehaviour.Inline;
public IEnumerable<string> DisallowedUploadFiles { get; set; } = new[] { "ashx", "aspx", "ascx", "config", "cshtml", "vbhtml", "asmx", "air", "axd" };

View File

@@ -1,5 +1,4 @@
using System;
using Umbraco.Core.Configuration.Models.Validation;
namespace Umbraco.Core.Configuration.Models
{
@@ -7,27 +6,10 @@ namespace Umbraco.Core.Configuration.Models
{
public string ApplicationVirtualPath { get; set; }
// See note on ContentSettings.MacroErrors
internal string LocalTempStorageLocation { get; set; } = LocalTempStorage.Default.ToString();
/// <summary>
/// Gets the configuration for the location of temporary files.
/// </summary>
public LocalTempStorage LocalTempStorageLocationValue
{
get
{
if (Enum.TryParse<LocalTempStorage>(LocalTempStorageLocation, true, out var value))
{
return value;
}
// We need to return somethhing valid here as this property is evalulated during start-up, and if there's an error
// in the configured value it won't be parsed to the enum.
// At run-time though this default won't be used, as an invalid value will be picked up by HostingSettingsValidator.
return LocalTempStorage.Default;
}
}
public LocalTempStorage LocalTempStorageLocation { get; set; } = LocalTempStorage.Default;
/// <summary>
/// Gets a value indicating whether umbraco is running in [debug mode].

View File

@@ -1,6 +1,4 @@
using System;
using Umbraco.Configuration;
using Umbraco.Core.Configuration.Models.Validation;
using Umbraco.Configuration;
namespace Umbraco.Core.Configuration.Models
{
@@ -20,27 +18,10 @@ namespace Umbraco.Core.Configuration.Models
/// </remarks>
public bool Enable { get; set; } = false;
// See note on ContentSettings.MacroErrors
internal string ModelsMode { get; set; } = Configuration.ModelsMode.Nothing.ToString();
/// <summary>
/// Gets the models mode.
/// </summary>
public ModelsMode ModelsModeValue
{
get
{
if (Enum.TryParse<ModelsMode>(ModelsMode, true, out var value))
{
return value;
}
// We need to return somethhing valid here as this property is evalulated during start-up, and if there's an error
// in the configured value it won't be parsed to the enum.
// At run-time though this default won't be used, as an invalid value will be picked up by ModelsBuilderSettingsValidator.
return Configuration.ModelsMode.Nothing;
}
}
public ModelsMode ModelsMode { get; set; } = ModelsMode.Nothing;
/// <summary>
/// Gets the models namespace.
@@ -70,7 +51,7 @@ namespace Umbraco.Core.Configuration.Models
set
{
if (!ModelsModeValue.IsLive())
if (!ModelsMode.IsLive())
{
_flagOutOfDateModels = false;
}

View File

@@ -17,24 +17,7 @@ namespace Umbraco.Core.Configuration.Models
public string PickupDirectoryLocation { get; set; }
// See notes on ContentSettings.MacroErrors
internal string DeliveryMethod { get; set; } = SmtpDeliveryMethod.Network.ToString();
public SmtpDeliveryMethod DeliveryMethodValue
{
get
{
if (Enum.TryParse<SmtpDeliveryMethod>(DeliveryMethod, true, out var value))
{
return value;
}
// We need to return somethhing valid here as this property is evalulated during start-up, and if there's an error
// in the configured value it won't be parsed to the enum.
// At run-time though this default won't be used, as an invalid value will be picked up by GlobalSettingsValidator.
return SmtpDeliveryMethod.Network;
}
}
public SmtpDeliveryMethod DeliveryMethod { get; set; } = SmtpDeliveryMethod.Network;
public string Username { get; set; }

View File

@@ -18,12 +18,6 @@ namespace Umbraco.Core.Configuration.Models.Validation
return true;
}
public bool ValidateStringIsOneOfEnumValues(string configPath, string value, Type enumType, out string message)
{
var validValues = Enum.GetValues(enumType).OfType<object>().Select(x => x.ToString());
return ValidateStringIsOneOfValidValues(configPath, value, validValues, out message);
}
public bool ValidateCollection(string configPath, IEnumerable<ValidatableEntryBase> values, string validationDescription, out string message)
{
if (values.Any(x => !x.IsValid()))

View File

@@ -9,11 +9,6 @@ namespace Umbraco.Core.Configuration.Models.Validation
public ValidateOptionsResult Validate(string name, ContentSettings options)
{
string message;
if (!ValidateMacroErrors(options.MacroErrors, out message))
{
return ValidateOptionsResult.Fail(message);
}
if (!ValidateError404Collection(options.Error404Collection, out message))
{
return ValidateOptionsResult.Fail(message);
@@ -27,11 +22,6 @@ namespace Umbraco.Core.Configuration.Models.Validation
return ValidateOptionsResult.Success;
}
private bool ValidateMacroErrors(string value, out string message)
{
return ValidateStringIsOneOfEnumValues($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.MacroErrors)}", value, typeof(MacroErrorBehaviour), out message);
}
private bool ValidateError404Collection(IEnumerable<ContentErrorPage> values, out string message)
{
return ValidateCollection($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.Error404Collection)}", values, "Culture and one and only one of ContentId, ContentKey and ContentXPath must be specified for each entry", out message);

View File

@@ -1,5 +1,4 @@
using System.Net.Mail;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options;
namespace Umbraco.Core.Configuration.Models.Validation
{
@@ -18,8 +17,7 @@ namespace Umbraco.Core.Configuration.Models.Validation
private bool ValidateSmtpSetting(SmtpSettings value, out string message)
{
return ValidateOptionalEntry($"{Constants.Configuration.ConfigGlobal}:{nameof(GlobalSettings.Smtp)}", value, "A valid From email address is required", out message) &&
(value == null || ValidateStringIsOneOfEnumValues("Global:Smtp:DeliveryMethod", value.DeliveryMethod, typeof(SmtpDeliveryMethod), out message));
return ValidateOptionalEntry($"{Constants.Configuration.ConfigGlobal}:{nameof(GlobalSettings.Smtp)}", value, "A valid From email address is required", out message);
}
}
}

View File

@@ -1,22 +0,0 @@
using Microsoft.Extensions.Options;
namespace Umbraco.Core.Configuration.Models.Validation
{
public class HostingSettingsValidator : ConfigurationValidatorBase, IValidateOptions<HostingSettings>
{
public ValidateOptionsResult Validate(string name, HostingSettings options)
{
if (!ValidateLocalTempStorageLocation(options.LocalTempStorageLocation, out var message))
{
return ValidateOptionsResult.Fail(message);
}
return ValidateOptionsResult.Success;
}
private bool ValidateLocalTempStorageLocation(string value, out string message)
{
return ValidateStringIsOneOfEnumValues($"{Constants.Configuration.ConfigHosting}:{nameof(HostingSettings.LocalTempStorageLocation)}", value, typeof(LocalTempStorage), out message);
}
}
}

View File

@@ -1,22 +0,0 @@
using Microsoft.Extensions.Options;
namespace Umbraco.Core.Configuration.Models.Validation
{
public class ModelsBuilderSettingsValidator : ConfigurationValidatorBase, IValidateOptions<ModelsBuilderSettings>
{
public ValidateOptionsResult Validate(string name, ModelsBuilderSettings options)
{
if (!ValidateModelsMode(options.ModelsMode, out var message))
{
return ValidateOptionsResult.Fail(message);
}
return ValidateOptionsResult.Success;
}
private bool ValidateModelsMode(string value, out string message)
{
return ValidateStringIsOneOfEnumValues($"{Constants.Configuration.ConfigModelsBuilder}:{nameof(ModelsBuilderSettings.ModelsMode)}", value, typeof(ModelsMode), out message);
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Umbraco.Core.Configuration.Models
public bool DisableRedirectUrlTracking { get; set; } = false;
public string UrlProviderMode { get; set; } = UrlMode.Auto.ToString();
public UrlMode UrlProviderMode { get; set; } = UrlMode.Auto;
public string UmbracoApplicationUrl { get; set; }
}