/// <summary>
        /// Validates <paramref name="target"/> using validation criteria specified for type <typeparamref name="T"/>
        /// through configuration and attributes on type <typeparamref name="T"/> and its ancestors for the default ruleset.
        /// </summary>
        /// <typeparam name="T">The type of object to validate.</typeparam>
        /// <param name="target">The instance of <typeparamref name="T"/> to validate.</param>
        /// <param name="source">The source of validation information.</param>
        /// <returns>A collection of with the results of the individual validations.</returns>
        public static ValidationResults Validate <T>(T target, ValidationSpecificationSource source)
        {
            Type targetType = target != null?target.GetType() : typeof(T);

            Validator validator = ValidationFactory.CreateValidator(targetType, source);

            return(validator.Validate(target));
        }
Esempio n. 2
0
 /// <summary>
 /// Creates an instance of PropertyValidator
 /// </summary>
 /// <param name="objectToValidate">The object to validate.</param>
 /// <param name="propertyName">The property for objectToValidate</param>
 /// <param name="ruleset">The optional ruleset to use for validation.</param>
 /// <param name="specificationSource">Whether to use validation rules from attributes, config file, or both.</param>
 public PropertyValidator(object objectToValidate, string propertyName, string ruleset, ValidationSpecificationSource specificationSource)
     : base(null, null)
 {
     _ruleset             = ruleset;
     _typeToValidate      = objectToValidate.GetType();
     _propertyName        = propertyName;
     _specificationSource = specificationSource;
     _objectToValidate    = objectToValidate;
 }
Esempio n. 3
0
        public static void SetUsingSource(FrameworkElement element, ValidationSpecificationSource property)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            element.SetValue(UsingSourceProperty, property);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DataErrorInfoHelper"/> class for a target object with the 
        /// supplied source and ruleset.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="source">The source for validation rules for the target object.</param>
        /// <param name="ruleset">The ruleset to use when retrieving rules for the target object.</param>
        public DataErrorInfoHelper(object target, ValidationSpecificationSource source, string ruleset)
        {
            if (target == null) throw new ArgumentNullException("target");

            this.target = target;
            this.targetType = target.GetType();
            this.source = source;
            this.ruleset = ruleset;
        }
Esempio n. 5
0
        /// <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);
            }
        }
Esempio n. 6
0
 internal static Validator GetPropertyValidator(Type type,
                                                PropertyInfo propertyInfo,
                                                string ruleset,
                                                ValidationSpecificationSource validationSpecificationSource,
                                                MemberValueAccessBuilder memberValueAccessBuilder)
 {
     return(GetPropertyValidator(type,
                                 propertyInfo,
                                 ruleset,
                                 validationSpecificationSource,
                                 new MemberAccessValidatorBuilderFactory(memberValueAccessBuilder)));
 }
Esempio n. 7
0
 public MockIntegrationProxy(object rawValue,
                             string ruleset,
                             ValidationSpecificationSource specificationSource,
                             string validatedPropertyName,
                             Type validatedType)
 {
     this.rawValue                   = rawValue;
     this.rulesetField               = ruleset;
     this.specificationSourceField   = specificationSource;
     this.validatedPropertyNameField = validatedPropertyName;
     this.validatedTypeField         = validatedType;
 }
Esempio n. 8
0
        public DataErrorInfoHelper(object target, ValidationSpecificationSource source, string ruleset)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            _target     = target;
            _targetType = target.GetType();
            _source     = source;
            _ruleset    = ruleset;
        }
Esempio n. 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="type"></param>
 /// <param name="propertyInfo"></param>
 /// <param name="ruleset"></param>
 /// <param name="validationSpecificationSource"></param>
 /// <param name="memberValueAccessBuilder"></param>
 /// <returns></returns>
 public static Validator GetPropertyValidator(Type type,
                                              PropertyInfo propertyInfo,
                                              string ruleset,
                                              ValidationSpecificationSource validationSpecificationSource,
                                              MemberValueAccessBuilder memberValueAccessBuilder)
 {
     // TODO should pass along validator factory?
     return(GetPropertyValidator(type,
                                 propertyInfo,
                                 ruleset,
                                 validationSpecificationSource,
                                 new MemberAccessValidatorBuilderFactory(memberValueAccessBuilder)));
 }
