Skip to content

Forward port of #19045 (Added custom RichTextRegexValidator to validate markup instead of JSON) #19280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@

builder.Services.AddSingleton<IRichTextRequiredValidator, RichTextRequiredValidator>();

// same as newing one up and required for the RichTextRegexValidator
builder.Services.AddTransient<RegexValidator>();
builder.Services.AddSingleton<IRichTextRegexValidator, RichTextRegexValidator>();

Check warning on line 254 in src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

❌ Getting worse: Large Method

AddCoreInitialServices increases from 118 to 120 lines of code, threshold = 70. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in src/Umbraco.Infrastructure/PropertyEditors/RichTextPropertyEditor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 40.68% to 40.00%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
// See LICENSE for more details.

using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -93,6 +93,7 @@
private readonly RichTextEditorPastedImages _pastedImages;
private readonly IJsonSerializer _jsonSerializer;
private readonly IRichTextRequiredValidator _richTextRequiredValidator;
private readonly IRichTextRegexValidator _richTextRegexValidator;
private readonly ILogger<RichTextPropertyValueEditor> _logger;

public RichTextPropertyValueEditor(
Expand All @@ -111,6 +112,7 @@
IPropertyValidationService propertyValidationService,
DataValueReferenceFactoryCollection dataValueReferenceFactoryCollection,
IRichTextRequiredValidator richTextRequiredValidator,
IRichTextRegexValidator richTextRegexValidator,
BlockEditorVarianceHandler blockEditorVarianceHandler,
ILanguageService languageService,
IIOHelper ioHelper)
Expand All @@ -122,6 +124,7 @@
_pastedImages = pastedImages;
_htmlSanitizer = htmlSanitizer;
_richTextRequiredValidator = richTextRequiredValidator;
_richTextRegexValidator = richTextRegexValidator;
_jsonSerializer = jsonSerializer;
_logger = logger;

Expand All @@ -131,6 +134,8 @@

public override IValueRequiredValidator RequiredValidator => _richTextRequiredValidator;

public override IValueFormatValidator FormatValidator => _richTextRegexValidator;

protected override RichTextBlockValue CreateWithLayout(IEnumerable<RichTextBlockLayoutItem> layout) => new(layout);

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Umbraco.Cms.Core.PropertyEditors.Validators;

internal interface IRichTextRegexValidator : IValueFormatValidator
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Core.PropertyEditors.Validators;

internal class RichTextRegexValidator : IRichTextRegexValidator
{
private readonly RegexValidator _regexValidator;
private readonly IJsonSerializer _jsonSerializer;
private readonly ILogger<RichTextRegexValidator> _logger;

public RichTextRegexValidator(
IJsonSerializer jsonSerializer,
ILogger<RichTextRegexValidator> logger,
RegexValidator regexValidator)
{
_jsonSerializer = jsonSerializer;
_logger = logger;
_regexValidator = regexValidator;
}

public IEnumerable<ValidationResult> ValidateFormat(object? value, string? valueType, string format) => _regexValidator.ValidateFormat(GetValue(value), valueType, format);

private object? GetValue(object? value) =>
RichTextPropertyEditorHelper.TryParseRichTextEditorValue(value, _jsonSerializer, _logger, out RichTextEditorValue? richTextEditorValue)
? richTextEditorValue?.Markup
: value;
}