Example #1
0
        public async Task <IValidationResult> ValidateAsync <TModel>(TModel model)
        {
            IValidatableModel validatable = model as IValidatableModel;

            if (validatable != null)
            {
                if (validatable.IsValid != null)
                {
                    return(new ValidationResult(validatable.IsValid.Value));
                }
            }

            Type   modelType = typeof(TModel);
            object validationHandler;

            if (TryGetValidationHandler(modelType, out validationHandler))
            {
                IValidationHandler <TModel> validator = (IValidationHandler <TModel>)validationHandler;
                IValidationResult           result    = await validator.HandleAsync(model);

                if (validatable != null)
                {
                    validatable.IsValid = result.IsValid;
                }

                return(result);
            }

            return(ProcessMissingHandler(model));
        }
        /// <summary>
        /// Gets the validation context for a complete object graph by also checking the properties and recursive
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The validation context for the whole object graph.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static IValidationContext GetValidationContextForObjectGraph(this IValidatableModel model)
        {
            Argument.IsNotNull("model", model);

            var validationContext = new ValidationContext();

            validationContext.AddModelValidation(model, new List <IValidatableModel>());

            return(validationContext);
        }
        public override System.Collections.Generic.IEnumerable <ModelValidationResult> Validate(object container)
        {
            IValidatableModel model = base.Metadata.Model as IValidatableModel;

            System.Collections.Generic.IEnumerable <ModelValidationResult> result;
            if (model == null)
            {
                result = System.Linq.Enumerable.Empty <ModelValidationResult>();
            }
            else
            {
                result = model.ValidateModel();
            }
            return(result);
        }
        public static ValidationError FirstErrorOnProperty(this IValidatableModel validatable, string propertyName)
        {
            try
            {
                var context = new ValidationContext(validatable)
                {
                    MemberName = propertyName
                };

                var propertyInfo      = validatable.GetType().GetRuntimeProperty(propertyName);
                var value             = propertyInfo.GetValue(validatable);
                var validationResults = new List <ValidationResult>();
                var isValid           = Validator.TryValidateProperty(value, context, validationResults);

                return(validationResults.FirstOrDefault());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception thrown in FirstErrorOnProperty");
                Console.WriteLine(ex);
                return(null);
            }
        }
        public static void CheckModelState(this IValidatableModel validatable)
        {
            try
            {
                if (validatable.Errors == null)
                {
                    validatable.Errors = new ObservableErrorCollection();
                }

                var context = new ValidationContext(validatable);
                var results = new List <ValidationResult>();
                var valid   = Validator.TryValidateObject(validatable, context, results, true);
                validatable.Errors.ReplaceRange(from r in results
                                                where !string.IsNullOrWhiteSpace(r.ErrorMessage)
                                                select(ValidationError) r);

                validatable.IsValid = !validatable.Errors.Any();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception thrown in CheckModelState");
                Console.WriteLine(ex);
            }
        }
 void Validate(IValidatableModel user)
 {
     Validator.ValidateUser(user as User);
 }
        private static void AddModelValidation(this ValidationContext validationContext, IValidatableModel model, List <IValidatableModel> handledModels)
        {
            Argument.IsNotNull("validationContext", validationContext);

            if (handledModels.Any(x => ReferenceEquals(x, model)))
            {
                return;
            }

            handledModels.Add(model);

            validationContext.SynchronizeWithContext(model.ValidationContext, true);

            var propertyDataManager = PropertyDataManager.Default;
            var catelTypeInfo       = propertyDataManager.GetCatelTypeInfo(model.GetType());

            foreach (var property in catelTypeInfo.GetCatelProperties())
            {
                var propertyValue = model.GetValue(property.Key);
                var enumerable    = propertyValue as IEnumerable;
                if (enumerable != null && !(propertyValue is string))
                {
                    foreach (var item in enumerable)
                    {
                        var modelItem = item as IValidatableModel;
                        if (modelItem != null)
                        {
                            validationContext.AddModelValidation(modelItem, handledModels);
                        }
                    }
                }

                var propertyModel = propertyValue as IValidatableModel;
                if (propertyModel != null)
                {
                    validationContext.AddModelValidation(propertyModel, handledModels);
                }
            }
        }
Example #8
0
 void Validate(IValidatableModel partner)
 {
     Validator.ValidatePartner(partner as Partner);
 }