Esempio n. 10
0
        public static ValidationResults Validate <T>(T target, ValidationSpecificationSource source, params string[] rulesets)
        {
            if (rulesets == null)
            {
                throw new ArgumentNullException("rulesets");
            }

            Type targetType = target != null?target.GetType() : typeof(T);

            var resultsReturned = new ValidationResults();

            foreach (string ruleset in rulesets)
            {
                Validator validator = ValidationFactory.CreateValidator(targetType, ruleset, source);
                foreach (ValidationResult validationResult in validator.Validate(target))
                {
                    resultsReturned.AddResult(validationResult);
                }
            }
            return(resultsReturned);
        }
Esempio n. 11
0
		/// <summary>
		/// Creates an instance of PropertyValidator
		/// </summary>
		/// <param name="objectToValidate">The object to validate.</param>
		/// <param name="propertyName">The property for objectToValidate</param>
		/// <param name="ruleset">The optional ruleset to use for validation.</param>
		/// <param name="specificationSource">Whether to use validation rules from attributes, config file, or both.</param>
		public PropertyValidator(object objectToValidate, string propertyName, string ruleset, ValidationSpecificationSource specificationSource)
			:base(null, null)
		{
			_ruleset = ruleset;
			_typeToValidate = objectToValidate.GetType();
			_propertyName = propertyName;
			_specificationSource = specificationSource;
			_objectToValidate = objectToValidate;
		}
 /// <summary>
 /// Resolve a validator with the given ruleset and validation source.
 /// </summary>
 /// <param name="typeToValidate">Type to construct a validator for.</param>
 /// <param name="ruleSet">Ruleset to use, or null for the default.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> controlling which
 /// set of validator to retrieve.</param>
 public ValidatorParameter(Type typeToValidate, string ruleSet, ValidationSpecificationSource validationSource)
 {
     validatorType = typeof (Validator<>).MakeGenericType(typeToValidate);
     this.ruleSet = ruleSet;
     this.validationSource = validationSource;
 }
Esempio n. 13
0
 public static bool IsSet(this ValidationSpecificationSource source, ValidationSpecificationSource value)
 {
     return(value == (source & value));
 }
Esempio n. 14
0
 /// <summary>
 /// Creates the <see cref="ValidatorParameter"/> object which resolves
 /// using the default rule set and the given <paramref name="validationSource"/>.
 /// </summary>
 /// <param name="typeToValidate">Type to construct a validator for.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> controlling which
 /// set of validator to retrieve.</param>
 public ValidatorParameter(Type typeToValidate, ValidationSpecificationSource validationSource)
     : this(typeToValidate, null, validationSource)
 {
 }
Esempio n. 15
0
 /// <summary>
 /// Returns a validator representing the validation criteria specified for type <paramref name="targetType"/>
 /// through configuration and attributes on type <paramref name="targetType"/> and its ancestors for the supplied ruleset
 /// retrieving configuration information from the supplied <see cref="IConfigurationSource"/>.
 /// </summary>
 /// <param name="targetType">The type to get the validator for.</param>
 /// <param name="ruleset">The name of the required ruleset.</param>
 /// <param name="configurationSource">The configuration source from where configuration information is to be retrieved.</param>
 /// <param name="source">The source of validation information.</param>
 /// <returns>The validator.</returns>
 /// <exception cref="ArgumentNullException">when the <paramref name="ruleset"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentNullException">when the <paramref name="configurationSource"/> is <see langword="null"/>.</exception>
 public static Validator CreateValidator(Type targetType, string ruleset, IConfigurationSource configurationSource, ValidationSpecificationSource source)
 {
     return(GetValidatorFactory(configurationSource, source).CreateValidator(targetType, ruleset));
 }
 /// <summary>
 /// Create a new instance of <see cref="ValidationSpecificationSourcePolicy"/>
 /// with the <see cref="Source"/> property initialized to the given value.
 /// </summary>
 /// <param name="source">The desired <see cref="ValidationSpecificationSource"/>.</param>
 public ValidationSpecificationSourcePolicy(ValidationSpecificationSource source)
 {
     Source = source;
 }
