Beispiel #1
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            SpecificationBase = specificationContainer.TryGetSpecification <TSpecification>() as Validates <TProperty> ??
                                new TSpecification();

            return(base.Validate(context, specificationContainer, notification));
        }
Beispiel #2
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            StringBuilder sb = new StringBuilder();

            if (context.PropertyValue.IsNullOrDefault())
            {
                return(true);
            }

            foreach (var value in context.PropertyValue)
            {
                if (!_forEachPredicate(value))
                {
                    sb.AppendLine(CreateErrorMessage(value));
                }
            }

            if (sb.Length > 0)
            {
                Message = sb.ToString();
                notification.Errors.Add(ValidationResultFactory.Create(this, context, null, null));
                return(false);
            }
            else
            {
                return(true);
            }
        }
 public List<ValidationResult> Validate(object instance, SpecificationContainer specificationContainer)
 {
     lock (this)
     {
         return PropertyValidators.SelectMany(x => x.Validate(instance, specificationContainer)).ToList();
     }
 }
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            //Resolve the Specification
            Specification = specificationContainer.TryGetSpecification <TSpecification>() as Validates <TCollectionType> ??
                            new TSpecification();

            return(base.Validate(context, specificationContainer, notification));
        }
Beispiel #5
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var lessThanEqualTo = (TProperty)Params[0].GetParamValue(context);

            Comparer <TProperty> comparer = System.Collections.Generic.Comparer <TProperty> .Default;

            return(Evaluate(comparer.Compare(context.PropertyValue, lessThanEqualTo) <= 0, context, notification));
        }
Beispiel #6
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var floor   = (TProperty)Params[0].GetParamValue(context);
            var ceiling = (TProperty)Params[1].GetParamValue(context);
            Comparer <TProperty> comparer = System.Collections.Generic.Comparer <TProperty> .Default;

            return(Evaluate(comparer.Compare(context.PropertyValue, ceiling) <= 0 && comparer.Compare(context.PropertyValue, floor) >= 0, context, notification));
        }
Beispiel #7
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var regexPattern = (string)Params[0].GetParamValue(context);

            Regex regex   = new Regex(regexPattern);
            bool  isMatch = regex.IsMatch(context.PropertyValue);

            return(Evaluate(isMatch, context, notification));
        }
        internal static void SetUnitOfWorkId(IDependencyResolver resolver)
        {
            if (resolver != resolverInternal)
            {
                unitOfWorkSpecificationContainer = null;
            }

            resolverInternal = resolver;
        }
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="specificationContainer">
        /// The specification container.
        /// </param>
        /// <param name="notification">
        /// The notification.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Validate(
            RuleValidatorContext <T, DateTime?> context,
            SpecificationContainer specificationContainer,
            ValidationNotification notification)
        {
            var isValid = context.PropertyValue.ToDateOnly() >= this.startDate && context.PropertyValue.ToDateOnly() <= DateTime.Today;

            return(this.Evaluate(isValid, context, notification));
        }
Beispiel #10
0
 public bool Validate(object instance, SpecificationContainer specificationContainer, ValidationNotification notification)
 {
     lock (this)
     {
         foreach (var validator in PropertyValidators)
         {
             validator.Validate(instance, specificationContainer, notification);
         }
         return notification.IsValid;
     }
 }
Beispiel #11
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            int length = String.IsNullOrEmpty(context.PropertyValue) ? 0 : context.PropertyValue.Length;

            var contextWithLength = new RuleValidatorContext <T, string>(context.Instance, context.PropertyName, length.ToString(),
                                                                         context.PropertyInfo, context.Level, null);

            var max = (int)Params[0].GetParamValue(context);

            return(Evaluate(length <= max, contextWithLength, notification));
        }
Beispiel #12
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            //Guard against null in Regex Match
            if (context.PropertyValue == null)
            {
                return(Evaluate(false, context, notification));
            }

            Match m = new Regex(@"^[a-zA-Z\s]+$").Match(context.PropertyValue);

            return(Evaluate(m.Success, context, notification));
        }
Beispiel #13
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            //Guard against null in Regex Match
            if (context.PropertyValue == null)
            {
                return(Evaluate(false, context, notification));
            }

            //use a regex over Convert.ToInt32 because the string could potentially be bigger than an integer
            Match m = new Regex(@"^\d+$").Match(context.PropertyValue);

            return(Evaluate(m.Success, context, notification));
        }
Beispiel #14
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            int count = 0;

            if (context.PropertyValue != null)
            {
                foreach (var value in context.PropertyValue)
                {
                    count++;
                    break;
                }
            }

            return(Evaluate(count == 0, context, notification));
        }
