Exemplo n.º 1
0
 private static IEnumerable <ModelValidator> GetValidators(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.º 2
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);
        }