public static IValidationTarget <TimeSpan> IsEqual(this IValidationTarget <TimeSpan> target, TimeSpan timeSpan)
 {
     return(target.And(new DefaultValidationPredicate <TimeSpan>(v => v == timeSpan,
                                                                 ExceptionMessages.DateTimeIsSameAsFailed.Inject(
                                                                     target.Value,
                                                                     timeSpan))));
 }
 public static IValidationTarget <TimeSpan> IsNotMaximumValue(this IValidationTarget <TimeSpan> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <TimeSpan>(v => v != TimeSpan.MaxValue,
                                                                    ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                        target.Value,
                                                                        TimeSpan.MaxValue))));
 }
 public static IValidationTarget <TimeSpan> IsBeforeOrSame(this IValidationTarget <TimeSpan> target, TimeSpan timeSpan)
 {
     return(target.And(new OutOfRangeValidationPredicate <TimeSpan>(v => v <= timeSpan,
                                                                    ExceptionMessages.DateTimeIsBeforeOrSameAsFailed.Inject(
                                                                        target.Value,
                                                                        timeSpan))));
 }
Example #4
0
 public static IValidationTarget <TType> IsSize <TType>(this IValidationTarget <TType> target, int expected) where TType : ICollection
 {
     return(target.And(new DefaultValidationPredicate <TType>(v => v.Count == expected,
                                                              ExceptionMessages.CollectionsSizeIsFailed.Inject(
                                                                  expected,
                                                                  target.Value.Count))));
 }
Example #5
0
 public static IValidationTarget <ICollection <TType> > DoesNotContainValue <TType>(
     this IValidationTarget <ICollection <TType> > target,
     TType value)
 {
     return(target.And(new DefaultValidationPredicate <ICollection <TType> >(v => !v.Contains(value),
                                                                             ExceptionMessages.CollectionsDoesNotContainValueFailed)));
 }
Example #6
0
 public static IValidationTarget <double> IsNotMinimumValue(this IValidationTarget <double> target)
 {
     return(target.And(new DefaultValidationPredicate <double>(v => Math.Abs(v - double.MinValue) > m_comparisionTolerance,
                                                               ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                   target.Value,
                                                                   double.MinValue))));
 }
Example #7
0
 public static IValidationTarget <long> IsNotMinimumValue(this IValidationTarget <long> target)
 {
     return(target.And(new DefaultValidationPredicate <long>(v => v != long.MinValue,
                                                             ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                 target.Value,
                                                                 long.MinValue))));
 }
Example #8
0
 public static IValidationTarget <sbyte> IsMaximumValue(this IValidationTarget <sbyte> target)
 {
     return(target.And(new DefaultValidationPredicate <sbyte>(v => v == sbyte.MaxValue,
                                                              ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                  target.Value,
                                                                  sbyte.MaxValue))));
 }
        protected override bool Validate(IValidationTarget <Identifier> target)
        {
            if (string.IsNullOrWhiteSpace(target.Value.Value))
            {
                return(target.Error("Invalid identifer"));
            }

            if (target.Value.Trim() != target.Value)
            {
                return(target.Error("Invalid identifier"));
            }

            if (target.Value.Trim('-') != target.Value)
            {
                return(target.Error("Invalid identifier"));
            }

            if (target.Value.Length > 100)
            {
                return(target.Error("Identifier is too long"));
            }

            if (!target.Value.Value.All(c => c == '-' || char.IsLower(c) || char.IsDigit(c)))
            {
                return(target.Error("Invalid identifier"));
            }

            return(true);
        }
Example #10
0
        public bool Validate(IValidationTarget target, out List <ValidationError> errors)
        {
            errors = new List <ValidationError>();
            if (target == null)
            {
                errors.Add(new ValidationError()
                {
                    ErrorMessage = ValidationResources.ERR_Invalid_Target, SourceModule = this.ModuleName, TimeStamp = DateTime.Now
                });
                return(false);
            }

            var readerTarget = target as ReaderObject;

            if (readerTarget == null)
            {
                errors.Add(new ValidationError()
                {
                    ErrorMessage = ValidationResources.ERR_Invalid_Target, SourceModule = this.ModuleName, TimeStamp = DateTime.Now
                });
                return(false);
            }
            if (!File.Exists(readerTarget.FileName))
            {
                errors.Add(new ValidationError()
                {
                    ErrorMessage = ValidationResources.ERR_Log_File_Not_Exist, SourceModule = this.ModuleName, TimeStamp = DateTime.Now
                });
                return(false);
            }
            return(true);
        }
 public static IValidationTarget <decimal> IsMaximumValue(this IValidationTarget <decimal> target)
 {
     return(target.And(new DefaultValidationPredicate <decimal>(v => Math.Abs(v - decimal.MaxValue) < m_comparisionTolerance,
                                                                ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                    target.Value,
                                                                    decimal.MaxValue))));
 }
