Exemplo n.º 1
0
        /// <summary>
        /// Gets the collection of registered <see cref="ModelValidator"/> instances.
        /// </summary>
        /// <param name="actionContext">The context.</param>
        /// <param name="metadata">The metadata.</param>
        /// <returns>A collection of registered <see cref="ModelValidator"/> instances.</returns>
        public static IEnumerable <ModelValidator> GetValidators(this HttpActionContext actionContext, ModelMetadata metadata)
        {
            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            IModelValidatorCache validatorCache = actionContext.GetValidatorCache();

            return(actionContext.GetValidators(metadata, validatorCache));
        }
Exemplo n.º 2
0
 internal static IEnumerable <ModelValidator> GetValidators(
     this HttpActionContext actionContext,
     ModelMetadata metadata,
     IModelValidatorCache validatorCache
     )
 {
     if (validatorCache == null)
     {
         // slow path: there is no validator cache on the configuration
         return(metadata.GetValidators(actionContext.GetValidatorProviders()));
     }
     else
     {
         return(validatorCache.GetValidators(metadata));
     }
 }
Exemplo n.º 3
0
        // Validates a single node (not including children)
        // Returns true if validation passes successfully
        private static bool ShallowValidate(ModelMetadata metadata, ValidationContext validationContext, object container)
        {
            Contract.Requires(validationContext != null);

            bool   isValid = true;
            string key     = null;
            IModelValidatorCache validatorCache = validationContext.ValidatorCache;

            ModelValidator[] validators;
            if (validatorCache == null)
            {
                // slow path: there is no validator cache on the configuration
                validators = metadata.GetValidators(validationContext.ValidatorProviders).AsArray();
            }
            else
            {
                validators = validatorCache.GetValidators(metadata).AsArray();
            }

            for (int index = 0; index < validators.Length; index++)
            {
                ModelValidator validator = validators[index];
                foreach (ModelValidationResult error in validator.Validate(metadata, container))
                {
                    if (key == null)
                    {
                        key = validationContext.RootPrefix;
                        foreach (IKeyBuilder keyBuilder in validationContext.KeyBuilders.Reverse())
                        {
                            key = keyBuilder.AppendTo(key);
                        }
                    }

                    validationContext.ModelState.AddModelError(key, error.Message);
                    isValid = false;
                }
            }

            return(isValid);
        }