コード例 #1
0
        /// <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());
        }
 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;
                                      }));
 }
コード例 #3
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);
            }));
        }
コード例 #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();
 }