public void CanGetAttributesForPropertyOnTypeWithMetadataTypeWithNoMatchingProperty() { var property = typeof(TypeWithMetadataTypeWithNoMatchingProperty).GetProperty("MyProperty"); var attributes = ValidationReflectionHelper.GetCustomAttributes(property, typeof(BaseAttribute), false); Assert.AreEqual(0, attributes.Length); }
public void MatchesMethodsOnMetadataTypeBasedOnParameterTypes() { var method = typeof(TypeWithMetadataTypeWithMatchingMethodWithDifferentSignatures).GetMethod("MyMethod"); var attributes = ValidationReflectionHelper.GetCustomAttributes(method, typeof(BaseAttribute), false); Assert.AreEqual(0, attributes.Length); }
/// <summary> /// Creates the <see cref="PropertyComparisonValidator"/> described by the configuration object. /// </summary> /// <param name="targetType">The type of object that will be validated by the validator.</param> /// <param name="ownerType">The type of the object from which the value to validate is extracted.</param> /// <param name="memberValueAccessBuilder">The <see cref="MemberValueAccessBuilder"/> to use for validators that /// require access to properties.</param> /// <param name="validatorFactory">Factory to use when building nested validators.</param> /// <returns>The created <see cref="PropertyComparisonValidator"/>.</returns> protected override Validator DoCreateValidator( Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder, ValidatorFactory validatorFactory) { if (string.IsNullOrEmpty(this.PropertyToCompare)) { throw new ConfigurationErrorsException(Resources.ExceptionPropertyToCompareNull); } PropertyInfo propertyInfo = ValidationReflectionHelper.GetProperty(ownerType, this.PropertyToCompare, false); if (propertyInfo == null) { throw new ConfigurationErrorsException( string.Format( CultureInfo.CurrentCulture, Resources.ExceptionPropertyToCompareNotFound, this.PropertyToCompare, ownerType.FullName)); } return(new PropertyComparisonValidator(memberValueAccessBuilder.GetPropertyValueAccess(propertyInfo), this.ComparisonOperator, this.Negated)); }
/// <summary> /// Crée un SelfValidator si la classe à valider possède des metadatas. /// </summary> /// <param name="targetType">Le type cible.</param> /// <param name="ruleset">Le jeu de règles.</param> /// <param name="mainValidatorFactory">La brique à utiliser lors de la construction des validateurs imbriqués..</param> /// <returns>Le validateur créé.</returns> private Validator CreateSelfValidator(Type targetType, string ruleset, ValidatorFactory mainValidatorFactory) { var validators = new List <Validator>(); // Si la classe utilise la validation par metadata, forcer quand même la SelfValidation à avoir lieu var selfValidationMethod = targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .FirstOrDefault(m => ValidationReflectionHelper.GetCustomAttributes(m, typeof(SelfValidationAttribute), false).Any()); if (selfValidationMethod != null) { validators.Add(new SelfValidationValidator(selfValidationMethod)); } // Parcourir toutes les propriétés qui possèdent de la validation classe VAB en plus de la validation METADATA // Pour chacun des ValueValidatorAttribute (ou dérivé) trouvé, ajouter un validateur foreach (var prop in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var validatedElement = (IValidatedElement) new ValidationAttributeValidatedElement(prop); var builder = new MemberAccessValidatorBuilderFactory().GetPropertyValueAccessValidatorBuilder(prop, validatedElement); foreach (IValidatorDescriptor att in prop.GetCustomAttributes(typeof(ValueValidatorAttribute), false)) { validators.Add(CreateValidatorForValidatedElement(att, validatedElement, builder, mainValidatorFactory)); } } return(new AndCompositeValidator(validators.ToArray())); }
public void CanGetAttributesForPropertyOnTypeWithMetadataTypeWithMatchingProperty() { var property = typeof(TypeWithMetadataTypeWithMatchingProperty).GetProperty("MyProperty"); var attributes = ValidationReflectionHelper.GetCustomAttributes(property, typeof(BaseAttribute), false); Assert.AreEqual(1, attributes.Length); Assert.AreEqual("from metadata", ((DerivedAttribute)attributes[0]).Description); }
public void GetsAttributesFromOriginalPropertyIfTypeHasMetadataTypeWithNoMatchingProperty() { var property = typeof(TypeWithPropertyWithAttributesAndMetadataTypeWithNoMatchingProperty).GetProperty("MyProperty"); var attributes = ValidationReflectionHelper.GetCustomAttributes(property, typeof(BaseAttribute), false); Assert.AreEqual(1, attributes.Length); Assert.AreEqual("from main", ((DerivedAttribute)attributes[0]).Description); }
protected override Validator DoCreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder) { PropertyInfo propertyInfo = ValidationReflectionHelper.GetProperty(ownerType, this.propertyToCompare, false); if (propertyInfo == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.ExceptionPropertyToCompareNotFound, new object[] { this.propertyToCompare, ownerType.FullName })); } return new PropertyComparisonValidator(memberValueAccessBuilder.GetPropertyValueAccess(propertyInfo), this.comparisonOperator, base.Negated); }
/// <summary> /// Creates the <see cref="Validator"/> described by the attribute. /// </summary> /// <param name="targetType">The type of object that will be validated by the validator.</param> /// <param name="ownerType">The type of the object from which the value to validate is extracted.</param> /// <param name="memberValueAccessBuilder">The <see cref="MemberValueAccessBuilder"/> to use for validators that /// require access to properties.</param> /// <param name="validatorFactory">Factory to use when building nested validators.</param> /// <returns>The created <see cref="Validator"/>.</returns> protected override Validator DoCreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder, ValidatorFactory validatorFactory) { PropertyInfo propertyInfo = ValidationReflectionHelper.GetProperty(ownerType, PropertyToCompare, false); if (propertyInfo == null) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Translations.ExceptionPropertyToCompareNotFound, PropertyToCompare, ownerType.FullName)); } return(new PropertyComparisonValidator(memberValueAccessBuilder.GetPropertyValueAccess(propertyInfo), ComparisonOperator, Negated)); }
public void CanGetAttributesForTypeWithAttributesOnMetadataType() { var attributes = ValidationReflectionHelper.GetCustomAttributes(typeof(TypeWithAttributesOnMetadataType), typeof(BaseAttribute), false); Assert.AreEqual(2, attributes.Length); }
private static ValueAccess GetPropertyValueAccess(string propertyName) { return(new PropertyValueAccess(ValidationReflectionHelper.GetProperty(typeof(T), propertyName, true))); }