/// <summary>
        /// Creates parameter validators.
        /// </summary>
        /// <param name="parameterInfo">The parameter info of the parameter for which to create the validator.</param>
        /// <param name="parameterValue">The parameter value.</param>
        /// <returns>The created validator.</returns>
        /// <remarks>
        /// The parameter validator must know the actual type of the parameter value.
        /// EL has a bug where they get the type of the parameter from the parameter info instead of the parameter itself -
        /// the parameter may be of a derived type of the formal parameter.
        /// </remarks>
        static Validator CreateParameterValidator(
            ParameterInfo parameterInfo,
            object parameterValue)
        {
            if (parameterInfo == null)
            {
                throw new ArgumentNullException(nameof(parameterInfo));
            }

            var parameterElement = new MetadataValidatedParameterElement();

            // gets the validators metadata
            parameterElement.UpdateFlyweight(parameterInfo);

            // compositions all validators
            var compositeBuilder = new CompositeValidatorBuilder(parameterElement);

            foreach (var descriptor in parameterElement.GetValidatorDescriptors())
            {
                // get the type of the parameter either from the parameter value or from the parameter info if the value is null
                compositeBuilder.AddValueValidator(
                    descriptor.CreateValidator(
                        parameterValue != null
                            ? parameterValue.GetType()
                            : parameterInfo.ParameterType,
                        null,
                        null,
                        ValidationFactory.DefaultCompositeValidatorFactory));
            }

            // returns all validators
            return(compositeBuilder.GetValidator());
        }
Esempio n. 2
0
        public void ShouldReturnDescriptorsWhenUpdatedWithParameterInfo()
        {
            MetadataValidatedParameterElement validatedElement = new MetadataValidatedParameterElement();

            validatedElement.UpdateFlyweight(GetMethod1NameParameterInfo());
            Assert.IsNull(validatedElement.MemberInfo);
            Assert.IsNull(validatedElement.TargetType);
            Assert.AreEqual(CompositionType.And, validatedElement.CompositionType);
            Assert.IsFalse(validatedElement.IgnoreNulls);
            List <IValidatorDescriptor> descriptors =
                new List <IValidatorDescriptor>(validatedElement.GetValidatorDescriptors());

            Assert.AreEqual(3, descriptors.Count);
            // reflection doesn't preserve ordering of attributes. Therefore,
            // it doesn't preserve ordering of validators. Is this an issue?
            Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
            {
                return(d is NotNullValidatorAttribute);
            }));
            Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
            {
                return(d is StringLengthValidatorAttribute);
            }));
            Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
            {
                return(d is RegexValidatorAttribute);
            }));
        }
 public void ShouldReturnDescriptorsWhenUpdatedWithParameterInfo()
 {
     MetadataValidatedParameterElement validatedElement = new MetadataValidatedParameterElement();
     validatedElement.UpdateFlyweight(GetMethod1NameParameterInfo());
     Assert.IsNull(validatedElement.MemberInfo);
     Assert.IsNull(validatedElement.TargetType);
     Assert.AreEqual(CompositionType.And, validatedElement.CompositionType);
     Assert.IsFalse(validatedElement.IgnoreNulls);
     List<IValidatorDescriptor> descriptors =
         new List<IValidatorDescriptor>(validatedElement.GetValidatorDescriptors());
     Assert.AreEqual(3, descriptors.Count);
     // reflection doesn't preserve ordering of attributes. Therefore,
     // it doesn't preserve ordering of validators. Is this an issue?
     Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
                                      {
                                          return d is NotNullValidatorAttribute;
                                      }));
     Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
                                      {
                                          return d is StringLengthValidatorAttribute;
                                      }));
     Assert.IsTrue(descriptors.Exists(delegate(IValidatorDescriptor d)
                                      {
                                          return d is RegexValidatorAttribute;
                                      }));
 }
Esempio n. 4
0
 public static Validator CreateValidator(ParameterInfo paramInfo)
 {
     MetadataValidatedParameterElement validatedElement = new MetadataValidatedParameterElement();
     validatedElement.UpdateFlyweight(paramInfo);
     CompositeValidatorBuilder builder = new CompositeValidatorBuilder(validatedElement);
     foreach (IValidatorDescriptor descriptor in validatedElement.GetValidatorDescriptors())
     {
         builder.AddValueValidator(descriptor.CreateValidator(paramInfo.ParameterType, null, null));
     }
     return builder.GetValidator();
 }
 public void ShouldBeCleanAtCreation()
 {
     MetadataValidatedParameterElement validatedElement =
         new MetadataValidatedParameterElement();
     Assert.IsNull(validatedElement.MemberInfo);
     Assert.IsNull(validatedElement.TargetType);
     Assert.AreEqual(CompositionType.And, validatedElement.CompositionType);
     Assert.IsFalse(validatedElement.IgnoreNulls);
     List<IValidatorDescriptor> descriptors =
         new List<IValidatorDescriptor>(validatedElement.GetValidatorDescriptors());
     Assert.AreEqual(0, descriptors.Count);
 }
Esempio n. 6
0
        public void ShouldBeCleanAtCreation()
        {
            MetadataValidatedParameterElement validatedElement =
                new MetadataValidatedParameterElement();

            Assert.IsNull(validatedElement.MemberInfo);
            Assert.IsNull(validatedElement.TargetType);
            Assert.AreEqual(CompositionType.And, validatedElement.CompositionType);
            Assert.IsFalse(validatedElement.IgnoreNulls);
            List <IValidatorDescriptor> descriptors =
                new List <IValidatorDescriptor>(validatedElement.GetValidatorDescriptors());

            Assert.AreEqual(0, descriptors.Count);
        }