Esempio n. 17
0
        internal static Validator GetPropertyValidator(Type type, PropertyInfo propertyInfo, string ruleset, ValidationSpecificationSource validationSpecificationSource, MemberAccessValidatorBuilderFactory memberAccessValidatorBuilderFactory)
        {
            if (type == null)
            {
                throw new InvalidOperationException(Resources.ExceptionTypeNotFound);
            }
            if (propertyInfo == null)
            {
                throw new InvalidOperationException(Resources.ExceptionPropertyNotFound);
            }
            if (!propertyInfo.CanRead)
            {
                throw new InvalidOperationException(Resources.ExceptionPropertyNotReadable);
            }
            switch (validationSpecificationSource)
            {
            case ValidationSpecificationSource.Both:
                return(PropertyValidationFactory.GetPropertyValidator(type, propertyInfo, ruleset, memberAccessValidatorBuilderFactory));

            case ValidationSpecificationSource.Attributes:
                return(PropertyValidationFactory.GetPropertyValidatorFromAttributes(type, propertyInfo, ruleset, memberAccessValidatorBuilderFactory));

            default:
                return(null);
            }
        }
        private static ValidatorFactory GetValidatorFactory(IConfigurationSource configurationSource, ValidationSpecificationSource source)
        {
            List <ValidatorFactory> factories = new List <ValidatorFactory>();

            if (source.IsSet(ValidationSpecificationSource.Attributes))
            {
                factories.Add(DefaultAttributeValidatorFactory);
            }
            if (source.IsSet(ValidationSpecificationSource.Configuration))
            {
                factories.Add(new ConfigurationValidatorFactory(configurationSource));
            }
            if (source.IsSet(ValidationSpecificationSource.DataAnnotations))
            {
                factories.Add(DefaultValidationAttributeValidatorFactory);
            }

            if (factories.Count == 1)
            {
                return(factories[0]);
            }

            return(CreateCompositeValidatorFactory(factories));
        }
 /// <summary>
 /// Create a new instance of <see cref="ValidatorResolver"/> with the given
 /// parameters.
 /// </summary>
 /// <param name="ruleSet">Rule set name, or null to use default.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> to use when resolving.</param>
 /// <param name="validatorType">Type of validator to resolve.</param>
 public ValidatorResolver(string ruleSet, ValidationSpecificationSource validationSource, Type validatorType)
 {
     this.ruleSet = ruleSet;
     this.validationSource = validationSource;
     this.validatorType = validatorType;
 }
Esempio n. 20
0
 /// <summary>
 /// Resolve a validator with the given ruleset and validation source.
 /// </summary>
 /// <param name="typeToValidate">Type to construct a validator for.</param>
 /// <param name="ruleSet">Ruleset to use, or null for the default.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> controlling which
 /// set of validator to retrieve.</param>
 public ValidatorParameter(Type typeToValidate, string ruleSet, ValidationSpecificationSource validationSource)
 {
     validatorType         = typeof(Validator <>).MakeGenericType(typeToValidate);
     this.ruleSet          = ruleSet;
     this.validationSource = validationSource;
 }
 /// <summary>
 /// Create a new instance of <see cref="ValidationSpecificationSourcePolicy"/>
 /// with the <see cref="Source"/> property initialized to the given value.
 /// </summary>
 /// <param name="source">The desired <see cref="ValidationSpecificationSource"/>.</param>
 public ValidationSpecificationSourcePolicy(ValidationSpecificationSource source)
 {
     Source = source;
 }
 /// <summary>
 /// Creates the <see cref="ValidatorParameter"/> object which resolves
 /// using the default rule set and the given <paramref name="validationSource"/>.
 /// </summary>
 /// <param name="typeToValidate">Type to construct a validator for.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> controlling which
 /// set of validator to retrieve.</param>
 public ValidatorParameter(Type typeToValidate, ValidationSpecificationSource validationSource)
     : this(typeToValidate, null, validationSource)
 {
 }