Beispiel #15
0
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="specificationContainer">
        /// The specification container.
        /// </param>
        /// <param name="notification">
        /// The notification.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Validate(
            RuleValidatorContext <T, string> context,
            SpecificationContainer specificationContainer,
            ValidationNotification notification)
        {
            if (string.IsNullOrEmpty(context.PropertyValue))
            {
                return(false);
            }

            var nameRegex = new Regex(this.RegexPattern);
            var isValid   = nameRegex.IsMatch(context.PropertyValue);

            return(this.Evaluate(isValid, context, notification));
        }
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var countGreaterThanEqualTo = (int)Params[0].GetParamValue(context);

            int collectionCount = 0;

            if (context.PropertyValue != null)
            {
                foreach (var value in context.PropertyValue)
                {
                    collectionCount++;
                }
            }

            return(Evaluate(collectionCount >= countGreaterThanEqualTo, context, notification));
        }
Beispiel #17
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            bool contains = false;

            var containsParamVal = Params[0].GetParamValue(context);

            foreach (var value in context.PropertyValue)
            {
                if (value.Equals(containsParamVal))
                {
                    contains = true;
                    break;
                }
            }

            return(Evaluate(contains, context, notification));
        }
        private void FillContextContainer()
        {
            if (resolverInternal == null)
            {
                resolverInternal =
                    ServiceLocator.Instance.GetRootContainer().Resolve(typeof(IDependencyResolver), null) as IDependencyResolver;
            }

            var creator = new SpecificationCreator(resolverInternal);

            if (unitOfWorkSpecificationContainer != null)
            {
                this.SpecificationContainer.Add(unitOfWorkSpecificationContainer.GetAllSpecifications());
                return;
            }

            ScanForSpecification(true);

            var instances = creator.CreateSpecifications(foundSpecifications);

            this.SpecificationContainer.Add(instances);
            unitOfWorkSpecificationContainer = this.SpecificationContainer;
        }
Beispiel #19
0
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="specificationContainer">
        /// The specification container.
        /// </param>
        /// <param name="notification">
        /// The notification.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Validate(
            RuleValidatorContext <T, int> context,
            SpecificationContainer specificationContainer,
            ValidationNotification notification)
        {
            var id = context.PropertyValue;

            if (id <= 0 || id > 999999999)
            {
                return(this.Evaluate(false, context, notification));
            }

            // fill with leading zeros to a length of 9 digits
            var idString = id.ToString("000000000");

            // define the multiplicator
            const string MultiplicatorString = "54327654";

            // create integer arrays for multiplication
            var idDigits = new int[idString.Length];

            for (var i = 0; i < idString.Length; i++)
            {
                idDigits[i] = int.Parse(idString[i].ToString(CultureInfo.InvariantCulture));
            }

            var multiplicatorDigits = new int[MultiplicatorString.Length];

            for (var i = 0; i < MultiplicatorString.Length; i++)
            {
                multiplicatorDigits[i] = int.Parse(MultiplicatorString[i].ToString(CultureInfo.InvariantCulture));
            }

            // Generate the product sum
            var productSum = 0;

            for (var i = 0; i < MultiplicatorString.Length; i++)
            {
                productSum += idDigits[i] * multiplicatorDigits[i];
            }

            // modulo 11
            var result = productSum % 11;

            // Check the result: if result is 1, the checkdigit as 11 minus result would be 10. Such a number is not used, hence it is illegal
            if (result == 1)
            {
                return(this.Evaluate(false, context, notification));
            }

            var checkDigit = 0;

            if (result > 1)
            {
                checkDigit = 11 - result;
            }

            var isValid = checkDigit == idDigits[8];

            return(this.Evaluate(isValid, context, notification));
        }
Beispiel #20
0
 public override bool Validate(RuleValidatorContext <T, bool> context, SpecificationContainer specificationContainer, ValidationNotification notification)
 {
     return(Evaluate(!context.PropertyValue, context, notification));
 }
Beispiel #21
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var set = (IEnumerable <TProperty>)Params[0].GetParamValue(context);

            return(Evaluate(context.PropertyValue != null && set.Contains(context.PropertyValue), context, notification));
        }
 public abstract List<ValidationResult> Validate(object instance, RuleValidatorContext parentRuleContexts, SpecificationContainer specificationContainer);
