/// <summary> /// Initializes a new instance of the <see cref="And{T}"/> class. /// </summary> /// <param name="rule1">First rule.</param> /// <param name="rule2">Second rule.</param> /// <param name="breakOnFirstError">Value indicating whether rule should break on first error.</param> public And(IPropertyValidationRule <T> rule1, IPropertyValidationRule <T> rule2, bool breakOnFirstError = false) { FirstRule = rule1.AssertArgumentNotNull(nameof(rule1)); LastRule = rule2.AssertArgumentNotNull(nameof(rule2)); Property = rule1.Property; BreakOnFirstError = breakOnFirstError; }
/// <summary> /// Sets <see cref="IPropertyValidationRule{T}"/>. /// Replaces property metadata <see cref="IPropertyValidationRules"/>. /// </summary> /// <typeparam name="T">Property value type.</typeparam> /// <param name="property">Source property.</param> /// <param name="validation">Property validation.</param> /// <returns>The same property.</returns> public static IProperty <T> SetValidation <T>(this IProperty <T> property, Func <IProperty <T>, IPropertyValidationRule <T> > validation) { property.AssertArgumentNotNull(nameof(property)); validation.AssertArgumentNotNull(nameof(validation)); IPropertyValidationRule <T> validationRule = validation(property); IPropertyValidationRules validationRules = new PropertyValidationRules(property, new[] { validationRule }); return(property.SetMetadata <IProperty <T>, IPropertyValidationRules>(validationRules)); }
/// <summary> /// Adds <see cref="IPropertyValidationRule{T}"/> to metadata <see cref="IPropertyValidationRules"/>. /// </summary> /// <typeparam name="T">Property value type.</typeparam> /// <param name="property">Source property.</param> /// <param name="validation">Property validation.</param> /// <returns>The same property.</returns> public static IProperty <T> AddValidation <T>(this IProperty <T> property, Func <IProperty <T>, IPropertyValidationRule <T> > validation) { property.AssertArgumentNotNull(nameof(property)); validation.AssertArgumentNotNull(nameof(validation)); return(property.ConfigureMetadata <IProperty <T>, IPropertyValidationRules>( createMetadata: CreatePropertyValidationRules, configureMetadata: propertyValidation => { IPropertyValidationRule <T> validationRule = validation(property); return propertyValidation.AddRule(validationRule); })); }
private void AddValidationRule(PropertyInfo propertyInfo, IPropertyValidationRule rule) { IList <IPropertyValidationRule> rulesForProperty; if (!this.PropertyValidationRules.TryGetValue(propertyInfo.Name, out rulesForProperty)) { this.PropertyValidationRules.Add(propertyInfo.Name, rulesForProperty = new List <IPropertyValidationRule>()); } rulesForProperty.Add(rule); LoggerExtensions.LogPropertyValidationInfo( "Adding validation rule {0} with Id {1} for property {2} on element {3}", rule.GetType().Name, rule.RuleInstanceId, propertyInfo.Name, this.GetType().Name); }
private void RemoveValidationResult(string propertyName, IPropertyValidationRule rule) { lock (this.validationErrorsLock) { KeyValuePair <string, ICollection <ValidationResult> > errorSet = this.validationErrors.FirstOrDefault(pair => pair.Key == propertyName); if (errorSet.Key == null) { // There arent any validation validation errors for this property. This can be expected when a // rule deems a property's value a 'valid' when it is already valid (such as the initial value). return; } ValidationResult error = errorSet.Value.FirstOrDefault(e => e.RuleInstanceId == rule.RuleInstanceId); if (error == null) { LoggerExtensions.LogPropertyValidationInfo( "RemoveValidationResult: Failed to remove validation result for rule {0} ({1}) because it was not found in the collection for element {2} of property {3}", rule.GetType().Name, rule.RuleInstanceId, this.TrackingId, propertyName); return; } errorSet.Value.Remove(error); if (!errorSet.Value.Any()) { this.validationErrors.Remove(errorSet.Key); } LoggerExtensions.LogPropertyValidationInfo( "RemoveValidationResult: Removed result for rule {0} ({1}) on property {2}", rule.GetType().Name, error.RuleInstanceId, error.PropertyName); this.ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); } }
private void AddValidationResult(ValidationResult result, IPropertyValidationRule rule) { lock (this.validationErrorsLock) { if (!this.validationErrors.ContainsKey(result.PropertyName)) { this.validationErrors[result.PropertyName] = new List <ValidationResult>(); } this.validationErrors[result.PropertyName].Add(result); } LoggerExtensions.LogPropertyValidationInfo( "AddValidationResult: Added result for rule {0} ({1}) on property {2}", rule.GetType().Name, result.RuleInstanceId, result.PropertyName); this.ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(result.PropertyName)); }
protected ValidationBase(bool disablePropertyValidation) { this.PropertyValidationRules = new Dictionary <string, IList <IPropertyValidationRule> >(); if (disablePropertyValidation) { return; } this.TrackingId = Guid.NewGuid(); LoggerExtensions.LogPropertyValidationInfo( "Instantiating ValidationBase with type {0} and Id {1}", this.GetType().Name, this.TrackingId); foreach (PropertyInfo propInfo in this.GetType().GetProperties()) { if (TypeHelper.PropertyImplementsInterface(propInfo, typeof(IValidationBase))) { // This property directly supports IValidationBase, so refer to the element for its own validation { foreach (object obj in propInfo.GetCustomAttributes(typeof(IPropertyValidationRule), true)) { IPropertyValidationRule validationAttribute = obj as IPropertyValidationRule; if (validationAttribute != null) { this.AddValidationRule(propInfo, validationAttribute); } } } continue; } // Examine the current class for any properties that have validation rule attributes declared foreach (IPropertyValidationRule rule in propInfo.GetCustomAttributes(typeof(IPropertyValidationRule), true).OfType <IPropertyValidationRule>()) { this.AddValidationRule(propInfo, rule); } } }
public PropertyRuleBuilder(IPropertyValidationRule <T, TProperty> propertyValidationRule) { _propertyValidationRule = propertyValidationRule; }
/// <summary> /// Starts to create <see cref="Or{T}"/> composite rule. /// </summary> /// <typeparam name="T">Property type.</typeparam> /// <param name="rule1">The first rule.</param> /// <returns>Builder.</returns> public static OrBuilder <T> Or <T>(this IPropertyValidationRule <T> rule1) { return(new OrBuilder <T>(rule1)); }
/// <summary> /// Initializes a new instance of the <see cref="OrBuilder{T}"/> class. /// </summary> /// <param name="firstRule">The first rule.</param> public OrBuilder(IPropertyValidationRule <T> firstRule) { FirstRule = firstRule.AssertArgumentNotNull(nameof(firstRule)); }
/// <inheritdoc /> public Or <T> CombineWith(IPropertyValidationRule <T> nextRule) { nextRule.AssertArgumentNotNull(nameof(nextRule)); return(new Or <T>(FirstRule, nextRule)); }
/// <summary> /// Starts to create <see cref="And{T}"/> composite rule. /// </summary> /// <typeparam name="T">Property type.</typeparam> /// <param name="rule1">The first rule.</param> /// <param name="breakOnFirstError">Value indicating whether rule should break on first error.</param> /// <returns>Builder.</returns> public static AndBuilder <T> And <T>(this IPropertyValidationRule <T> rule1, bool breakOnFirstError = false) { return(new AndBuilder <T>(rule1, breakOnFirstError)); }
/// <summary> /// Initializes a new instance of the <see cref="AndBuilder{T}"/> class. /// </summary> /// <param name="firstRule">The first rule.</param> /// <param name="breakOnFirstError">Value indicating whether rule should break on first error.</param> public AndBuilder(IPropertyValidationRule <T> firstRule, bool breakOnFirstError = false) { FirstRule = firstRule.AssertArgumentNotNull(nameof(firstRule)); BreakOnFirstError = breakOnFirstError; }
/// <inheritdoc /> public And <T> CombineWith(IPropertyValidationRule <T> nextRule) { nextRule.AssertArgumentNotNull(nameof(nextRule)); return(new And <T>(FirstRule, nextRule, BreakOnFirstError)); }