Esempio n. 23
0
 /// <summary>
 /// Create a new instance of <see cref="ValidatorResolver"/> with the given
 /// parameters.
 /// </summary>
 /// <param name="ruleSet">Rule set name, or null to use default.</param>
 /// <param name="validationSource"><see cref="ValidationSpecificationSource"/> to use when resolving.</param>
 /// <param name="validatorType">Type of validator to resolve.</param>
 public ValidatorResolver(string ruleSet, ValidationSpecificationSource validationSource, Type validatorType)
 {
     this.ruleSet          = ruleSet;
     this.validationSource = validationSource;
     this.validatorType    = validatorType;
 }
 public static bool IsSet(this ValidationSpecificationSource source, ValidationSpecificationSource value)
 {
     return value == (source & value);
 }
 /// <summary>
 /// Create a new instance of <see cref="InjectionValidationSource"/>, specifying
 /// the validation source.
 /// </summary>
 /// <param name="source">Source of validation metadata.</param>
 public InjectionValidationSource(ValidationSpecificationSource source)
 {
     Source = source;
 }
Esempio n. 26
0
 /// <summary>
 /// Create a new instance of <see cref="InjectionValidationSource"/>, specifying
 /// the validation source.
 /// </summary>
 /// <param name="source">Source of validation metadata.</param>
 public InjectionValidationSource(ValidationSpecificationSource source)
 {
     Source = source;
 }
Esempio n. 27
0
 /// <summary>
 /// Returns a validator representing the validation criteria specified for type <typeparamref name="T"/>
 /// through configuration and attributes on type <typeparamref name="T"/> and its ancestors for the default ruleset.
 /// </summary>
 /// <typeparam name="T">The type to get the validator for.</typeparam>
 /// <param name="source">The source of validation information.</param>
 /// <returns>The validator.</returns>
 public static Validator <T> CreateValidator <T>(ValidationSpecificationSource source)
 {
     return(GetValidatorFactory(source).CreateValidator <T>());
 }
 public MockIntegrationProxy(object rawValue,
                             string ruleset,
                             ValidationSpecificationSource specificationSource,
                             string validatedPropertyName,
                             Type validatedType)
 {
     this.rawValue = rawValue;
     rulesetField = ruleset;
     specificationSourceField = specificationSource;
     validatedPropertyNameField = validatedPropertyName;
     validatedTypeField = validatedType;
 }
Esempio n. 29
0
 /// <summary>
 /// Returns a validator representing the validation criteria specified for type <typeparamref name="T"/>
 /// through configuration and attributes on type <typeparamref name="T"/> and its ancestors for the supplied ruleset
 /// retrieving configuration information from the supplied <see cref="IConfigurationSource"/>.
 /// </summary>
 /// <typeparam name="T">The type to get the validator for.</typeparam>
 /// <param name="ruleset">The name of the required ruleset.</param>
 /// <param name="configurationSource">The configuration source from where configuration information is to be retrieved.</param>
 /// <param name="source">The source of validation information.</param>
 /// <returns>The validator.</returns>
 /// <exception cref="ArgumentNullException">when the <paramref name="ruleset"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentNullException">when the <paramref name="configurationSource"/> is <see langword="null"/>.</exception>
 public static Validator <T> CreateValidator <T>(string ruleset, IConfigurationSource configurationSource, ValidationSpecificationSource source)
 {
     return(GetValidatorFactory(configurationSource, source).CreateValidator <T>(ruleset));
 }
Esempio n. 30
0
        public static void SetUsingSource(FrameworkElement element, ValidationSpecificationSource property)
        {
            if (element == null) throw new ArgumentNullException("element");

            element.SetValue(UsingSourceProperty, property);
        }
Esempio n. 31
0
 /// <summary>
 /// Returns a validator representing the validation criteria specified for type <paramref name="targetType"/>
 /// through configuration and aatributes on type <paramref name="targetType"/> and its ancestors for the default ruleset.
 /// </summary>
 /// <param name="targetType">The type to get the validator for.</param>
 /// <param name="source">The source of validation information.</param>
 /// <returns>The validator.</returns>
 public static Validator CreateValidator(Type targetType, ValidationSpecificationSource source)
 {
     return(GetValidatorFactory(source).CreateValidator(targetType));
 }