From e9488797607bdae8215800ed0700274dca092686 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 26 Jun 2020 15:23:46 +1000 Subject: [PATCH] Starting to get somewhere, validation results are now being returned for nested items --- .../Validation/ContentModelValidatorTests.cs | 9 +++----- src/Umbraco.Web/ModelStateExtensions.cs | 22 +++++-------------- .../ContentPropertyValidationResult.cs | 17 +++++++++++++- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Tests/Web/Validation/ContentModelValidatorTests.cs b/src/Umbraco.Tests/Web/Validation/ContentModelValidatorTests.cs index 8c52c7e918..49a18574fe 100644 --- a/src/Umbraco.Tests/Web/Validation/ContentModelValidatorTests.cs +++ b/src/Umbraco.Tests/Web/Validation/ContentModelValidatorTests.cs @@ -212,21 +212,18 @@ namespace Umbraco.Tests.Web.Validation // assert Assert.IsFalse(isValid); - Assert.AreEqual(5, modelState.Keys.Count); + Assert.AreEqual(11, modelState.Keys.Count); const string complexPropertyKey = "_Properties.complex.invariant.null"; Assert.IsTrue(modelState.Keys.Contains(complexPropertyKey)); foreach (var state in modelState.Where(x => x.Key != complexPropertyKey)) { foreach (var error in state.Value.Errors) { - Assert.IsTrue(error.ErrorMessage.DetectIsJson()); - var json = JsonConvert.DeserializeObject(error.ErrorMessage); - Assert.IsNotEmpty(json["errorMessage"].Value()); - Assert.AreEqual(1, json["memberNames"].Value().Count); + Assert.IsFalse(error.ErrorMessage.DetectIsJson()); // non complex is just an error message } } var complexEditorErrors = modelState.Single(x => x.Key == complexPropertyKey).Value.Errors; - Assert.AreEqual(3, complexEditorErrors.Count); + Assert.AreEqual(1, complexEditorErrors.Count); var nestedError = complexEditorErrors.Single(x => x.ErrorMessage.Contains("nestedValidation")); var jsonNestedError = JsonConvert.DeserializeObject(nestedError.ErrorMessage); Assert.AreEqual(JTokenType.Array, jsonNestedError["nestedValidation"].Type); diff --git a/src/Umbraco.Web/ModelStateExtensions.cs b/src/Umbraco.Web/ModelStateExtensions.cs index 8002c32e61..c135c41657 100644 --- a/src/Umbraco.Web/ModelStateExtensions.cs +++ b/src/Umbraco.Web/ModelStateExtensions.cs @@ -53,26 +53,14 @@ namespace Umbraco.Web internal static void AddPropertyError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, ValidationResult result, string propertyAlias, string culture = "", string segment = "") { - - var propValidationResult = new ContentPropertyValidationResult(result); - - var keyParts = new[] - { + modelState.AddValidationError( + new ContentPropertyValidationResult(result), "_Properties", propertyAlias, //if the culture is null, we'll add the term 'invariant' as part of the key culture.IsNullOrWhiteSpace() ? "invariant" : culture, // if the segment is null, we'll add the term 'null' as part of the key - segment.IsNullOrWhiteSpace() ? "null" : segment - }; - - var key = string.Join(".", keyParts); - - modelState.AddModelError( - key, - JsonConvert.SerializeObject( - propValidationResult, - new ValidationResultConverter())); + segment.IsNullOrWhiteSpace() ? "null" : segment); } /// @@ -181,12 +169,12 @@ namespace Umbraco.Web var delimitedParts = string.Join(".", parts); foreach (var memberName in result.MemberNames) { - modelState.TryAddModelError($"{delimitedParts}.{memberName}", result.ErrorMessage); + modelState.TryAddModelError($"{delimitedParts}.{memberName}", result.ToString()); withNames = true; } if (!withNames) { - modelState.TryAddModelError($"{delimitedParts}", result.ErrorMessage); + modelState.TryAddModelError($"{delimitedParts}", result.ToString()); } } diff --git a/src/Umbraco.Web/PropertyEditors/Validation/ContentPropertyValidationResult.cs b/src/Umbraco.Web/PropertyEditors/Validation/ContentPropertyValidationResult.cs index 9c80f6b8e7..2f1fe010f0 100644 --- a/src/Umbraco.Web/PropertyEditors/Validation/ContentPropertyValidationResult.cs +++ b/src/Umbraco.Web/PropertyEditors/Validation/ContentPropertyValidationResult.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using Newtonsoft.Json; +using System.ComponentModel.DataAnnotations; namespace Umbraco.Web.PropertyEditors.Validation { @@ -23,5 +24,19 @@ namespace Umbraco.Web.PropertyEditors.Validation /// There can be nested results for complex editors that contain other editors /// public ComplexEditorValidationResult ComplexEditorResults { get; } + + /// + /// Return the if is null, else the serialized + /// complex validation results + /// + /// + public override string ToString() + { + if (ComplexEditorResults == null) + return base.ToString(); + + var json = JsonConvert.SerializeObject(this, new ValidationResultConverter()); + return json; + } } }