public override object FromEditor(ContentPropertyData editorValue, object currentValue)
        {
            string json       = editorValue.Value?.ToString();
            var    modelValue = _deserializer.Deserialize(json);

            if (modelValue == null)
            {
                return(base.FromEditor(editorValue, currentValue));
            }

            JArray fromEditor(ContentBlockModelValue block)
            {
                if (_utils.GetDataType(block.DefinitionId) is IDataType dataType &&
                    dataType.Editor?.GetValueEditor() is IDataValueEditor valueEditor)
                {
                    var propertyData = new ContentPropertyData(block.Content.ToString(), dataType.Configuration);

                    try
                    {
                        var ncJson = valueEditor.FromEditor(propertyData, null)?.ToString();

                        if (!string.IsNullOrWhiteSpace(ncJson))
                        {
                            return(JArray.Parse(ncJson));
                        }
                    }
                    catch
                    {
                        return(block.Content);
                    }
                }

                // Fallback: return the original value
                return(block.Content);
            }

            if (modelValue.Header != null)
            {
                modelValue.Header.Content = fromEditor(modelValue.Header);
            }

            if (modelValue.Blocks?.Any() == true)
            {
                foreach (var block in modelValue.Blocks)
                {
                    block.Content = fromEditor(block);
                }
            }

            return(JsonConvert.SerializeObject(modelValue, Formatting.None));
        }
        private IEnumerable <ValidationResult> Validate(ContentBlockModelValue blockValue)
        {
            if (blockValue == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            IDataType dataType = _utils.GetDataType(blockValue.DefinitionId);

            if (dataType == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            var valueEditor = dataType.Editor?.GetValueEditor();

            if (valueEditor == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            try
            {
                // Validate the value using all validators that have been defined for the datatype
                return(valueEditor.Validators.SelectMany(ve => ve.Validate(blockValue.Content, ValueTypes.Json, dataType.Configuration)));
            }
            catch
            {
                // Nested Content validation will throw in some situations,
                // e.g. when a ContentBlock document type has no properties.
                return(Enumerable.Empty <ValidationResult>());
            }
        }
Beispiel #3
0
        private IEnumerable <ValidationResult> Validate(ContentBlockModelValue blockValue)
        {
            if (blockValue == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            IDataType dataType = _utils.GetDataType(blockValue.DefinitionId);

            if (dataType == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            var valueEditor = dataType.Editor?.GetValueEditor();

            if (valueEditor == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            // We add a prefix to memberNames of fields with errors
            // so we can display it at the correct Content Block
            string memberNamePrefix = $"#content-blocks-id:{blockValue.Id}#";

            // Validate the value using all validators that have been defined for the datatype
            try
            {
                return(valueEditor.Validators
                       .SelectMany(ve => ve
                                   .Validate(blockValue.Content, ValueTypes.Json, dataType.Configuration)
                                   .Select(vr =>
                {
                    var memberNames = vr.MemberNames.Select(memberName => memberNamePrefix + memberName);
                    var errorMessage = Regex.Replace(vr.ErrorMessage ?? "", @"^Item \d+:?\s*", "");
                    return new ValidationResult(errorMessage, memberNames);
                })
                                   )
                       .ToList());
            }
            catch
            {
                // Nested Content validation will throw in some situations,
                // e.g. when a ContentBlock document type has no properties.
                return(Enumerable.Empty <ValidationResult>());
            }
        }