Example #1
0
        public static TBuilder IsNotNullOrEmpty <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator)
        {
            const string message   = "Value is null or empty but should not be";
            const string errorCode = "STR_IS_NULL_OR_EMPTY";

            propertyValidator.AddRule(x => !string.IsNullOrEmpty(x), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #2
0
        public static TBuilder IsNotNullOrWhiteSpace <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator)
        {
            var          message   = "Value is null or whitespace but should not be";
            const string errorCode = "STR_IS_NULL_OR_WHITESPACE";

            propertyValidator.AddRule(x => !string.IsNullOrWhiteSpace(x), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #3
0
        public static TBuilder IsBoolParsable <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator)
        {
            var          message   = $"It is not parsable to bool";
            const string errorCode = "STR_NOT_PARSABLE_TO_BOOL";

            propertyValidator.AddRule(x => bool.TryParse(x, out _), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #4
0
        public static TBuilder IsGuid <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator)
        {
            const string message   = "It is not guid";
            const string errorCode = "STR_IS_NOT_GUID";

            propertyValidator.AddRule(x => Guid.TryParse(x, out _), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #5
0
        public static TBuilder IsNull <TBuilder, TProperty>(this PropertyValidatorBase <TBuilder, TProperty> propertyValidator)
        {
            var          message   = "Value must be null";
            const string errorCode = "NULL";

            propertyValidator.AddRule(x => x == null, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #6
0
        public static TBuilder IsEqual <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator,
                                                  string value, StringComparison stringComparison = StringComparison.CurrentCulture)
        {
            var          message   = $"Value should be equal to {value}";
            const string errorCode = "STR_IS_NOT_EQUAL";

            propertyValidator.AddRule(x => x.Equals(value, stringComparison), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
        public static TBuilder IsGreaterThan <TBuilder, TNumber>(
            this PropertyValidatorBase <TBuilder, TNumber> propertyValidator, TNumber value)
            where TNumber : IComparable <TNumber>
        {
            var          message   = $"Value must be greater than {value}";
            const string errorCode = "NUM_LESS_OR_THAN_OR_EQUAL_TO";

            propertyValidator.AddRule(x => x.CompareTo(value) > 0, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
        public static TBuilder IsLessThanOrEqualTo <TBuilder, TNumber>(
            this PropertyValidatorBase <TBuilder, TNumber> propertyValidator, TNumber value)
            where TNumber : IComparable <TNumber>
        {
            var          message   = $"Value must be less than or equal to {value}";
            const string errorCode = "NUM_GREATER_THAN";

            propertyValidator.AddRule(x => x.CompareTo(value) <= 0, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #9
0
        public static TBuilder IsEmailAddress <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator)
        {
            const string message   = "Has invalid email format";
            const string errorCode = "STR_INVALID_EMAIL";

            var emailRegex = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";

            propertyValidator.AddRule(x => Regex.IsMatch(x, emailRegex, RegexOptions.IgnoreCase),
                                      message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #10
0
        public static TBuilder HasMaxLength <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator,
                                                       int maxLength)
        {
            if (maxLength < 0)
            {
                throw new ArgumentException($"'{nameof(maxLength)}' must be >= 0");
            }

            var          message   = $"Cannot have value greater than {maxLength}";
            const string errorCode = "STR_GREATER_THAN_MAX_LENGTH";

            propertyValidator.AddRule(x => x?.Length <= maxLength, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #11
0
        public static TBuilder HasLength <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator,
                                                    int length)
        {
            if (length < 0)
            {
                throw new ArgumentException($"'{nameof(length)}' must be >= 0");
            }

            var          message   = $"Must have length equal to {length}";
            const string errorCode = "STR_DIFFERENT_THAN_REQUIRED_LENGTH";

            propertyValidator.AddRule(x => x.Length == length, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #12
0
        public static TBuilder MatchesRegex <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator,
                                                       string regex)
        {
            try
            {
                Regex.Match("", regex);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException($"'{nameof(regex)}' represents invalid regex");
            }

            var          message   = $"Does not match regex '{regex}'";
            const string errorCode = "STR_NOT_MATCHING_TO_REGEX";

            propertyValidator.AddRule(x => Regex.IsMatch(x, regex), message, errorCode);

            return(propertyValidator.PropertyValidator);
        }
Example #13
0
        public static TBuilder HasLengthBetween <TBuilder>(this PropertyValidatorBase <TBuilder, string> propertyValidator,
                                                           int minLength, int maxLength)
        {
            if (minLength < 0 || maxLength < 0)
            {
                throw new ArgumentException($"'{nameof(minLength)}' and '{nameof(maxLength)}' must be >= 0");
            }
            if (minLength > maxLength)
            {
                throw new ArgumentException($"'{nameof(minLength)}' cannot be greater than '{nameof(maxLength)}'");
            }

            var          message   = $"Must have value in range [{minLength}, {maxLength}](inclusive)";
            const string errorCode = "STR_LENGTH_OUT_RANGE";

            propertyValidator.AddRule(x => x?.Length >= minLength &&
                                      x.Length <= maxLength, message, errorCode);

            return(propertyValidator.PropertyValidator);
        }