protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object value)
 {
     foreach (var row in _nestedContentValues.GetPropertyValues(value))
     {
         var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Id);
         foreach (var prop in row.PropertyValues)
         {
             elementValidation.AddPropertyTypeValidation(
                 new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
         }
         yield return(elementValidation);
     }
 }
Пример #2
0
            protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object value)
            {
                var rows = _nestedContentValues.GetPropertyValues(value);

                if (rows.Count == 0)
                {
                    yield break;
                }

                // There is no guarantee that the client will post data for every property defined in the Element Type but we still
                // need to validate that data for each property especially for things like 'required' data to work.
                // Lookup all element types for all content/settings and then we can populate any empty properties.
                var allElementAliases = rows.Select(x => x.ContentTypeAlias).ToList();
                // unfortunately we need to get all content types and post filter - but they are cached so its ok, there's
                // no overload to lookup by many aliases.
                var allElementTypes = _contentTypeService.GetAll().Where(x => allElementAliases.Contains(x.Alias)).ToDictionary(x => x.Alias);

                foreach (var row in rows)
                {
                    if (!allElementTypes.TryGetValue(row.ContentTypeAlias, out var elementType))
                    {
                        throw new InvalidOperationException($"No element type found with alias {row.ContentTypeAlias}");
                    }

                    // now ensure missing properties
                    foreach (var elementTypeProp in elementType.CompositionPropertyTypes)
                    {
                        if (!row.PropertyValues.ContainsKey(elementTypeProp.Alias))
                        {
                            // set values to null
                            row.PropertyValues[elementTypeProp.Alias] = new NestedContentValues.NestedContentPropertyValue
                            {
                                PropertyType = elementTypeProp,
                                Value        = null
                            };
                            row.RawPropertyValues[elementTypeProp.Alias] = null;
                        }
                    }

                    var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Id);
                    foreach (var prop in row.PropertyValues)
                    {
                        elementValidation.AddPropertyTypeValidation(
                            new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
                    }
                    yield return(elementValidation);
                }
            }
Пример #3
0
            protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object value)
            {
                var blockEditorData = _blockEditorValues.DeserializeAndClean(value);

                if (blockEditorData != null)
                {
                    foreach (var row in blockEditorData.BlockValue.ContentData.Concat(blockEditorData.BlockValue.SettingsData))
                    {
                        var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Key);
                        foreach (var prop in row.PropertyValues)
                        {
                            elementValidation.AddPropertyTypeValidation(
                                new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
                        }
                        yield return(elementValidation);
                    }
                }
            }
        protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object?value)
        {
            BlockEditorData?blockEditorData = _blockEditorValues.DeserializeAndClean(value);

            if (blockEditorData != null)
            {
                // There is no guarantee that the client will post data for every property defined in the Element Type but we still
                // need to validate that data for each property especially for things like 'required' data to work.
                // Lookup all element types for all content/settings and then we can populate any empty properties.
                var allElements = blockEditorData.BlockValue.ContentData.Concat(blockEditorData.BlockValue.SettingsData)
                                  .ToList();
                var allElementTypes = _contentTypeService.GetAll(allElements.Select(x => x.ContentTypeKey).ToArray())
                                      .ToDictionary(x => x.Key);

                foreach (BlockItemData row in allElements)
                {
                    if (!allElementTypes.TryGetValue(row.ContentTypeKey, out IContentType? elementType))
                    {
                        throw new InvalidOperationException($"No element type found with key {row.ContentTypeKey}");
                    }

                    // now ensure missing properties
                    foreach (IPropertyType elementTypeProp in elementType.CompositionPropertyTypes)
                    {
                        if (!row.PropertyValues.ContainsKey(elementTypeProp.Alias))
                        {
                            // set values to null
                            row.PropertyValues[elementTypeProp.Alias] =
                                new BlockItemData.BlockPropertyValue(null, elementTypeProp);
                            row.RawPropertyValues[elementTypeProp.Alias] = null;
                        }
                    }

                    var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Key);
                    foreach (KeyValuePair <string, BlockItemData.BlockPropertyValue> prop in row.PropertyValues)
                    {
                        elementValidation.AddPropertyTypeValidation(
                            new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
                    }

                    yield return(elementValidation);
                }
            }
        }