From 4957acb8d32ec1ece4a38891b4ea24837aeab70d Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 10 Sep 2013 17:20:13 +1000 Subject: [PATCH] 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. --- .../Manifest/PropertyEditorConverter.cs | 53 ++++++++++++++++++- .../PropertyEditors/PropertyEditor.cs | 2 +- .../PropertyEditors/ValidatorsResolver.cs | 2 +- .../Serialization/JsonCreationConverter.cs | 1 + .../Manifest/ManifestParserTests.cs | 49 ++++++++--------- ...ing-Configuration-To-Property-Editor.ngdoc | 2 +- .../packages/MarkdownEditor/package.manifest | 2 +- .../packages/propertyeditors/package.manifest | 2 +- .../App_Plugins/MyPackage/Package.manifest | 41 +++++--------- 9 files changed, 90 insertions(+), 64 deletions(-) diff --git a/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs b/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs index 8d946d56a9..e6f256c789 100644 --- a/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs +++ b/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs @@ -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; + } } } \ No newline at end of file diff --git a/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs b/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs index 7b2adf0e26..6df0f91275 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs @@ -63,7 +63,7 @@ namespace Umbraco.Core.PropertyEditors get { return CreateValueEditor(); } } - [JsonProperty("preValueEditor")] + [JsonProperty("prevalues")] public PreValueEditor PreValueEditor { get { return CreatePreValueEditor(); } diff --git a/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs b/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs index 4b1f0b7c28..6fcbe112f7 100644 --- a/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs +++ b/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs @@ -30,7 +30,7 @@ namespace Umbraco.Core.PropertyEditors /// public ManifestValueValidator GetValidator(string name) { - return Values.FirstOrDefault(x => x.TypeName == name); + return Values.FirstOrDefault(x => x.TypeName.InvariantEquals(name)); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Serialization/JsonCreationConverter.cs b/src/Umbraco.Core/Serialization/JsonCreationConverter.cs index 5716d11dd9..1dd79fbb96 100644 --- a/src/Umbraco.Core/Serialization/JsonCreationConverter.cs +++ b/src/Umbraco.Core/Serialization/JsonCreationConverter.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq; namespace Umbraco.Core.Serialization { + internal abstract class JsonCreationConverter : JsonConverter { /// diff --git a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs index 1e631bb09a..5cc1c2fae0 100644 --- a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs +++ b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs @@ -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); diff --git a/src/Umbraco.Web.UI.Client/docs/src/tutorials/Adding-Configuration-To-Property-Editor.ngdoc b/src/Umbraco.Web.UI.Client/docs/src/tutorials/Adding-Configuration-To-Property-Editor.ngdoc index 79663f1a6b..75fd88857b 100644 --- a/src/Umbraco.Web.UI.Client/docs/src/tutorials/Adding-Configuration-To-Property-Editor.ngdoc +++ b/src/Umbraco.Web.UI.Client/docs/src/tutorials/Adding-Configuration-To-Property-Editor.ngdoc @@ -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", diff --git a/src/Umbraco.Web.UI.Client/src/packages/MarkdownEditor/package.manifest b/src/Umbraco.Web.UI.Client/src/packages/MarkdownEditor/package.manifest index dc3c7bb77b..fa7be19e47 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/MarkdownEditor/package.manifest +++ b/src/Umbraco.Web.UI.Client/src/packages/MarkdownEditor/package.manifest @@ -6,7 +6,7 @@ editor: { view: "~/App_Plugins/MarkDownEditor/markdowneditor.html" }, - preValueEditor: { + prevalues: { fields: [ { label: "Preview", diff --git a/src/Umbraco.Web.UI.Client/src/packages/propertyeditors/package.manifest b/src/Umbraco.Web.UI.Client/src/packages/propertyeditors/package.manifest index 46831aaae2..5ac299c246 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/propertyeditors/package.manifest +++ b/src/Umbraco.Web.UI.Client/src/packages/propertyeditors/package.manifest @@ -7,7 +7,7 @@ view: "~/App_Plugins/propertyeditors/mntp/mntp.html" }, - preValueEditor: { + prevalues: { fields: [ { label: "Multipicker", diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest index 5b68389b52..d612d3f0e3 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest @@ -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'*/ ] } \ No newline at end of file