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); } }
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); } }
public IEnumerable <ValidationResult> Validate(object rawValue, string valueType, object dataTypeConfiguration) { var validationResults = new List <ValidationResult>(); foreach (var row in _nestedContentValues.GetPropertyValues(rawValue, out _)) { if (row.PropType == null) { continue; } var config = _dataTypeService.GetDataType(row.PropType.DataTypeId).Configuration; var propertyEditor = _propertyEditors[row.PropType.PropertyEditorAlias]; if (propertyEditor == null) { continue; } foreach (var validator in propertyEditor.GetValueEditor().Validators) { foreach (var result in validator.Validate(row.JsonRowValue[row.PropKey], propertyEditor.GetValueEditor().ValueType, config)) { result.ErrorMessage = "Item " + (row.RowIndex + 1) + " '" + row.PropType.Name + "' " + result.ErrorMessage; validationResults.Add(result); } } // Check mandatory if (row.PropType.Mandatory) { if (row.JsonRowValue[row.PropKey] == null) { var message = string.IsNullOrWhiteSpace(row.PropType.MandatoryMessage) ? $"'{row.PropType.Name}' cannot be null" : row.PropType.MandatoryMessage; validationResults.Add(new ValidationResult($"Item {(row.RowIndex + 1)}: {message}", new[] { row.PropKey })); } else if (row.JsonRowValue[row.PropKey].ToString().IsNullOrWhiteSpace() || (row.JsonRowValue[row.PropKey].Type == JTokenType.Array && !row.JsonRowValue[row.PropKey].HasValues)) { var message = string.IsNullOrWhiteSpace(row.PropType.MandatoryMessage) ? $"'{row.PropType.Name}' cannot be empty" : row.PropType.MandatoryMessage; validationResults.Add(new ValidationResult($"Item {(row.RowIndex + 1)}: {message}", new[] { row.PropKey })); } } // Check regex if (!row.PropType.ValidationRegExp.IsNullOrWhiteSpace() && row.JsonRowValue[row.PropKey] != null && !row.JsonRowValue[row.PropKey].ToString().IsNullOrWhiteSpace()) { var regex = new Regex(row.PropType.ValidationRegExp); if (!regex.IsMatch(row.JsonRowValue[row.PropKey].ToString())) { var message = string.IsNullOrWhiteSpace(row.PropType.ValidationRegExpMessage) ? $"'{row.PropType.Name}' is invalid, it does not match the correct pattern" : row.PropType.ValidationRegExpMessage; validationResults.Add(new ValidationResult($"Item {(row.RowIndex + 1)}: {message}", new[] { row.PropKey })); } } } return(validationResults); }