Completes: U4-2693 Make package.manifest less complex - WARNING: You need to upgrade your package.manifest files to support the new syntax. If you don't have validation specified then you just need to update the "prevalues" key. See the issue for details.

This commit is contained in:
Shannon
2013-09-10 17:20:13 +10:00
parent a244516b19
commit 4957acb8d3
9 changed files with 90 additions and 64 deletions

View File

@@ -28,14 +28,63 @@ namespace Umbraco.Core.Manifest
{
View = jObject["editor"]["view"].ToString()
};
//the manifest JSON is a simplified json for the validators which is actually a dictionary, however, the
//c# model requires an array of validators not a dictionary so we need to change the json to an array
//to deserialize properly.
JArray converted;
if (TryConvertValidatorDictionaryToArray(jObject["editor"]["validation"] as JObject, out converted))
{
jObject["editor"]["validation"] = converted;
}
}
if (jObject["preValueEditor"] != null)
if (jObject["prevalues"] != null)
{
target.ManifestDefinedPreValueEditor = new PreValueEditor();
//the manifest JSON is a simplified json for the validators which is actually a dictionary, however, the
//c# model requires an array of validators not a dictionary so we need to change the json to an array
//to deserialize properly.
var fields = jObject["prevalues"]["fields"] as JArray;
if (fields != null)
{
foreach (var f in fields)
{
JArray converted;
if (TryConvertValidatorDictionaryToArray(f["validation"] as JObject, out converted))
{
f["validation"] = converted;
}
}
}
}
base.Deserialize(jObject, target, serializer);
}
private bool TryConvertValidatorDictionaryToArray(JObject validation, out JArray result)
{
if (validation == null)
{
result = null;
return false;
}
result = new JArray();
foreach (var entry in validation)
{
//in a special case if the value is simply 'true' (boolean) this just indicates that the
// validator is enabled, the config should just be empty.
var formattedItem = JObject.FromObject(new { type = entry.Key, config = entry.Value });
if (entry.Value.Type == JTokenType.Boolean)
{
formattedItem["config"] = "";
}
result.Add(formattedItem);
}
return true;
}
}
}

View File

@@ -63,7 +63,7 @@ namespace Umbraco.Core.PropertyEditors
get { return CreateValueEditor(); }
}
[JsonProperty("preValueEditor")]
[JsonProperty("prevalues")]
public PreValueEditor PreValueEditor
{
get { return CreatePreValueEditor(); }

View File

@@ -30,7 +30,7 @@ namespace Umbraco.Core.PropertyEditors
/// <returns></returns>
public ManifestValueValidator GetValidator(string name)
{
return Values.FirstOrDefault(x => x.TypeName == name);
return Values.FirstOrDefault(x => x.TypeName.InvariantEquals(name));
}
}
}

View File

