Example #1
0
        public static IRuleOut <string> Email(this IRuleIn <string> @this, EmailValidationMode mode = EmailValidationMode.ComplexRegex)
        {
            if (!Enum.IsDefined(typeof(EmailValidationMode), mode))
            {
                throw new ArgumentException("Invalid EmailValidationMode value", nameof(mode));
            }

            if (mode == EmailValidationMode.DataAnnotationsCompatible)
            {
                return(@this.RuleTemplate(IsEmailValidAccordingToDataAnnotations, MessageKey.Texts.Email));
            }

            return(@this.RuleTemplate(IsEmailValidAccordingToRegex, MessageKey.Texts.Email));
        }
Example #2
0
 public static IRuleOut <string> HasCharacter(this IRuleIn <string> @this)
 {
     return(@this.RuleTemplate(
                value => value.Length > 0,
                "Must have at least one character!"
                ));
 }
Example #3
0
        public static IRuleOut <TCollection> MinCollectionSize <TCollection, TItem>(this IRuleIn <TCollection> @this, int min)
            where TCollection : IEnumerable <TItem>
        {
            ThrowHelper.BelowZero(min, nameof(min));

            return(@this.RuleTemplate(m => m.Count() >= min, MessageKey.Collections.MinCollectionSize, Arg.Number(nameof(min), min)));
        }
Example #4
0
        public static IRuleOut <TCollection> ExactCollectionSize <TCollection, TItem>(this IRuleIn <TCollection> @this, int size)
            where TCollection : IEnumerable <TItem>
        {
            ThrowHelper.BelowZero(size, nameof(size));

            return(@this.RuleTemplate(m => m.Count() == size, MessageKey.Collections.ExactCollectionSize, Arg.Number(nameof(size), size)));
        }
Example #5
0
 public static IRuleOut <string> ExactLinesCount(this IRuleIn <string> @this, int count)
 {
     return(@this.RuleTemplate(
                value => value.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Length == count,
                "Must contain exactly {count} lines",
                Arg.Number("count", count)
                ));
 }
Example #6
0
 public static IRuleOut <string> HasCharacter(
     this IRuleIn <string> @this,
     char character,
     int count = 1)
 {
     return(@this.RuleTemplate(
                value => value.Count(c => c == character) == count,
                "Text.HasCharacter",
                Arg.Text(nameof(character), character),
                Arg.Number(nameof(count), count)
                ));
 }
Example #7
0
        public static IRuleOut <string> LengthBetween(this IRuleIn <string> @this, int min, int max)
        {
            ThrowHelper.BelowZero(min, nameof(min));
            ThrowHelper.BelowZero(max, nameof(max));
            ThrowHelper.InvalidRange(min, nameof(min), max, nameof(max));

            return(@this.RuleTemplate(
                       v =>
            {
                var squashedLength = v.Replace(Environment.NewLine, " ").Length;

                return squashedLength >= min && squashedLength <= max;
            },
                       MessageKey.Texts.LengthBetween,
                       Arg.Number(nameof(min), min),
                       Arg.Number(nameof(max), max)));
        }
Example #8
0
        public static IRuleOut <TCollection> CollectionSizeBetween <TCollection, TItem>(this IRuleIn <TCollection> @this, int min, int max)
            where TCollection : IEnumerable <TItem>
        {
            ThrowHelper.BelowZero(min, nameof(min));
            ThrowHelper.BelowZero(max, nameof(max));

            ThrowHelper.InvalidRange(min, nameof(min), max, nameof(max));

            return(@this.RuleTemplate(
                       m =>
            {
                var count = m.Count();

                return count >= min && count <= max;
            },
                       MessageKey.Collections.CollectionSizeBetween,
                       Arg.Number(nameof(min), min),
                       Arg.Number(nameof(max), max)));
        }
Example #9
0
 public static IRuleOut <TimeSpan> EqualTo(this IRuleIn <TimeSpan> @this, TimeSpan value)
 {
     return(@this.RuleTemplate(m => m == value, MessageKey.TimeSpanType.EqualTo, Arg.Time(nameof(value), value)));
 }
Example #10
0
 public static IRuleOut <ushort> NonPositive(this IRuleIn <ushort> @this)
 {
     return(@this.RuleTemplate(m => m <= 0, MessageKey.Numbers.NonPositive));
 }
Example #11
0
 public static IRuleOut <ushort?> NotEqualTo(this IRuleIn <ushort?> @this, ushort value)
 {
     return(@this.RuleTemplate(m => m.Value != value, MessageKey.Numbers.NotEqualTo, Arg.Number(nameof(value), value)));
 }
Example #12
0
        public static IRuleOut <short?> BetweenOrEqualTo(this IRuleIn <short?> @this, short min, short max)
        {
            ThrowHelper.InvalidRange(min, nameof(min), max, nameof(max));

            return(@this.RuleTemplate(m => m.Value >= min && m.Value <= max, MessageKey.Numbers.BetweenOrEqualTo, Arg.Number(nameof(min), min), Arg.Number(nameof(max), max)));
        }
Example #13
0
 public static IRuleOut <TimeSpan?> GreaterThanOrEqualTo(this IRuleIn <TimeSpan?> @this, TimeSpan min)
 {
     return(@this.RuleTemplate(m => m.Value >= min, MessageKey.TimeSpanType.GreaterThanOrEqualTo, Arg.Time(nameof(min), min)));
 }
