Browse Source

Improved value converter.

pull/478/head
Sebastian 6 years ago
parent
commit
ef670e069c
  1. 25
      backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/ContentConverter.cs
  2. 7
      backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/FieldConverters.cs
  3. 7
      backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/ValueConverters.cs
  4. 88
      backend/src/Squidex.Domain.Apps.Core.Operations/ValidateContent/JsonValueConverter.cs
  5. 11
      backend/src/Squidex.Domain.Apps.Core.Operations/ValidateContent/Validators/FieldValidator.cs
  6. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/ArrayFieldTests.cs
  7. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/AssetsFieldTests.cs
  8. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/BooleanFieldTests.cs
  9. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/DateTimeFieldTests.cs
  10. 6
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/GeolocationFieldTests.cs
  11. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/NumberFieldTests.cs
  12. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/ReferencesFieldTests.cs
  13. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/TagsFieldTests.cs

25
backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/ContentConverter.cs

@ -20,31 +20,6 @@ namespace Squidex.Domain.Apps.Core.ConvertContent
private static readonly Func<IRootField, string> KeyNameResolver = f => f.Name;
private static readonly Func<IRootField, long> KeyIdResolver = f => f.Id;
public static string ToFullText<T>(this ContentData<T> data, int maxTotalLength = 1024 * 1024, int maxFieldLength = 1000, string separator = " ") where T : notnull
{
var stringBuilder = new StringBuilder();
foreach (var fieldValue in data.Values)
{
if (fieldValue != null)
{
foreach (var value in fieldValue.Values)
{
AppendText(value, stringBuilder, maxFieldLength, separator, false);
}
}
}
var result = stringBuilder.ToString();
if (result.Length > maxTotalLength)
{
result = result.Substring(0, maxTotalLength);
}
return result;
}
private static void AppendText(IJsonValue value, StringBuilder stringBuilder, int maxFieldLength, string separator, bool allowObjects)
{
if (value.Type == JsonValueType.String)

7
backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/FieldConverters.cs

@ -68,7 +68,12 @@ namespace Squidex.Domain.Apps.Core.ConvertContent
try
{
JsonValueConverter.ConvertValue(field, value);
var (_, error) = JsonValueConverter.ConvertValue(field, value);
if (error != null)
{
return null;
}
}
catch
{

7
backend/src/Squidex.Domain.Apps.Core.Operations/ConvertContent/ValueConverters.cs

@ -64,7 +64,12 @@ namespace Squidex.Domain.Apps.Core.ConvertContent
try
{
JsonValueConverter.ConvertValue(field, value);
var (_, error) = JsonValueConverter.ConvertValue(field, value);
if (error != null)
{
return Value.Unset;
}
}
catch
{

88
backend/src/Squidex.Domain.Apps.Core.Operations/ValidateContent/JsonValueConverter.cs

@ -14,7 +14,7 @@ using Squidex.Infrastructure.Validation;
namespace Squidex.Domain.Apps.Core.ValidateContent
{
public sealed class JsonValueConverter : IFieldVisitor<object>
public sealed class JsonValueConverter : IFieldVisitor<(object? Result, JsonError? Error)>
{
private readonly IJsonValue value;
@ -23,67 +23,67 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
this.value = value;
}
public static object ConvertValue(IField field, IJsonValue json)
public static (object? Result, JsonError? Error) ConvertValue(IField field, IJsonValue json)
{
return field.Accept(new JsonValueConverter(json));
}
public object Visit(IArrayField field)
public (object? Result, JsonError? Error) Visit(IArrayField field)
{
return ConvertToObjectList();
}
public object Visit(IField<AssetsFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<AssetsFieldProperties> field)
{
return ConvertToGuidList();
}
public object Visit(IField<ReferencesFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<ReferencesFieldProperties> field)
{
return ConvertToGuidList();
}
public object Visit(IField<TagsFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<TagsFieldProperties> field)
{
return ConvertToStringList();
}
public object Visit(IField<BooleanFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<BooleanFieldProperties> field)
{
if (value is JsonScalar<bool> b)
{
return b.Value;
return (b.Value, null);
}
throw new InvalidCastException("Invalid json type, expected boolean.");
return (null, new JsonError("Invalid json type, expected boolean."));
}
public object Visit(IField<NumberFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<NumberFieldProperties> field)
{
if (value is JsonScalar<double> b)
if (value is JsonScalar<double> n)
{
return b.Value;
return (n.Value, null);
}
throw new InvalidCastException("Invalid json type, expected number.");
return (null, new JsonError("Invalid json type, expected number."));
}
public object Visit(IField<StringFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<StringFieldProperties> field)
{
if (value is JsonScalar<string> b)
if (value is JsonScalar<string> s)
{
return b.Value;
return (s.Value, null);
}
throw new InvalidCastException("Invalid json type, expected string.");
return (null, new JsonError("Invalid json type, expected string."));
}
public object Visit(IField<UIFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<UIFieldProperties> field)
{
return value;
return (value, null);
}
public object Visit(IField<DateTimeFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<DateTimeFieldProperties> field)
{
if (value.Type == JsonValueType.String)
{
@ -94,13 +94,13 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
throw parseResult.Exception;
}
return parseResult.Value;
return (parseResult.Value, null);
}
throw new InvalidCastException("Invalid json type, expected string.");
return (null, new JsonError("Invalid json type, expected string."));
}
public object Visit(IField<GeolocationFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<GeolocationFieldProperties> field)
{
if (value is JsonObject geolocation)
{
@ -109,7 +109,7 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
if (!string.Equals(propertyName, "latitude", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(propertyName, "longitude", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidCastException("Geolocation can only have latitude and longitude property.");
return (null, new JsonError("Geolocation can only have latitude and longitude property."));
}
}
@ -119,12 +119,12 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
if (!lat.IsBetween(-90, 90))
{
throw new InvalidCastException("Latitude must be between -90 and 90.");
return (null, new JsonError("Latitude must be between -90 and 90."));
}
}
else
{
throw new InvalidCastException("Invalid json type, expected latitude/longitude object.");
return (null, new JsonError("Invalid json type, expected latitude/longitude object."));
}
if (geolocation.TryGetValue("longitude", out var lonValue) && lonValue is JsonScalar<double> lonNumber)
@ -133,26 +133,26 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
if (!lon.IsBetween(-180, 180))
{
throw new InvalidCastException("Longitude must be between -180 and 180.");
return (null, new JsonError("Longitude must be between -180 and 180."));
}
}
else
{
throw new InvalidCastException("Invalid json type, expected latitude/longitude object.");
return (null, new JsonError("Invalid json type, expected latitude/longitude object."));
}
return value;
return (value, null);
}
throw new InvalidCastException("Invalid json type, expected latitude/longitude object.");
return (null, new JsonError("Invalid json type, expected latitude/longitude object."));
}
public object Visit(IField<JsonFieldProperties> field)
public (object? Result, JsonError? Error) Visit(IField<JsonFieldProperties> field)
{
return value;
return (value, null);
}
private object ConvertToGuidList()
private (object? Result, JsonError? Error) ConvertToGuidList()
{
if (value is JsonArray array)
{
@ -166,17 +166,17 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
}
else
{
throw new InvalidCastException("Invalid json type, expected array of guid strings.");
return (null, new JsonError("Invalid json type, expected array of guid strings."));
}
}
return result;
return (result, null);
}
throw new InvalidCastException("Invalid json type, expected array of guid strings.");
return (null, new JsonError("Invalid json type, expected array of guid strings."));
}
private object ConvertToStringList()
private (object? Result, JsonError? Error) ConvertToStringList()
{
if (value is JsonArray array)
{
@ -194,17 +194,17 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
}
else
{
throw new InvalidCastException("Invalid json type, expected array of strings.");
return (null, new JsonError("Invalid json type, expected array of strings."));
}
}
return result;
return (result, null);
}
throw new InvalidCastException("Invalid json type, expected array of strings.");
return (null, new JsonError("Invalid json type, expected array of strings."));
}
private object ConvertToObjectList()
private (object? Result, JsonError? Error) ConvertToObjectList()
{
if (value is JsonArray array)
{
@ -218,14 +218,14 @@ namespace Squidex.Domain.Apps.Core.ValidateContent
}
else
{
throw new InvalidCastException("Invalid json type, expected array of objects.");
return (null, new JsonError("Invalid json type, expected array of objects."));
}
}
return result;
return (result, null);
}
throw new InvalidCastException("Invalid json type, expected array of objects.");
return (null, new JsonError("Invalid json type, expected array of objects."));
}
}
}

