Browse Source

Allow multiple errors in reject.

pull/978/head
Sebastian 4 years ago
parent
commit
2ae60f56bd
  1. 43
      backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptOperations.cs
  2. 25
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/Scripting/JintScriptEngineHelperTests.cs

43
backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptOperations.cs

@ -1,4 +1,4 @@
// ========================================================================== // ==========================================================================
// Squidex Headless CMS // Squidex Headless CMS
// ========================================================================== // ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt) // Copyright (c) Squidex UG (haftungsbeschraenkt)
@ -6,6 +6,7 @@
// ========================================================================== // ==========================================================================
using Jint; using Jint;
using Jint.Native;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Squidex.Infrastructure.Translations; using Squidex.Infrastructure.Translations;
using Squidex.Infrastructure.Validation; using Squidex.Infrastructure.Validation;
@ -16,18 +17,50 @@ internal static class ScriptOperations
{ {
private delegate void MessageDelegate(string? message); private delegate void MessageDelegate(string? message);
private static readonly MessageDelegate Disallow = message => private delegate void MessageJsonDelegate(string? message);
private static readonly Action<string> Disallow = message =>
{ {
message = !string.IsNullOrWhiteSpace(message) ? message : T.Get("common.jsNotAllowed"); message = !string.IsNullOrWhiteSpace(message) ? message : T.Get("common.jsNotAllowed");
throw new DomainForbiddenException(message); throw new DomainForbiddenException(message);
}; };
private static readonly MessageDelegate Reject = message => private static readonly Action<JsValue> Reject = message =>
{ {
message = !string.IsNullOrWhiteSpace(message) ? message : T.Get("common.jsRejected"); var errors = new List<ValidationError>();
void AddError(JsString message)
{
var text = message.ToString();
if (!string.IsNullOrWhiteSpace(text))
{
errors.Add(new ValidationError(text));
}
}
if (message is JsString typed)
{
AddError(typed);
}
else if (message is JsArray jsArray)
{
foreach (var item in jsArray)
{
if (item is JsString typedItem)
{
AddError(typedItem);
}
}
}
if (errors.Count == 0)
{
errors.Add(new ValidationError(T.Get("common.jsRejected")));
}
throw new ValidationException(message); throw new ValidationException(errors);
}; };
public static Engine AddDisallow(this Engine engine) public static Engine AddDisallow(this Engine engine)

25
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/Scripting/JintScriptEngineHelperTests.cs

@ -279,12 +279,33 @@ public class JintScriptEngineHelperTests : IClassFixture<TranslationsFixture>
}; };
const string script = @" const string script = @"
reject('Not valid') reject('Error1')
"; ";
var ex = await Assert.ThrowsAsync<ValidationException>(() => sut.ExecuteAsync(vars, script, options)); var ex = await Assert.ThrowsAsync<ValidationException>(() => sut.ExecuteAsync(vars, script, options));
Assert.Equal("Not valid", ex.Errors.Single().Message); Assert.Equal(new[] { "Error1" }, ex.Errors.Select(x => x.Message).ToArray());
}
[Fact]
public async Task Should_throw_validation_exception_if_calling_reject_with_messages()
{
var options = new ScriptOptions
{
CanReject = true
};
var vars = new ScriptVars
{
};
const string script = @"
reject(['Error1', 'Error2'])
";
var ex = await Assert.ThrowsAsync<ValidationException>(() => sut.ExecuteAsync(vars, script, options));
Assert.Equal(new[] { "Error1", "Error2" }, ex.Errors.Select(x => x.Message).ToArray());
} }
[Fact] [Fact]

Loading…
Cancel
Save