Beispiel #23
0
 public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
 {
     return(Evaluate(!context.PropertyValue.IsNullOrDefault(), context, notification));
 }
 public abstract List<ValidationResult> Validate(object instance, SpecificationContainer specificationContainer);
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            ValidationResult collectionValidationResult = null;

            //Check if the Collection is null/default
            if (context.PropertyValue.IsNullOrDefault())
            {
                return(true);
            }


            var itemsNestedValidationResult = new List <ValidationResult>();

            var propertyEnumerable = ((IEnumerable)(context.PropertyValue));

            if (propertyEnumerable == null)
            {
                throw new ArgumentException("Property must be IEnumerable");
            }

            int index = 1;

            foreach (var item in propertyEnumerable)
            {
                if (item == null)
                {
                    throw new NullReferenceException(string.Format("One of the items is null in the list {0} of the instance {1}", context.PropertyName, context.Instance.GetType().FullName));
                }

                var innerNotfication = new ValidationNotification();

                Type objectType = item.GetType();

                var specification = specificationContainer.GetSpecification(objectType);

                if (!specification.Validate(item, specificationContainer, innerNotfication))
                {
                    var propertyName = String.IsNullOrEmpty(ItemName) ? item.GetType().Name : ItemName;

                    Message = String.Format("{0} {1} in {2} is invalid.", propertyName, index, context.PropertyName);

                    var childContext = new RuleValidatorContext(item, propertyName, item, context.Level,
                                                                item.GetType() as MemberInfo, context);

                    var itemError = ValidationResultFactory.Create(this, childContext, new List <object>(), MessageKey);

                    //var itemError = ValidationResultFactory.Create(this, context, Parameters, MessageKey);
                    itemError.NestedValidationResults = innerNotfication.Errors;
                    itemsNestedValidationResult.Add(itemError);
                }
                index++;
            }

            if (itemsNestedValidationResult.Any())
            {
                //Errors were found on at least one item in the collection to return a ValidationResult for the Collection property
                Message = "{PropertyName} is invalid.";
                collectionValidationResult = ValidationResultFactory.Create(this, context, new List <object>(), MessageKey);
                collectionValidationResult.NestedValidationResults = itemsNestedValidationResult;
                notification.Errors.Add(collectionValidationResult);
                return(false);
            }
            else
            {
                return(true);
            }
        }
Beispiel #26
0
 public override bool Validate(RuleValidatorContext <T, DateTime> context, SpecificationContainer specificationContainer, ValidationNotification notification)
 {
     return(Evaluate(context.PropertyValue < DateTime.Now, context, notification));
 }
Beispiel #27
0
 public abstract bool Validate(object instance, RuleValidatorContext parentRuleContexts, SpecificationContainer specificationContainer, ValidationNotification notification);
Beispiel #28
0
        /// <summary>
        /// The validate.
        ///     1.Move the four initial characters to the end of the string
        ///     2.Replace the letters in the string with digits, expanding the string as necessary, such that A=10, B=11 and Z=35.
        ///     3.Convert the string to an integer and mod-97 the entire number
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="specificationContainer">
        /// The specification container.
        /// </param>
        /// <param name="notification">
        /// The notification.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Validate(
            RuleValidatorContext <T, string> context,
            SpecificationContainer specificationContainer,
            ValidationNotification notification)
        {
            if (context.PropertyValue == null)
            {
                return(false);
            }

            var ibanValue = context.PropertyValue.Replace(" ", string.Empty);

            if (ibanValue.Length > 34)
            {
                return(this.Evaluate(false, context, notification));
            }

            // CH & LI Validator
            var countryIso2 = ibanValue.Substring(0, 2);

            if (countryIso2 == "CH" || countryIso2 == "LI")
            {
                if (ibanValue.Length != 21)
                {
                    return(this.Evaluate(false, context, notification));
                }
            }

            // General IBAN Validator
            if (System.Text.RegularExpressions.Regex.IsMatch(ibanValue, "^[A-Z0-9]"))
            {
                ibanValue = ibanValue.Replace(" ", string.Empty);
                var       iban       = ibanValue.Substring(4, ibanValue.Length - 4) + ibanValue.Substring(0, 4);
                const int AsciiShift = 55;
                var       sb         = new StringBuilder();
                foreach (var c in iban)
                {
                    int v;
                    if (char.IsLetter(c))
                    {
                        v = c - AsciiShift;
                    }
                    else
                    {
                        v = int.Parse(c.ToString(CultureInfo.InvariantCulture));
                    }

                    sb.Append(v);
                }

                var checkSumString = sb.ToString();
                var checksum       = int.Parse(checkSumString.Substring(0, 1));
                for (var i = 1; i < checkSumString.Length; i++)
                {
                    var v = int.Parse(checkSumString.Substring(i, 1));
                    checksum *= 10;
                    checksum += v;
                    checksum %= 97;
                }

                var isValid = checksum == 1;
                return(this.Evaluate(isValid, context, notification));
            }

            return(this.Evaluate(false, context, notification));
        }
Beispiel #29
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var result = _expression(context.Instance, context.PropertyValue);

            return(Evaluate(result, context, notification));
        }
Beispiel #30
0
 public abstract bool Validate(object instance, SpecificationContainer specificationContainer, ValidationNotification notification);
Beispiel #31
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            _dups = duplicateItems(context.PropertyValue);

            return(Evaluate(_dups == string.Empty, context, notification));
        }