Example #14
0
 public static IRuleOut <short> LessThan(this IRuleIn <short> @this, short max)
 {
     return(@this.RuleTemplate(m => m < max, MessageKey.Numbers.LessThan, Arg.Number(nameof(max), max)));
 }
Example #15
0
        public static IRuleOut <short> Between(this IRuleIn <short> @this, short min, short max)
        {
            ThrowHelper.InvalidRange(min, nameof(min), max, nameof(max));

            return(@this.RuleTemplate(m => m > min && m < max, MessageKey.Numbers.Between, Arg.Number(nameof(min), min), Arg.Number(nameof(max), max)));
        }
Example #16
0
 public static IRuleOut <short> GreaterThan(this IRuleIn <short> @this, short min)
 {
     return(@this.RuleTemplate(m => m > min, MessageKey.Numbers.GreaterThan, Arg.Number(nameof(min), min)));
 }
Example #17
0
 public static IRuleOut <short> Negative(this IRuleIn <short> @this)
 {
     return(@this.RuleTemplate(m => m < 0, MessageKey.Numbers.Negative));
 }
Example #18
0
        public static IRuleOut <TimeSpan> Between(this IRuleIn <TimeSpan> @this, TimeSpan min, TimeSpan max)
        {
            ThrowHelper.InvalidRange(min.Ticks, nameof(min), max.Ticks, nameof(max));

            return(@this.RuleTemplate(m => m > min && m < max, MessageKey.TimeSpanType.Between, Arg.Time(nameof(min), min), Arg.Time(nameof(max), max)));
        }
Example #19
0
 public static IRuleOut <TimeSpan?> LessThanOrEqualTo(this IRuleIn <TimeSpan?> @this, TimeSpan max)
 {
     return(@this.RuleTemplate(m => m.Value <= max, MessageKey.TimeSpanType.LessThanOrEqualTo, Arg.Time(nameof(max), max)));
 }
Example #20
0
 public static IRuleOut <TimeSpan> LessThan(this IRuleIn <TimeSpan> @this, TimeSpan max)
 {
     return(@this.RuleTemplate(m => m < max, MessageKey.TimeSpanType.LessThan, Arg.Time(nameof(max), max)));
 }
Example #21
0
 public static IRuleOut <short?> NonNegative(this IRuleIn <short?> @this)
 {
     return(@this.RuleTemplate(m => m.Value >= 0, MessageKey.Numbers.NonNegative));
 }
Example #22
0
        public static IRuleOut <TimeSpan?> BetweenOrEqualTo(this IRuleIn <TimeSpan?> @this, TimeSpan min, TimeSpan max)
        {
            ThrowHelper.InvalidRange(min.Ticks, nameof(min), max.Ticks, nameof(max));

            return(@this.RuleTemplate(m => m.Value >= min && m.Value <= max, MessageKey.TimeSpanType.BetweenOrEqualTo, Arg.Time(nameof(min), min), Arg.Time(nameof(max), max)));
        }
Example #23
0
 public static IRuleOut <short?> GreaterThanOrEqualTo(this IRuleIn <short?> @this, short min)
 {
     return(@this.RuleTemplate(m => m.Value >= min, MessageKey.Numbers.GreaterThanOrEqualTo, Arg.Number(nameof(min), min)));
 }
Example #24
0
 public static IRuleOut <TimeSpan> NonZero(this IRuleIn <TimeSpan> @this)
 {
     return(@this.RuleTemplate(m => m.Ticks != 0, MessageKey.TimeSpanType.NonZero));
 }
Example #25
0
 public static IRuleOut <short?> LessThanOrEqualTo(this IRuleIn <short?> @this, short max)
 {
     return(@this.RuleTemplate(m => m.Value <= max, MessageKey.Numbers.LessThanOrEqualTo, Arg.Number(nameof(max), max)));
 }
Example #26
0
 public static IRuleOut <char?> NotEqualToIgnoreCase(this IRuleIn <char?> @this, char value)
 {
     return(@this.RuleTemplate(v => string.Compare(v.Value.ToString(CultureInfo.InvariantCulture).ToUpperInvariant(), value.ToString(CultureInfo.InvariantCulture).ToUpperInvariant(), StringComparison.Ordinal) != 0, MessageKey.CharType.NotEqualToIgnoreCase, Arg.Text(nameof(value), value)));
 }
Example #27
0
 public static IRuleOut <short> EqualTo(this IRuleIn <short> @this, short value)
 {
     return(@this.RuleTemplate(m => m == value, MessageKey.Numbers.EqualTo, Arg.Number(nameof(value), value)));
 }
Example #28
0
 public static IRuleOut <ushort?> NonZero(this IRuleIn <ushort?> @this)
 {
     return(@this.RuleTemplate(m => m.Value != 0, MessageKey.Numbers.NonZero));
 }
Example #29
0
 public static IRuleOut <short> NonZero(this IRuleIn <short> @this)
 {
     return(@this.RuleTemplate(m => m != 0, MessageKey.Numbers.NonZero));
 }
Example #30
0
 public static IRuleOut <ushort?> Positive(this IRuleIn <ushort?> @this)
 {
     return(@this.RuleTemplate(m => m.Value > 0, MessageKey.Numbers.Positive));
 }