< Summary

Information
Class: Elsa.Workflows.OutputConverterSettingsValidator
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/OutputConverterSettingsValidator.cs
Line coverage
63%
Covered lines: 14
Uncovered lines: 8
Coverable lines: 22
Total lines: 60
Line coverage: 63.6%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor()100%11100%
Validate(...)75%251666.66%
CreateSchemaValidationPlan(...)100%1150%
get_Schema()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/OutputConverterSettingsValidator.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Text.Json;
 3using Elsa.Workflows.Models;
 4using Json.Schema;
 5
 6namespace Elsa.Workflows;
 7
 8/// <inheritdoc />
 9public class OutputConverterSettingsValidator : IOutputConverterSettingsValidator
 10{
 011    private static readonly JsonElement EmptySettings = JsonDocument.Parse("{}").RootElement.Clone();
 13812    private readonly ConcurrentDictionary<string, SchemaValidationPlan> _schemaValidationPlans = new(StringComparer.Ordi
 13
 14    /// <inheritdoc />
 15    public IReadOnlyCollection<string> Validate(
 16        OutputConverterDescriptor descriptor,
 17        IOutputConverter converter,
 18        JsonElement? settings)
 19    {
 1220        var errors = new List<string>();
 21
 1222        if (settings is { ValueKind: not JsonValueKind.Object })
 023            errors.Add("Converter settings must be a JSON object.");
 24
 1225        if (descriptor.SettingsSchema is { } schemaElement)
 26        {
 227            var plan = _schemaValidationPlans.GetOrAdd(descriptor.Id, _ => CreateSchemaValidationPlan(schemaElement));
 28
 129            if (plan.Schema == null)
 030                errors.Add("The registered converter settings schema is invalid.");
 131            else if (!plan.Schema.Evaluate(settings ?? EmptySettings).IsValid)
 132                errors.Add("Converter settings do not satisfy the registered JSON Schema.");
 33        }
 34
 35        try
 36        {
 1237            errors.AddRange(converter.ValidateSettings(settings?.Clone()) ?? []);
 1238        }
 039        catch
 40        {
 041            errors.Add("Converter-owned settings validation failed.");
 042        }
 43
 1244        return errors;
 45    }
 46
 47    private static SchemaValidationPlan CreateSchemaValidationPlan(JsonElement schemaElement)
 48    {
 49        try
 50        {
 151            return new(JsonSchema.Build(schemaElement));
 52        }
 053        catch
 54        {
 055            return new(null);
 56        }
 157    }
 58
 359    private sealed record SchemaValidationPlan(JsonSchema? Schema);
 60}