コード例 #1
0
		public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container) {
			if (metadata.Model != null) {
				var selector = new DefaultValidatorSelector();
				var context = new ValidationContext(metadata.Model, new PropertyChain(), selector);

				var result = validator.Validate(context);

				if (!result.IsValid) {
					return ConvertValidationResultToModelValidationResults(result);
				}
			}
			return Enumerable.Empty<ModelValidationResult>();
		}
コード例 #2
0
        /// <summary>
        /// Validates a single node (not including children)
        /// </summary>
        /// <param name="metadata">Model Metadata</param>
        /// <param name="validationContext">Validation Context</param>
        /// <param name="container">The container.</param>
        /// <returns>True if validation passes successfully</returns>
        private static bool ShallowValidate(ModelMetadata metadata, InternalValidationContext validationContext, object container)
        {
            bool isValid = true;
            string key = null;

            // Use the DependencyResolver to find any validators appropriate for this type
            IEnumerable<IValidator> validators = validationContext.Provider.GetValidators(metadata.ModelType, validationContext.Scope);

            foreach (IValidator validator in validators)
            {
                IValidatorSelector selector = new DefaultValidatorSelector();
                ValidationContext context = new ValidationContext(metadata.Model, new PropertyChain(), selector);

                ValidationResult result = validator.Validate(context);
                foreach (var error in result.Errors)
                {
                    if (key == null)
                    {
                        key = validationContext.RootPrefix;
                        foreach (IKeyBuilder keyBuilder in validationContext.KeyBuilders.Reverse())
                        {
                            key = keyBuilder.AppendTo(key);
                        }

                        // Avoid adding model errors if the model state already contains model errors for that key
                        // We can't perform this check earlier because we compute the key string only when we detect an error
                        if (!validationContext.ModelState.IsValidField(key))
                        {
                            return false;
                        }
                    }
                    validationContext.ModelState.AddModelError(key, JsonConvert.SerializeObject(error));
                    isValid = false;
                }
            }
            return isValid;
        }
        /// <summary>
        /// Validates a single node (not including children)
        /// </summary>
        /// <param name="metadata">Model Metadata</param>
        /// <param name="validationContext">Validation Context</param>
        /// <param name="container">The container.</param>
        /// <returns>True if validation passes successfully</returns>
        private static bool ShallowValidate(ModelMetadata metadata, InternalValidationContext validationContext, object container)
        {
            bool isValid = true;

            // Use the DependencyResolver to find any validators appropriate for this type
            IEnumerable<IValidator> validators = validationContext.Provider.GetValidators(metadata.ModelType, validationContext.Scope);

            foreach (IValidator validator in validators)
            {
                IValidatorSelector selector = new DefaultValidatorSelector();
                var context = new ValidationContext(metadata.Model, new PropertyChain(), selector);

                ValidationResult result = validator.Validate(context);

                foreach (ValidationFailure error in result.Errors)
                {
                    if (!validationContext.ModelState.ContainsKey(error.PropertyName))
                    {
                        validationContext.ModelState.Add(error.PropertyName, new ModelState
                        {
                            Value = new ValueProviderResult(error.AttemptedValue, error.AttemptedValue.ToString(), CultureInfo.CurrentCulture)
                        });
                    }

                    validationContext.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                    isValid = false;
                }
            }
            return isValid;
        }