コード例 #1
0
        public static ValidationRule <string> FileExists(this ValidationRule <string> rule, string message = "File must exist")
        {
            rule.NotNullOrWhiteSpace();

            if (!File.Exists(rule.Value))
            {
                throw new FileNotFoundException(message, rule.Value);
            }

            return(rule);
        }
コード例 #2
0
        /// <summary>
        /// Checks that the given string is equal to the passed string.
        /// </summary>
        /// <param name="rule">rule on which it should be asserted.</param>
        /// <param name="value">The value the asserted value should have</param>
        /// <param name="comparison">The comparison behavior</param>
        /// <returns>The validation rule.</returns>
        /// /// <exception cref="ArgumentException">If the asserted string isn't equal to the given strign</exception>
        public static ValidationRule <string> Equal(this ValidationRule <string> rule, string value,
                                                    StringComparison comparison, string message = null)
        {
            if (string.Equals(rule.Value, value, comparison))
            {
                return(rule);
            }

            message = message ?? "Value must not be null or empty!";
            throw new ArgumentException(rule.Name, message);
        }
コード例 #3
0
        /// <summary>
        /// Asserts that the given string is at least as long as the given length.
        /// </summary>
        /// <param name="rule">rule on which it should be asserted.</param>
        /// <param name="length">minimal length of the string.</param>
        /// <returns>The validation rule.</returns>
        public static ValidationRule <string> MinLength(this ValidationRule <string> rule, int length, string message = null)
        {
            if (!string.IsNullOrWhiteSpace(rule.Value) && rule.Value.Length >= length)
            {
                return(rule);
            }

            message = message ?? $"The value should be at least {length} characters long!";

            throw new ArgumentOutOfRangeException(rule.Name, rule.Value.Length, message);
        }
コード例 #4
0
        /// <summary>
        /// Checks that the given string is not null or whitespace
        /// </summary>
        /// <param name="rule">rule on which it should be asserted.</param>
        /// <returns>The validation rule.</returns>
        /// <exception cref="ArgumentNullException">If the asserted string is null</exception>
        /// <exception cref="ArgumentException">If the asserted string is empty or whitespaces only</exception>
        public static ValidationRule <string> NotNullOrWhiteSpace(this ValidationRule <string> rule)
        {
            if (rule.Value == null)
            {
                throw new ArgumentNullException(rule.Name, "Value must not be null!");
            }

            if (string.IsNullOrWhiteSpace(rule.Value))
            {
                throw new ArgumentException(rule.Name, "Value must not be null or empty!");
            }

            return(rule);
        }
コード例 #5
0
        public static ValidationRule <ICollection <T> > NotNullOrEmpty <T>(this ValidationRule <ICollection <T> > rule)
        {
            if (rule.Value == null)
            {
                throw new ArgumentException("The collection must not be null!", rule.Name);
            }

            if (!rule.Value.HasAny())
            {
                throw new ArgumentException("The enumerable should have items!", rule.Name);
            }

            return(rule);
        }
コード例 #6
0
        public static ValidationRule <ICollection> HasExact(this ValidationRule <ICollection> rule, int expectedCount, string message = null)
        {
            if (rule.Value.Count == expectedCount)
            {
                return(rule);
            }

            if (message == null)
            {
                message = $"The collection must have exact {expectedCount} items.";
            }

            throw new ArgumentOutOfRangeException(rule.Name, rule.Value.Count, message);
        }
コード例 #7
0
        public static ValidationRule <ICollection> HasAtMost(this ValidationRule <ICollection> rule, int maximumCount, string message = null)
        {
            if (rule.Value.Count <= maximumCount)
            {
                return(rule);
            }

            if (message == null)
            {
                message = $"The collection must have at most {maximumCount} items.";
            }

            throw new ArgumentOutOfRangeException(rule.Name, rule.Value.Count, message);
        }
コード例 #8
0
        /// <summary>
        /// Checks that the given string is not null or empty
        /// </summary>
        /// <param name="rule">rule on which it should be asserted.</param>
        /// <returns>The validation rule.</returns>
        /// <exception cref="ArgumentNullException">If the asserted string is null</exception>
        /// <exception cref="ArgumentException">If the asserted string is empty</exception>
        public static ValidationRule <string> NotNullOrEmpty(this ValidationRule <string> rule, string message = null)
        {
            if (rule.Value == null)
            {
                throw new ArgumentNullException(rule.Name, message ?? "Value must not be null!");
            }

            if (string.IsNullOrEmpty(rule.Value))
            {
                throw new ArgumentException(rule.Name, message ?? "Value must not be null or empty!");
            }

            return(rule);
        }
コード例 #9
0
        public static ValidationRule <string> DirectoryExists(this ValidationRule <string> rule, string message = null)
        {
            rule.NotNullOrWhiteSpace();

            if (Directory.Exists(rule.Value))
            {
                return(rule);
            }

            if (message == null)
            {
                message = $"Directory \"{rule.Value}\" must exist";
            }

            throw new DirectoryNotFoundException(message);
        }