public void Should_be_able_to_define_rules_for_a_specific_class_using_the_ValidationMap() { Assert.Fail(); var validationMap = new ValidationMap<TestEntity>(); validationMap.Property(x => x.Email, rule => rule.WillBeValidatedBy<IsRequired<CanBeAnyViewModel>>()); }
public void Should_be_able_to_define_rules_for_a_specific_class_using_the_ValidationMap() { Assert.Fail(); var validationMap = new ValidationMap <TestEntity>(); validationMap.Property(x => x.Email, rule => rule.WillBeValidatedBy <IsRequired <CanBeAnyViewModel> >()); }
public IEnumerable GetErrors(string propertyName) { if (string.IsNullOrEmpty(propertyName) || !ValidationMap.ContainsKey(propertyName)) { return(Enumerable.Empty <string>()); } return(ValidationMap[propertyName].Where(p => p.HasError).Select(p => p.Error)); }
public IEnumerable GetErrors(string propertyName) { if (string.IsNullOrEmpty(propertyName) || !ValidationMap.ContainsKey(propertyName)) { return(Enumerable.Empty <string>()); } Debug.WriteLine($"IDataError[{propertyName}]"); return(ValidationMap[propertyName].Where(b => b.HasError).Select(b => b.Error)); }
void IPropertyValidatorContainer.AddValidator <TProperty>(string propertyName, PropertyValidator <TProperty> validator) { List <PropertyValidatorBase> validatorList; if (!ValidationMap.TryGetValue(propertyName, out validatorList)) { validatorList = new List <PropertyValidatorBase>(); ValidationMap.Add(propertyName, validatorList); } validatorList.Add(validator); }
protected void ValidateProperty(string propertyName) { Debug.Assert(propertyName != null, "propertyName != null"); List <ValidationBinder> binder; if (ValidationMap.TryGetValue(propertyName, out binder)) { binder.ForEach(b => b.Update()); RaiseErrorsChanged(propertyName); } }
internal void AddValidation(string propertyName, ValidationBinder binder) { List <ValidationBinder> bindList; if (!ValidationMap.TryGetValue(propertyName, out bindList)) { bindList = new List <ValidationBinder>(); ValidationMap.Add(propertyName, bindList); } bindList.Add(binder); }
private void ClearValidationErrors(string propertyName) { List <PropertyValidatorBase> validatorList; if (!ValidationMap.TryGetValue(propertyName, out validatorList)) { return; } foreach (var v in validatorList) { v.Clear(); } RaiseErrorsChanged(propertyName); }
public static void SetValidationAnnotation <TEntity>( this EdmModel model, IEdmTerm term, IEdmExpression expression, string message, Expression <Func <TEntity, bool> > validationExpression = null, Expression <Func <TEntity, object> > propertyExpression = null) { if (model == null) { throw new ArgumentNullException(nameof(model)); } // TODO: If validation expression is null, then compile it from the IQL var type = model.GetEdmType(typeof(TEntity)) as EdmEntityType; IEdmVocabularyAnnotatable target = type; string propertyName = null; if (propertyExpression != null) { propertyName = propertyExpression.GetAccessedProperty().Name; var property = type.Properties().Single(p => p.Name == propertyName); target = property; } IqlQueryableAdapter.ExpressionConverter = () => new ExpressionToIqlConverter(); //var iql = ExpressionToIqlExpressionParser<TEntity>.Parse(validationExpression); //var parser = // new ActionParserInstance<ODataIqlData, ODataIqlExpressionAdapter>(new ODataIqlExpressionAdapter()); var expressionLabel = new EdmLabeledExpression("Expression", expression); var messageLabel = new EdmLabeledExpression("Message", new EdmStringConstant(message)); var coll = new EdmCollectionExpression(expressionLabel, messageLabel); var annotation = new EdmVocabularyAnnotation(target, term, coll); var validation = ValidationMap.ForModel(model); var validationObject = new EntityValidation <TEntity>(validationExpression, message); validation.EntityValidation <TEntity>() .AddValidation(validationObject, propertyName); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
private void ValidateProperty(string propertyName) { List <PropertyValidatorBase> validatorList; if (!ValidationMap.TryGetValue(propertyName, out validatorList)) { return; } var value = GetPropertyValue(propertyName); validatorList.ForEach(b => { if (!b.IsManual) { b.Update(value); } }); RaiseErrorsChanged(propertyName); }
public void ClearValidationRules() { ValidationMap.Clear(); }
public virtual bool ValidateEntity <TEntity>(TEntity entity, Dictionary <object, bool> validated = null, string path = "") { validated = validated ?? new Dictionary <object, bool>(); if (validated.ContainsKey(entity)) { return(validated[entity]); } var iqlValidation = ValidationMap.ForType <TEntity>(); var accessor = string.IsNullOrWhiteSpace(path) ? "" : "."; var isValid = !string.IsNullOrWhiteSpace(path) || TryValidateModel(entity); validated.Add(entity, isValid); if (iqlValidation?.EntityValidations != null) { foreach (var entityValidation in iqlValidation.EntityValidations) { var iqlValidationResult = entityValidation.ValidationFunction(entity); isValid = isValid && iqlValidationResult; if (!iqlValidationResult) { ModelState.AddModelError(path, entityValidation.Message); } } } if (iqlValidation?.PropertyValidations != null) { foreach (var propertyValidationCollection in iqlValidation.PropertyValidations) { foreach (var propertyValidation in propertyValidationCollection.Validations) { var iqlValidationResult = propertyValidation.ValidationFunction(entity); isValid = isValid && iqlValidationResult; if (!iqlValidationResult) { ModelState.AddModelError($"{path}{accessor}{propertyValidationCollection.PropertyName}", propertyValidation.Message); } } } } var entityType = entity.GetType(); foreach (var property in entityType.GetRuntimeProperties()) { var propertyType = property.PropertyType; var elementType = propertyType; var isSimpleEnumerable = false; if (typeof(IEnumerable).IsAssignableFrom(propertyType) && propertyType.IsGenericType) { var genericArguments = propertyType.GetGenericArguments(); if (genericArguments.Length == 1) { elementType = genericArguments[0]; isSimpleEnumerable = true; } } var relatedEntityType = Crud.Unsecured.Context.Model.FindEntityType(elementType); if (relatedEntityType != null) { var value = entity.GetPropertyValue(property.Name); if (value != null) { if (isSimpleEnumerable) { var i = 0; foreach (var element in (IEnumerable)value) { var childPath = $"{path}{accessor}{property.Name}[{i}]"; isValid = InvokeValidateEntity(element, validated, childPath) && isValid; i++; } } else { var childPath = $"{path}{accessor}{property.Name}"; isValid = InvokeValidateEntity(value, validated, childPath) && isValid; } } } } return(isValid); }