Added configuration validation for content and request handler settings.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Umbraco.Core.Configuration.Models.Validation
|
||||
{
|
||||
public abstract class ConfigurationValidationBase
|
||||
{
|
||||
public bool ValidateStringIsOneOfValidValues(string configPath, string value, IEnumerable<string> validValues, out string message)
|
||||
{
|
||||
if (!validValues.InvariantContains(value))
|
||||
{
|
||||
message = $"Configuration entry {configPath} contains an invalid value '{value}', it should be one of the following: '{string.Join(", ", validValues)}'.";
|
||||
return false;
|
||||
}
|
||||
|
||||
message = string.Empty;
|
||||
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().ToFirstLowerInvariant());
|
||||
return ValidateStringIsOneOfValidValues(configPath, value, validValues, out message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Umbraco.Core.Macros;
|
||||
|
||||
namespace Umbraco.Core.Configuration.Models.Validation
|
||||
{
|
||||
public class ContentSettingsValidation : ConfigurationValidationBase, IValidateOptions<ContentSettings>
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
return ValidateOptionsResult.Success;
|
||||
}
|
||||
|
||||
private bool ValidateMacroErrors(string value, out string message)
|
||||
{
|
||||
return ValidateStringIsOneOfEnumValues("Content:MacroErrors", value, typeof(MacroErrorBehaviour), out message);
|
||||
}
|
||||
|
||||
private bool ValidateError404Collection(IEnumerable<ContentErrorPage> values, out string message)
|
||||
{
|
||||
if (values.Any(x => !x.IsValid()))
|
||||
{
|
||||
message = $"Configuration entry Content:Error404Collection contains one or more invalid values. Culture and one and only one of ContentId, ContentKey and ContentXPath must be specified for each entry.";
|
||||
return false;
|
||||
}
|
||||
|
||||
message = string.Empty;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Umbraco.Core.Configuration.Models.Validation
|
||||
{
|
||||
public class RequestHandlerSettingsValidation : ConfigurationValidationBase, IValidateOptions<RequestHandlerSettings>
|
||||
{
|
||||
public ValidateOptionsResult Validate(string name, RequestHandlerSettings options)
|
||||
{
|
||||
if (!ValidateConvertUrlsToAscii(options.ConvertUrlsToAscii, out var message))
|
||||
{
|
||||
return ValidateOptionsResult.Fail(message);
|
||||
}
|
||||
|
||||
return ValidateOptionsResult.Success;
|
||||
}
|
||||
|
||||
private bool ValidateConvertUrlsToAscii(string value, out string message)
|
||||
{
|
||||
var validValues = new[] { "try", "true", "false" };
|
||||
return ValidateStringIsOneOfValidValues("RequestHandler:ConvertUrlsToAscii", value, validValues, out message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user