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>());
            }
        }
        private IEnumerable <ValidationResult> Validate(ContentBlockModelValue blockValue)
        {
            if (blockValue == null)
            {
                return(Enumerable.Empty <ValidationResult>());
            }

            var repository = _definitionRepository.Value;

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

            var definition = repository.GetById(blockValue.DefinitionId);

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

            IDataType dataType = null;

            if (definition.DataTypeId is int dataTypeId)
            {
                dataType = _dataTypeService.GetDataType(dataTypeId);
            }
            else if (definition.DataTypeKey is Guid dataTypeKey)
            {
                dataType = _dataTypeService.GetDataType(dataTypeKey);
            }

            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
            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);
            })
                                                     ));
        }
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>());
            }
        }