Remove manifest validators (#16027)
* Remove manifest validators * Remove "Configuration" from RegexValidator
This commit is contained in:
@@ -10,7 +10,6 @@ using Umbraco.Cms.Core.HealthChecks.NotificationMethods;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
using Umbraco.Cms.Core.Media.EmbedProviders;
|
||||
using Umbraco.Cms.Core.PropertyEditors;
|
||||
using Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
using Umbraco.Cms.Core.Routing;
|
||||
using Umbraco.Cms.Core.Snippets;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
@@ -69,13 +68,6 @@ public static partial class UmbracoBuilderExtensions
|
||||
builder.DataValueReferenceFactories();
|
||||
builder.PropertyValueConverters().Append(builder.TypeLoader.GetTypes<IPropertyValueConverter>());
|
||||
builder.UrlSegmentProviders().Append<DefaultUrlSegmentProvider>();
|
||||
builder.ManifestValueValidators()
|
||||
.Add<RequiredValidator>()
|
||||
.Add<RegexValidator>()
|
||||
.Add<DelimitedValueValidator>()
|
||||
.Add<EmailValidator>()
|
||||
.Add<IntegerValidator>()
|
||||
.Add<DecimalValidator>();
|
||||
builder.MediaUrlGenerators();
|
||||
// register OEmbed providers - no type scanning - all explicit opt-in of adding types, IEmbedProvider is not IDiscoverable
|
||||
builder.EmbedProviders()
|
||||
@@ -212,13 +204,6 @@ public static partial class UmbracoBuilderExtensions
|
||||
public static UrlSegmentProviderCollectionBuilder UrlSegmentProviders(this IUmbracoBuilder builder)
|
||||
=> builder.WithCollectionBuilder<UrlSegmentProviderCollectionBuilder>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the validators collection builder.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder.</param>
|
||||
internal static ManifestValueValidatorCollectionBuilder ManifestValueValidators(this IUmbracoBuilder builder)
|
||||
=> builder.WithCollectionBuilder<ManifestValueValidatorCollectionBuilder>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the content finders collection builder.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Umbraco.Cms.Core.PropertyEditors;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a value validator that can be referenced in a manifest.
|
||||
/// </summary>
|
||||
/// <remarks>If the manifest can be configured, then it should expose a Configuration property.</remarks>
|
||||
public interface IManifestValueValidator : IValueValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the validator.
|
||||
/// </summary>
|
||||
string ValidationName { get; }
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using Umbraco.Cms.Core.Composing;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors;
|
||||
|
||||
public class ManifestValueValidatorCollection : BuilderCollectionBase<IManifestValueValidator>
|
||||
{
|
||||
public ManifestValueValidatorCollection(Func<IEnumerable<IManifestValueValidator>> items)
|
||||
: base(items)
|
||||
{
|
||||
}
|
||||
|
||||
public IManifestValueValidator? Create(string name)
|
||||
{
|
||||
IManifestValueValidator v = GetByName(name);
|
||||
|
||||
// TODO: what is this exactly?
|
||||
// we cannot return this instance, need to clone it?
|
||||
return (IManifestValueValidator?)Activator.CreateInstance(v.GetType()); // ouch
|
||||
}
|
||||
|
||||
public IManifestValueValidator GetByName(string name)
|
||||
{
|
||||
IManifestValueValidator? v = this.FirstOrDefault(x => x.ValidationName.InvariantEquals(name));
|
||||
if (v == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not find a validator named \"{name}\".");
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using Umbraco.Cms.Core.Composing;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors;
|
||||
|
||||
public class ManifestValueValidatorCollectionBuilder : SetCollectionBuilderBase<ManifestValueValidatorCollectionBuilder, ManifestValueValidatorCollection, IManifestValueValidator>
|
||||
{
|
||||
protected override ManifestValueValidatorCollectionBuilder This => this;
|
||||
}
|
||||
@@ -6,11 +6,8 @@ namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
/// <summary>
|
||||
/// A validator that validates that the value is a valid decimal
|
||||
/// </summary>
|
||||
public sealed class DecimalValidator : IManifestValueValidator
|
||||
public sealed class DecimalValidator : IValueValidator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string ValidationName => "Decimal";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// A validator that validates a delimited set of values against a common regex
|
||||
/// </summary>
|
||||
public sealed class DelimitedValueValidator : IManifestValueValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the configuration, when parsed as <see cref="IManifestValueValidator" />.
|
||||
/// </summary>
|
||||
public DelimitedValueValidatorConfig? Configuration { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string ValidationName => "Delimited";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
// TODO: localize these!
|
||||
if (value != null)
|
||||
{
|
||||
var delimiter = Configuration?.Delimiter ?? ",";
|
||||
Regex? regex = Configuration?.Pattern != null ? new Regex(Configuration.Pattern) : null;
|
||||
|
||||
var stringVal = value.ToString();
|
||||
var split = stringVal!.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var i = 0; i < split.Length; i++)
|
||||
{
|
||||
var s = split[i];
|
||||
|
||||
// next if we have a regex statement validate with that
|
||||
if (regex != null)
|
||||
{
|
||||
if (regex.IsMatch(s) == false)
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"The item at index " + i + " did not match the expression " + regex,
|
||||
new[]
|
||||
{
|
||||
// make the field name called 'value0' where 0 is the index
|
||||
"value" + i,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DelimitedValueValidatorConfig
|
||||
{
|
||||
public string? Delimiter { get; set; }
|
||||
|
||||
public string? Pattern { get; set; }
|
||||
}
|
||||
@@ -5,11 +5,8 @@ namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
/// <summary>
|
||||
/// A validator that validates an email address
|
||||
/// </summary>
|
||||
public sealed class EmailValidator : IManifestValueValidator
|
||||
public sealed class EmailValidator : IValueValidator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string ValidationName => "Email";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
|
||||
@@ -6,11 +6,8 @@ namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
/// <summary>
|
||||
/// A validator that validates that the value is a valid integer
|
||||
/// </summary>
|
||||
public sealed class IntegerValidator : IManifestValueValidator
|
||||
public sealed class IntegerValidator : IValueValidator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string ValidationName => "Integer";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
/// <summary>
|
||||
/// A validator that validates that the value against a regular expression.
|
||||
/// </summary>
|
||||
public sealed class RegexValidator : IValueFormatValidator, IManifestValueValidator
|
||||
public sealed class RegexValidator : IValueFormatValidator, IValueValidator
|
||||
{
|
||||
private string _regex;
|
||||
|
||||
@@ -31,9 +31,7 @@ public sealed class RegexValidator : IValueFormatValidator, IManifestValueValida
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Use this constructor when the validator is used as an <see cref="IValueFormatValidator" />,
|
||||
/// and the regular expression is supplied at validation time. This constructor is also used when
|
||||
/// the validator is used as an <see cref="IManifestValueValidator" /> and the regular expression
|
||||
/// is supplied via the <see cref="Configuration" /> method.
|
||||
/// and the regular expression is supplied at validation time.
|
||||
/// </remarks>
|
||||
public RegexValidator()
|
||||
: this(string.Empty)
|
||||
@@ -50,33 +48,6 @@ public sealed class RegexValidator : IValueFormatValidator, IManifestValueValida
|
||||
public RegexValidator(string regex)
|
||||
=> _regex = regex;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the configuration, when parsed as <see cref="IManifestValueValidator" />.
|
||||
/// </summary>
|
||||
public string Configuration
|
||||
{
|
||||
get => _regex;
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"Value can't be empty or consist only of white-space characters.",
|
||||
nameof(value));
|
||||
}
|
||||
|
||||
_regex = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IManifestValueValidator.ValidationName" />
|
||||
public string ValidationName => "Regex";
|
||||
|
||||
/// <inheritdoc cref="IValueValidator.Validate" />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.Validators;
|
||||
/// <summary>
|
||||
/// A validator that validates that the value is not null or empty (if it is a string)
|
||||
/// </summary>
|
||||
public sealed class RequiredValidator : IValueRequiredValidator, IManifestValueValidator
|
||||
public sealed class RequiredValidator : IValueRequiredValidator, IValueValidator
|
||||
{
|
||||
[Obsolete($"Use the constructor that does not accept {nameof(ILocalizedTextService)}. Will be removed in V15.")]
|
||||
public RequiredValidator(ILocalizedTextService textService)
|
||||
@@ -19,9 +19,6 @@ public sealed class RequiredValidator : IValueRequiredValidator, IManifestValueV
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IManifestValueValidator.ValidationName" />
|
||||
public string ValidationName => "Required";
|
||||
|
||||
/// <inheritdoc cref="IValueValidator.Validate" />
|
||||
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration) =>
|
||||
ValidateRequired(value, valueType);
|
||||
|
||||
Reference in New Issue
Block a user