Skip to content

Commit

Permalink
Fix rich text validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Roblinde committed Mar 16, 2019
1 parent 6e7e53b commit 9299c93
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Expand Up @@ -13,8 +13,8 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>4.0.0</Version>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<Version>4.1.0</Version>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand All @@ -24,7 +24,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="4.0.0" />
<PackageReference Include="contentful.csharp" Version="4.1.0" />
<PackageReference Include="gitlink" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
3 changes: 3 additions & 0 deletions Contentful.Core.Tests/ContentfulManagementClientTests.cs
Expand Up @@ -429,6 +429,9 @@ public async Task GetActivatedContentTypesShouldSerializeCorrectly()
Assert.Equal($"https://api.contentful.com/spaces/666/public/content_types", requestUrl);
Assert.Equal("someName", res.First().Name);
Assert.Equal(8, (res.First().Fields.First().Validations.First() as SizeValidator).Max);
Assert.Equal(8, ((res.First().Fields.Last().Validations.First() as NodesValidator).EmbeddedEntryInline.First() as SizeValidator).Max);
Assert.Equal(2, ((res.First().Fields.Last().Validations.First() as NodesValidator).EmbeddedEntryBlock.First() as SizeValidator).Min);
Assert.Equal(4, ((res.First().Fields.Last().Validations.First() as NodesValidator).EmbeddedEntryBlock.First() as SizeValidator).Max);
}

[Fact]
Expand Down
Expand Up @@ -94,7 +94,49 @@
"type": "Link",
"localized": false,
"required": false,
"validations": [],
"validations": [
{
"nodes": {
"entry-hyperlink": [
{
"linkContentType": [
"bob"
],
"message": "bob"
}
],
"embedded-entry-block": [
{
"size": {
"min": 2,
"max": 4
},
"message": "bob2"
},
{
"linkContentType": [
"bob"
],
"message": "bob3"
}
],
"embedded-entry-inline": [
{
"size": {
"max": 8
},
"message": "bob4"
},
{
"linkContentType": [
"bob"
],
"message": "bob5"
}
]
}
}
],
"disabled": false,
"omitted": false,
"linkType": "Asset"
Expand Down
21 changes: 21 additions & 0 deletions Contentful.Core.Tests/Models/Management/IFieldValidationTests.cs
Expand Up @@ -185,5 +185,26 @@ public void FileSizeValidatorShouldReturnCorrectJson(string message, int min, in
//Assert
Assert.Equal($@"{{""assetFileSize"":{{""min"":{expectedMin},""max"":{expectedMax}}},""message"":""{message}""}}", json);
}

[Fact]
public void NodesValidatorShouldReturnCorrectJson()
{
//Arrange
var validator = new NodesValidator();
validator.EmbeddedEntryBlock = new List<IFieldValidator>() {
new SizeValidator(2,4)
};
validator.EmbeddedEntryInline = new List<IFieldValidator>() {
new SizeValidator(8, null)
};
validator.EntryHyperlink = new List<IFieldValidator>() {
new SizeValidator(null, 3)
};
//Act
var created = validator.CreateValidator();
var json = JsonConvert.SerializeObject(created);
//Assert
Assert.Equal(@"{""nodes"":{""entry-hyperlink"":[{""size"":{""min"":null,""max"":3},""message"":null}],""embedded-entry-block"":[{""size"":{""min"":2,""max"":4},""message"":null}],""embedded-entry-inline"":[{""size"":{""min"":8,""max"":null},""message"":null}]}}", json);
}
}
}
22 changes: 21 additions & 1 deletion Contentful.Core/Configuration/ValidationsJsonConverter.cs
Expand Up @@ -34,7 +34,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
var jsonObject = JObject.Load(reader);

if (jsonObject.TryGetValue("size", out JToken jToken))
if (jsonObject.TryGetValue("size", out var jToken))
{
return new SizeValidator(
jToken["min"].ToNullableInt(),
Expand Down Expand Up @@ -125,6 +125,26 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return new ImageSizeValidator(minWidth, maxWidth, minHeight, maxHeight, jsonObject["message"]?.ToString());
}

if (jsonObject.TryGetValue("nodes", out jToken))
{
var validator = new NodesValidator();

if (jToken["entry-hyperlink"] != null)
{
validator.EntryHyperlink = jToken["entry-hyperlink"].ToObject<IEnumerable<IFieldValidator>>(serializer);
}
if (jToken["embedded-entry-block"] != null)
{
validator.EmbeddedEntryBlock = jToken["embedded-entry-block"].ToObject<IEnumerable<IFieldValidator>>(serializer);
}
if (jToken["embedded-entry-inline"] != null)
{
validator.EmbeddedEntryInline = jToken["embedded-entry-inline"].ToObject<IEnumerable<IFieldValidator>>(serializer);
}

return validator;
}

return Activator.CreateInstance(objectType);
}

Expand Down
2 changes: 1 addition & 1 deletion Contentful.Core/Contentful.Core.csproj
Expand Up @@ -20,7 +20,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>4.0.0.0</FileVersion>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="gitlink" Version="3.1.0">
Expand Down
51 changes: 51 additions & 0 deletions Contentful.Core/Models/Management/IFieldValidation.cs
Expand Up @@ -484,4 +484,55 @@ public object CreateValidator()
return new { assetImageDimensions = new { width = new { min = MinWidth, max = MaxWidth }, height = new { min = MinHeight, max = MaxHeight } }, message = Message };
}
}

/// <summary>
/// Represents a validator that validates the nodes of a rich text field.
/// </summary>
public class NodesValidator : IFieldValidator
{
/// <summary>
/// A list of validations applied to entry hyper links.
/// </summary>
[JsonProperty("entry-hyperlink")]
public IEnumerable<IFieldValidator> EntryHyperlink { get; set; }
/// <summary>
/// A list of validations applied to embedded entry blocks.
/// </summary>
[JsonProperty("embedded-entry-block")]
public IEnumerable<IFieldValidator> EmbeddedEntryBlock { get; set; }
/// <summary>
/// A list of validations applied to inline embedded entries.
/// </summary>
[JsonProperty("embedded-entry-inline")]
public IEnumerable<IFieldValidator> EmbeddedEntryInline { get; set; }


/// <summary>
/// Creates a representation of this validator that can be easily serialized.
/// </summary>
/// <returns>The object to serialize.</returns>
public object CreateValidator()
{
return new { nodes = new Nodes(this) };
}
}

internal class Nodes
{
public Nodes(NodesValidator validator)
{
EntryHyperlink = validator.EntryHyperlink;
EmbeddedEntryBlock = validator.EmbeddedEntryBlock;
EmbeddedEntryInline = validator.EmbeddedEntryInline;
}

[JsonProperty("entry-hyperlink")]
public IEnumerable<IFieldValidator> EntryHyperlink { get; set; }

[JsonProperty("embedded-entry-block")]
public IEnumerable<IFieldValidator> EmbeddedEntryBlock { get; set; }

[JsonProperty("embedded-entry-inline")]
public IEnumerable<IFieldValidator> EmbeddedEntryInline { get; set; }
}
}

0 comments on commit 9299c93

Please sign in to comment.