/// <summary> /// /// </summary> /// <param name="memberAccessValidatorFactory"></param> /// <param name="validatorFactory"></param> public ValidatorBuilderBase( MemberAccessValidatorBuilderFactory memberAccessValidatorFactory, ValidatorFactory validatorFactory) { this.memberAccessValidatorFactory = memberAccessValidatorFactory; this.validatorFactory = validatorFactory; }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="propertyInfo"></param> /// <param name="ruleset"></param> /// <param name="memberAccessValidatorBuilderFactory"></param> /// <returns></returns> public static Validator GetPropertyValidatorFromValidationAttributes( Type type, PropertyInfo propertyInfo, string ruleset, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory) { Validator validator = null; lock (validationAttributeOnlyPropertyValidatorsCache) { var key = new PropertyValidatorCacheKey(type, propertyInfo.Name, ruleset); if (!validationAttributeOnlyPropertyValidatorsCache.TryGetValue(key, out validator)) { validator = string.IsNullOrEmpty(ruleset) ? new ValidationAttributeValidatorBuilder(memberAccessValidatorBuilderFactory, ValidationFactory.DefaultCompositeValidatorFactory) .CreateValidatorForProperty(propertyInfo) : new AndCompositeValidator(); validationAttributeOnlyPropertyValidatorsCache[key] = validator; } } return(validator); }
///<summary> ///</summary> ///<param name="validationSettings"></param> ///<param name="memberAccessValidatorFactory"></param> ///<param name="validatorFactory"></param> public ConfigurationValidatorBuilder( ValidationSettings validationSettings, MemberAccessValidatorBuilderFactory memberAccessValidatorFactory, ValidatorFactory validatorFactory) : base(memberAccessValidatorFactory, validatorFactory) { this.validationSettings = validationSettings; }
/// <summary> /// Returns a <see cref="Validator"/> for <paramref name="propertyInfo"/> as defined in the validation specification /// for <paramref name="type"/>. /// </summary> /// <param name="type">The type for which the validation specification must be retrieved.</param> /// <param name="propertyInfo">The property for which the validator must be returned.</param> /// <param name="ruleset">The ruleset to use when retrieving validation information, or an empty string to use /// the default ruleset.</param> /// <param name="validationSpecificationSource">The <see cref="ValidationSpecificationSource"/> indicating /// where to retrieve validation specifications.</param> /// <param name="memberAccessValidatorBuilderFactory"></param> /// <returns>The appropriate validator, or null if there is no such validator specified.</returns> /// <exception cref="InvalidOperationException">when <paramref name="type"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">when <paramref name="propertyInfo"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">when <paramref name="propertyInfo"/> is not a readable property.</exception> /// <remarks> /// Both <paramref name="type"/> and <paramref name="propertyInfo"/> must be provided as <paramref name="type"/> might be different /// from the declaring type for <paramref name="propertyInfo"/>. /// </remarks> public static Validator GetPropertyValidator(Type type, PropertyInfo propertyInfo, string ruleset, ValidationSpecificationSource validationSpecificationSource, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory) { if (null == type) { // invalid operation exception is used to match the platform's errors throw new InvalidOperationException(Resources.ExceptionTypeNotFound); } if (null == propertyInfo) { throw new InvalidOperationException(Resources.ExceptionPropertyNotFound); } if (!propertyInfo.CanRead) { throw new InvalidOperationException(Resources.ExceptionPropertyNotReadable); } var validators = new List <Validator>(); if (validationSpecificationSource.IsSet(ValidationSpecificationSource.Attributes)) { validators.Add( GetPropertyValidatorFromAttributes(type, propertyInfo, ruleset, memberAccessValidatorBuilderFactory)); } if (validationSpecificationSource.IsSet(ValidationSpecificationSource.Configuration)) { validators.Add( GetPropertyValidatorFromConfiguration(type, propertyInfo, ruleset, memberAccessValidatorBuilderFactory)); } if (validationSpecificationSource.IsSet(ValidationSpecificationSource.DataAnnotations)) { validators.Add( GetPropertyValidatorFromValidationAttributes(type, propertyInfo, ruleset, memberAccessValidatorBuilderFactory)); } var effectiveValidators = validators.Where(v => v != null).ToArray(); if (effectiveValidators.Length == 1) { return(effectiveValidators[0]); } else if (effectiveValidators.Length > 1) { return(new AndCompositeValidator(effectiveValidators)); } else { return(null); } }
///<summary> ///</summary> ///<param name="configurationSource"></param> ///<param name="memberAccessValidatorBuilderFactory"></param> ///<param name="validatorFactory"></param> ///<returns></returns> public static ConfigurationValidatorBuilder FromConfiguration( IConfigurationSource configurationSource, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory, ValidatorFactory validatorFactory) { var settings = ValidationSettings.TryGet(configurationSource); return new ConfigurationValidatorBuilder( settings, memberAccessValidatorBuilderFactory, validatorFactory); }
///<summary> ///</summary> ///<param name="configurationSource"></param> ///<param name="memberAccessValidatorBuilderFactory"></param> ///<param name="validatorFactory"></param> ///<returns></returns> public static ConfigurationValidatorBuilder FromConfiguration( IConfigurationSource configurationSource, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory, ValidatorFactory validatorFactory) { var settings = ValidationSettings.TryGet(configurationSource); return (new ConfigurationValidatorBuilder( settings, memberAccessValidatorBuilderFactory, validatorFactory)); }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="propertyInfo"></param> /// <param name="ruleset"></param> /// <param name="memberAccessValidatorBuilderFactory"></param> /// <returns></returns> public static Validator GetPropertyValidatorFromConfiguration( Type type, PropertyInfo propertyInfo, string ruleset, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory) { Validator validator = null; lock (defaultConfigurationOnlyPropertyValidatorsCache) { PropertyValidatorCacheKey key = new PropertyValidatorCacheKey(type, propertyInfo.Name, ruleset); if (!defaultConfigurationOnlyPropertyValidatorsCache.TryGetValue(key, out validator)) { using (var configurationSource = ConfigurationSourceFactory.Create()) { ConfigurationValidatorBuilder builder = ConfigurationValidatorBuilder.FromConfiguration( configurationSource, memberAccessValidatorBuilderFactory, ValidationFactory.DefaultCompositeValidatorFactory); ValidatedPropertyReference propertyReference = GetValidatedPropertyReference(type, ruleset, propertyInfo.Name, configurationSource); if (null == propertyReference) { validator = null; } else { validator = builder.CreateValidatorForProperty(type, propertyReference); } defaultConfigurationOnlyPropertyValidatorsCache[key] = validator; } } } return(validator); }
/// <summary> /// /// </summary> /// <param name="memberAccessValidatorFactory"></param> /// <param name="validatorFactory"></param> public MetadataValidatorBuilder( MemberAccessValidatorBuilderFactory memberAccessValidatorFactory, ValidatorFactory validatorFactory) : base(memberAccessValidatorFactory, validatorFactory) { }
/// <summary> /// /// </summary> /// <param name="memberAccessValidatorFactory"></param> public MetadataValidatorBuilder(MemberAccessValidatorBuilderFactory memberAccessValidatorFactory) : this(memberAccessValidatorFactory, ValidationFactory.DefaultCompositeValidatorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="ValidationAttributeValidatorBuilder"/> class. /// </summary> /// <param name="memberAccessValidatorFactory"></param> /// <param name="validatorFactory">Factory to use when building nested validators.</param> public ValidationAttributeValidatorBuilder( MemberAccessValidatorBuilderFactory memberAccessValidatorFactory, ValidatorFactory validatorFactory) : base(memberAccessValidatorFactory, validatorFactory) { }