/// <summary>
        /// Merges the specified messages.
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>
        /// The merged message.
        /// </returns>
        public MergedValidationMessage Merge(IValidatable validationSource, string propertyName = null)
        {
            validationSource.CannotBeNull();

            ValidationMessageCollection messages = new ValidationMessageCollection();

            if (propertyName == null)
            {
                foreach (var validationMessage in this)
                {
                    if (validationMessage.ValidationSource == validationSource)
                    {
                        messages.Add(validationMessage);
                    }
                }
            }
            else
            {
                foreach (var validationMessage in this)
                {
                    if (validationMessage.ValidationSource == validationSource &&
                        validationMessage.PropertyName == propertyName)
                    {
                        messages.Add(validationMessage);
                    }
                }
            }

            return(Merge(messages));
        }
示例#2
0
        /// <summary>
        /// Validates the specified property of the specified validation source.
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The collection of validation mesasges.</returns>
        public static ValidationMessageCollection Validate(this IValidatable validationSource, string propertyName)
        {
            validationSource.CannotBeNull();
            propertyName.CannotBeNullOrEmpty();

            ValidationMessageCollection messages = new ValidationMessageCollection();
            List <string> validationContexts;
            object        propertyValue;

            // get property value
            var properties = ReflectionExtensions.GetProperties(validationSource.GetType());

            if (properties.TryGetValue(propertyName, out PropertyData propertyData))
            {
                if (propertyData.PropertyInfo.CanRead &&
                    propertyData.PropertyInfo.CanWrite)
                {
                    propertyValue = propertyData.PropertyInfo.GetValue(validationSource);

                    // get validation context
                    validationContexts = new List <string>();
                    validationContexts.Add(ValidationContext.Default);                                                     // always add the default validation context
                    validationContexts.AddRange(validationSource.GetActiveValidationContexts() ?? Array.Empty <string>()); // add currently active validation context

                    foreach (var validationContext in validationContexts.Distinct())
                    {
                        messages.AddRange(validationSource.Validate(propertyName, propertyValue, validationContext));
                    }
                }
            }

            return(messages);
        }
示例#3
0
        /// <summary>
        /// Checks if the specified property of the specified validation source is valid.
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>
        ///   <c>true</c> if the specified property name is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValid(this IValidatable validationSource, string propertyName)
        {
            propertyName.CannotBeNull();
            validationSource.CannotBeNull();

            return(!validationSource.Validate(propertyName).Any(x => x.ValidationLevel == ValidationLevel.Error));
        }
示例#4
0
        /// <summary>
        /// Validates the the specified validation source.
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <returns>
        /// The collection of validation mesasges.
        /// </returns>
        public static ValidationMessageCollection Validate(this IValidatable validationSource)
        {
            validationSource.CannotBeNull();

            ValidationMessageCollection messages = new ValidationMessageCollection();
            var propertyNames = ReflectionExtensions.GetProperties(validationSource.GetType()).Keys;

            foreach (var propertyName in propertyNames)
            {
                messages.AddRange(validationSource.Validate(propertyName));
            }

            return(messages);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationMessage" /> class.
        /// </summary>
        /// <param name="messageKey">The message key.</param>
        /// <param name="defaultMessage">The default message.</param>
        /// <param name="messageParameters">The message parameters.</param>
        /// <param name="validationSource">The validation source.</param>
        /// <param name="propertyName">The validated property name.</param>
        /// <param name="validationLevel">The level of validation.</param>
        /// <param name="validationContext">The validation context.</param>
        /// <param name="validationPriority">The validation priority.</param>
        public ValidationMessage(string messageKey, string defaultMessage, IEnumerable <object> messageParameters, IValidatable validationSource, string propertyName, ValidationLevel validationLevel = ValidationLevel.Error, string validationContext = ValidationFramework.ValidationContext.Default, int validationPriority = 0)
            : this()
        {
            messageKey.CannotBeNullOrEmpty();
            defaultMessage.CannotBeNullOrEmpty();
            validationSource.CannotBeNull();
            propertyName.CannotBeNullOrEmpty();

            this.Message            = FormatMessage(messageKey, defaultMessage, messageParameters);
            this.ValidationLevel    = validationLevel;
            this.ValidationSource   = validationSource;
            this.PropertyName       = propertyName;
            this.ValidationContext  = validationContext;
            this.ValidationPriority = validationPriority;
        }
示例#6
0
        /// <summary>
        /// Validates the specified property of the specified validation source for the specified property value in specified validation context by using validation attributes.
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="validationContext">The validation context.</param>
        /// <returns>
        /// The collection of validation mesasges.
        /// </returns>
        public static ValidationMessageCollection ValidateAttributes(this IValidatable validationSource, string propertyName, object propertyValue, string validationContext)
        {
            validationSource.CannotBeNull();
            propertyName.CannotBeNullOrEmpty();

            ValidationMessageCollection messages = new ValidationMessageCollection();
            var  validationAttributes            = ReflectionExtensions.GetProperties(validationSource.GetType())[propertyName].ValidationAttributes;
            bool isValid;

            // perform attribute based validation
            foreach (var validationAttribute in validationAttributes)
            {
                // only matching validation context gets validated
                if (validationAttribute.ValidationContext == validationContext)
                {
                    // custom validators might cause exceptions that are hard to find
                    try
                    {
                        isValid = validationAttribute.IsValid(propertyValue);
                    }
                    catch (Exception ex)
                    {
                        throw new ValidationErrorException("Unhandled validation exception occurred.", validationAttribute.GetType(), validationSource.GetType(), propertyName, ex);
                    }

                    if (!isValid)
                    {
                        var messageKey         = validationAttribute.MessageKey ?? validationAttribute.GetDefaultMessageKey() ?? "UndefinedMessageKey";
                        var message            = validationAttribute.Message ?? validationAttribute.GetDefaultMessage() ?? "Undefined message.";
                        var messageParameters  = validationAttribute.MessageParameters;
                        var validationLevel    = validationAttribute.ValidationLevel;
                        var validationPriority = validationAttribute.ValidationPriority;

                        // value is invalid -> add it to the list
                        messages.Add(new ValidationMessage(messageKey, message, messageParameters, validationSource, propertyName, validationLevel, validationContext, validationPriority));
                    }
                }
            }

            return(messages);
        }
示例#7
0
        /// <summary>
        /// Checks if the specified specified validation source is valid (has no invalid properties).
        /// </summary>
        /// <param name="validationSource">The validation source.</param>
        /// <returns>
        ///   <c>true</c> if the specified validation source is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValid(this IValidatable validationSource)
        {
            validationSource.CannotBeNull();

            return(!validationSource.Validate().Any(x => x.ValidationLevel == ValidationLevel.Error));
        }