Example #12
0
 public static IValidationTarget <DateTime> IsAfterOrSameAsToday(this IValidationTarget <DateTime> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTime>(v => v.Date >= DateTime.Today.Date,
                                                                    ExceptionMessages.DateTimeIsAfterOrSameAsFailed.Inject(
                                                                        target.Value,
                                                                        DateTime.Now.Date))));
 }
Example #13
0
 public static IValidationTarget <DateTime> IsBeforeUtcNow(this IValidationTarget <DateTime> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTime>(v => v < DateTime.UtcNow,
                                                                    ExceptionMessages.DateTimeIsBeforeFailed.Inject(
                                                                        target.Value,
                                                                        DateTime.UtcNow))));
 }
Example #14
0
 public static IValidationTarget <DateTime> IsAfterOrSame(this IValidationTarget <DateTime> target, DateTime dateTime)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTime>(v => v >= dateTime,
                                                                    ExceptionMessages.DateTimeIsAfterOrSameAsFailed.Inject(
                                                                        target.Value,
                                                                        dateTime))));
 }
Example #15
0
 public static IValidationTarget <DateTime> IsNotEqualToNow(this IValidationTarget <DateTime> target)
 {
     return(target.And(new DefaultValidationPredicate <DateTime>(v => v != DateTime.Now,
                                                                 ExceptionMessages.DateTimeIsNotSameAsFailed.Inject(
                                                                     target.Value,
                                                                     DateTime.Now))));
 }
Example #16
0
 public static IValidationTarget <DateTime> IsEqual(this IValidationTarget <DateTime> target, DateTime dateTime)
 {
     return(target.And(new DefaultValidationPredicate <DateTime>(v => v == dateTime,
                                                                 ExceptionMessages.DateTimeIsSameAsFailed.Inject(
                                                                     target.Value,
                                                                     dateTime))));
 }
Example #17
0
 public static IValidationTarget <IDictionary <TKey, TType> > DoesNotContainKey <TKey, TType>(
     this IValidationTarget <IDictionary <TKey, TType> > target,
     Func <TKey, bool> predicate)
 {
     return(target.And(new DefaultValidationPredicate <IDictionary <TKey, TType> >(v => !v.Keys.All(predicate),
                                                                                   ExceptionMessages.DictionaryNotAnyKeyFailed)));
 }
Example #18
0
 public static IValidationTarget <IDictionary <TKey, TType> > DoesNotContainValue <TKey, TType>(
     this IValidationTarget <IDictionary <TKey, TType> > target,
     Func <TType, bool> predicate)
 {
     return(target.And(new DefaultValidationPredicate <IDictionary <TKey, TType> >(v => !v.Values.Any(predicate),
                                                                                   ExceptionMessages.CollectionsNotAnyFailed)));
 }
 public static IValidationTarget <float> IsNotMinimumValue(this IValidationTarget <float> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <float>(v => Math.Abs(v - float.MinValue) > m_comparisionTolerance,
                                                                 ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                     target.Value,
                                                                     float.MinValue))));
 }
        protected override bool Validate(IValidationTarget <EmailAddress> target)
        {
            if (string.IsNullOrWhiteSpace(target.Value.Value))
            {
                return(target.Error("Invalid email address"));
            }

            if (target.Value.Trim() != target.Value)
            {
                return(target.Error("Invalid email address"));
            }

            if (target.Value.Length > 50)
            {
                return(target.Error("Email address is too long"));
            }

            try
            {
                if (new MailAddress(target.Value.Value).Address != target.Value.Value)
                {
                    return(target.Error("Invalid email address"));
                }

                return(true);
            }
            catch (FormatException)
            {
                return(target.Error("Invalid email address"));
            }
        }
Example #21
0
 public static IValidationTarget <DateTimeOffset> IsAfterOrSameAsUtcNow(this IValidationTarget <DateTimeOffset> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTimeOffset>(v => v >= DateTimeOffset.UtcNow,
                                                                          ExceptionMessages.DateTimeIsAfterOrSameAsFailed.Inject(
                                                                              target.Value,
                                                                              DateTimeOffset.UtcNow))));
 }
Example #22
0
        public void Validate(IValidationProvider validation, IValidationTarget <T> target)
        {
            if (target.Value == null)
            {
                return;
            }

            foreach (var member in memberValidators)
            {
                member.Validate(validation, target);
                if (!target.Continue)
                {
                    return;
                }
            }

            foreach (var type in typeValidators)
            {
                type.Validate(validation, target);
                if (!target.Continue)
                {
                    return;
                }
            }
        }