@@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq;
namespace Umbraco.Core.Serialization
{
internal abstract class JsonCreationConverter<T> : JsonConverter
{
/// <summary>

View File

@@ -1,14 +1,13 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Manifest;
using Umbraco.Core.PropertyEditors;
namespace Umbraco.Belle.Tests
namespace Umbraco.Tests.Manifest
{
[TestFixture]
public class ManifestParserTests
@@ -25,27 +24,20 @@ namespace Umbraco.Belle.Tests
editor: {
view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html',
valueType: 'int',
validation: [
{
type: 'Required'
},
{
type: 'Regex',
value: '\\d*'
},
]
validation: {
'required': true,
'Regex': '\\d*'
}
},
preValueEditor: {
prevalues: {
fields: [
{
label: 'Some config 1',
key: 'key1',
view: '~/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html',
validation: [
{
type: 'Required'
}
]
validation: {
required: true
}
},
{
label: 'Some config 2',
@@ -64,7 +56,7 @@ namespace Umbraco.Belle.Tests
Assert.AreEqual("Some config 1", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).Name);
Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).View);
Assert.AreEqual(1, parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).Validators.Count());
Assert.AreEqual("key2", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).Key);
Assert.AreEqual("Some config 2", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).Name);
Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val2.html", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).View);
@@ -82,15 +74,10 @@ namespace Umbraco.Belle.Tests
editor: {
view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html',
valueType: 'int',
validation: [
{
type: 'Required'
},
{
type: 'Regex',
value: '\\d*'
},
]
validation: {
required : true,
regex : '\\d*'
}
}
},
{
@@ -110,6 +97,12 @@ namespace Umbraco.Belle.Tests
Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/MyEditor.html", parser.ElementAt(0).ValueEditor.View);
Assert.AreEqual("int", parser.ElementAt(0).ValueEditor.ValueType);
Assert.AreEqual(2, parser.ElementAt(0).ValueEditor.Validators.Count());
var manifestValidator1 = parser.ElementAt(0).ValueEditor.Validators.ElementAt(0) as ManifestPropertyValidator;
Assert.IsNotNull(manifestValidator1);
Assert.AreEqual("required", manifestValidator1.Type);
var manifestValidator2 = parser.ElementAt(0).ValueEditor.Validators.ElementAt(1) as ManifestPropertyValidator;
Assert.IsNotNull(manifestValidator2);
Assert.AreEqual("regex", manifestValidator2.Type);
Assert.AreEqual(new Guid("1FCF5C39-5FC7-4BCE-AFBE-6500D9EBA261"), parser.ElementAt(1).Id);
Assert.AreEqual("Test 2", parser.ElementAt(1).Name);

View File

@@ -15,7 +15,7 @@ So an editor can be used several times, with different configurations, and that
##package.manifest
So to add configuration options to our markdown editor, open the package.manifest file. rigth below the editor definition, paste in the following:
preValueEditor: {
prevalues: {
fields: [
{
label: "Preview",

View File

@@ -6,7 +6,7 @@
editor: {
view: "~/App_Plugins/MarkDownEditor/markdowneditor.html"
},
preValueEditor: {
prevalues: {
fields: [
{
label: "Preview",

View File

@@ -7,7 +7,7 @@
view: "~/App_Plugins/propertyeditors/mntp/mntp.html"
},
preValueEditor: {
prevalues: {
fields: [
{
label: "Multipicker",

View File

@@ -8,17 +8,12 @@
},
editor: {
view: "~/App_Plugins/MyPackage/PropertyEditors/Views/RegexEditor.html",
validation: [
{
type: "Required"
},
{
type: "Regex",
config: "^\\d*$"
}
]
validation: {
required : true,
regex : "^\\d*$"
}
},
preValueEditor: {
prevalues: {
fields: [
{
label: "Regular expression",
@@ -39,31 +34,19 @@
name: "CSV Editor",
editor: {
view: "~/App_Plugins/MyPackage/PropertyEditors/Views/CsvEditor.html",
validation: [
{
type: "Delimited",
config: {
delimiter: ",",
pattern: "^[a-zA-Z]*$"
}
},
]
validation: {
delimited : {
delimiter: ",",
pattern: "^[a-zA-Z]*$"
}
}
}
},
{
id: "48B0DA8C-3492-4693-96DB-5C5099C680F3",
name: "Custom editor",
editor: {
view: "~/App_Plugins/MyPackage/PropertyEditors/Views/CustomJson.html"
}
}
}
],
javascript: [
'~/App_Plugins/MyPackage/Common/Js/MyPackage.js',
'~/App_Plugins/MyPackage/PropertyEditors/Js/CsvEditor.js',
'~/App_Plugins/MyPackage/PropertyEditors/Js/PostcodeEditor.js',
'~/App_Plugins/MyPackage/PropertyEditors/Js/RegexEditor.js'
/*
'~/App_Plugins/MyPackage/PropertyEditors/Js/CustomJsonEditor.js'*/
]
}