11
backend/src/Squidex.Domain.Apps.Core.Operations/ValidateContent/Validators/FieldValidator.cs

@ -42,7 +42,16 @@ namespace Squidex.Domain.Apps.Core.ValidateContent.Validators
}
else
{
typedValue = JsonValueConverter.ConvertValue(field, jsonValue);
var (json, error) = JsonValueConverter.ConvertValue(field, jsonValue);
if (error != null)
{
addError(context.Path, error.Error);
}
else
{
typedValue = json;
}
}
}

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/ArrayFieldTests.cs

@ -87,7 +87,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("invalid"), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected array of objects." });
}
[Fact]

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/AssetsFieldTests.cs

@ -163,7 +163,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("invalid"), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected array of guid strings." });
}
[Fact]

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/BooleanFieldTests.cs

@ -65,7 +65,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("Invalid"), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected boolean." });
}
private static IJsonValue CreateValue(bool? v)

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/DateTimeFieldTests.cs

@ -90,7 +90,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create(123), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected string." });
}
private static Instant FutureDays(int days)

6
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/GeolocationFieldTests.cs

@ -62,7 +62,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(geolocation, errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Latitude must be between -90 and 90." });
}
[Fact]
@ -77,7 +77,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(geolocation, errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Longitude must be between -180 and 180." });
}
[Fact]
@ -93,7 +93,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(geolocation, errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Geolocation can only have latitude and longitude property." });
}
[Fact]

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/NumberFieldTests.cs

@ -90,7 +90,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("Invalid"), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected number." });
}
[Fact]

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/ReferencesFieldTests.cs

@ -112,7 +112,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("invalid"), errors, Context());
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected array of guid strings." });
}
[Fact]

2
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/TagsFieldTests.cs

@ -110,7 +110,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
await sut.ValidateAsync(JsonValue.Create("invalid"), errors);
errors.Should().BeEquivalentTo(
new[] { "Not a valid value." });
new[] { "Invalid json type, expected array of strings." });
}
[Fact]

Loading…
Cancel
Save