Example #23
0
 public static IValidationTarget <DateTimeOffset> IsBeforeToday(this IValidationTarget <DateTimeOffset> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTimeOffset>(v => v.Date < DateTimeOffset.Now.Date,
                                                                          ExceptionMessages.DateTimeIsBeforeFailed.Inject(
                                                                              target.Value,
                                                                              DateTimeOffset.Now.Date))));
 }
Example #24
0
 public static IValidationTarget <DateTimeOffset> IsNotEqualToToday(this IValidationTarget <DateTimeOffset> target)
 {
     return(target.And(new DefaultValidationPredicate <DateTimeOffset>(v => v.Date != DateTimeOffset.Now.Date,
                                                                       ExceptionMessages.DateTimeIsNotSameAsFailed.Inject(
                                                                           target.Value,
                                                                           DateTimeOffset.Now.Date))));
 }
Example #25
0
 public static IValidationTarget <DateTimeOffset> IsNotMinimumValue(this IValidationTarget <DateTimeOffset> target)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTimeOffset>(v => v != DateTimeOffset.MinValue,
                                                                          ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                              target.Value,
                                                                              DateTimeOffset.MinValue))));
 }
Example #26
0
 public static IValidationTarget <DateTimeOffset> IsNotEqual(this IValidationTarget <DateTimeOffset> target, DateTimeOffset offset)
 {
     return(target.And(new DefaultValidationPredicate <DateTimeOffset>(v => v != offset,
                                                                       ExceptionMessages.DateTimeIsNotSameAsFailed.Inject(
                                                                           target.Value,
                                                                           offset))));
 }
Example #27
0
 public static IValidationTarget <DateTimeOffset> IsEqualToUtcNow(this IValidationTarget <DateTimeOffset> target)
 {
     return(target.And(new DefaultValidationPredicate <DateTimeOffset>(v => v == DateTimeOffset.UtcNow,
                                                                       ExceptionMessages.DateTimeIsSameAsFailed.Inject(
                                                                           target.Value,
                                                                           DateTimeOffset.UtcNow))));
 }
Example #28
0
 public static IValidationTarget <DateTimeOffset> IsBeforeOrSame(this IValidationTarget <DateTimeOffset> target, DateTimeOffset offset)
 {
     return(target.And(new OutOfRangeValidationPredicate <DateTimeOffset>(v => v <= offset,
                                                                          ExceptionMessages.DateTimeIsBeforeOrSameAsFailed.Inject(
                                                                              target.Value,
                                                                              offset))));
 }
Example #29
0
 public static IValidationTarget <int> IsNotMaximumValue(this IValidationTarget <int> target)
 {
     return(target.And(new DefaultValidationPredicate <int>(v => v != int.MaxValue,
                                                            ExceptionMessages.NumbersIsMaxValueFailed.Inject(
                                                                target.Value,
                                                                int.MaxValue))));
 }
        public ValidationRule(IValidationTarget <TType> target)
        {
            target.ThrowIfNull();

            m_target     = target;
            m_conditions = new ConcurrentList <Predicate <TType> >();
            m_predicates = new ValidationPredicateSet <TType>(this);
        }
        public ValidationRule(IValidationTarget target, Func<RuleResult> validateDelegate,
            Func<Task<RuleResult>> asyncValidateAction)
        {
            Contract.Requires(target != null);
            Contract.Requires(validateDelegate != null || asyncValidateAction != null);

            Target = target;
            ValidateDelegate = validateDelegate;
            AsyncValidateAction = asyncValidateAction ?? (() => Task.Factory.StartNew(() => ValidateDelegate()));
        }
Example #32
0
        public ValidationRule(IValidationTarget target, Func<RuleResult> validateDelegate, Func<Task<RuleResult>> asyncValidateAction)
        {
            Guard.NotNull(target, nameof(target));
            Guard.Assert(validateDelegate != null || asyncValidateAction != null,
                "validateDelegate != null || asyncValidateAction != null");

            Target = target;
            ValidateDelegate = validateDelegate;
            AsyncValidateAction = asyncValidateAction ?? (() => Task.Run(() => ValidateDelegate()));
            Settings = new ValidationRuleSettings();
        }
        private IAsyncValidationRule AddRuleCore(IValidationTarget target, Func<RuleResult> validateDelegate,
            Func<Task<RuleResult>> asyncValidateAction)
        {
            var rule = new ValidationRule(target, validateDelegate, asyncValidateAction);

            RegisterValidationRule(rule);

            return rule;
        }