예제 #1
0
        /// <summary>
        /// Run the validation rules contained in <code>validators</code>
        /// for element <code>element</code>. Throw an exception if validation
        /// fails.
        /// </summary>
        /// <typeparam name="T">the type of the element to validate.</typeparam>
        /// <typeparam name="TK">the type of the UI element keys on which errors must be displayed.</typeparam>
        /// <typeparam name="TV">the type of the error messages.</typeparam>
        /// <param name="element">the element to validate.</param>
        /// <param name="validators">the validators.</param>
        /// <exception cref="ArgumentNullException">if any of the method parameters is <code>null</code>.</exception>
        /// <exception cref="GojulValidationException{TK, TV}">if the validation fails.</exception>
        public static async Task ValidateAsync <T, TK, TV>(T element, IEnumerable <IGojulValidator <T, TK, TV> > validators)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element is null");
            }

            Condition.Requires(validators).IsNotNull();

            var errorMessageContainer = new GojulValidationErrorMessageContainer <TK, TV>();

            foreach (var v in validators)
            {
                await v.ValidateAsync(element, errorMessageContainer).ConfigureAwait(false);
            }

            if (errorMessageContainer.HasErrors)
            {
                throw new GojulValidationException <TK, TV>("Validation failed", errorMessageContainer);
            }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="message">the error message.</param>
 /// <param name="errorMessageContainer">the error message container which is the cause of the error.</param>
 /// <exception cref="ArgumentNullException">if <code>errorMessageContainer</code> is <code>null</code></exception>.
 public GojulValidationException(string message, GojulValidationErrorMessageContainer <TK, TV> errorMessageContainer) : base(message)
 {
     Condition.Requires(errorMessageContainer).IsNotNull();
     this.ErrorMessageContainer = errorMessageContainer;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="errorMessageContainer">the error message container which is the cause of the error.</param>
 /// <exception cref="ArgumentNullException">if <code>errorMessageContainer</code> is <code>null</code></exception>.
 public GojulValidationException(GojulValidationErrorMessageContainer <TK, TV> errorMessageContainer) : this("A validation error occurred", errorMessageContainer)
 {
 }