예제 #1
0
        protected override bool IsValid(PropertyValidatorContext context) {
            var value = context.PropertyValue as string;

            if (value == null) {
                return true;
            }

            value = value.Replace("-", "");

            int checksum = 0;
            bool evenDigit = false;

            // http://www.beachnet.com/~hstiles/cardtype.html
            foreach (char digit in value.Reverse()) {
                if (!char.IsDigit(digit)) {
                    return false;
                }

                int digitValue = (digit - '0') * (evenDigit ? 2 : 1);
                evenDigit = !evenDigit;

                while (digitValue > 0) {
                    checksum += digitValue % 10;
                    digitValue /= 10;
                }
            }

            return (checksum % 10) == 0;
        }
예제 #2
0
        private object GetComparisonValue(PropertyValidatorContext context) {
            if (func != null) {
                return func(context.Instance);
            }

            return ValueToCompare;
        }
예제 #3
0
        protected override bool IsValid(PropertyValidatorContext context) {
            if (!predicate(context.Instance, context.PropertyValue, context)) {
                return false;
            }

            return true;
        }
        public override IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) {
            if (context.Rule.Member == null) {
                throw new InvalidOperationException(string.Format("Nested validators can only be used with Member Expressions."));
            }

            var collection = context.PropertyValue as IEnumerable;

            if (collection == null) {
                yield break;
            }

            int count = 0;
            
            var predicate = Predicate ?? (x => true);

            foreach (var element in collection) {

                if(element == null || !(predicate(element))) {
                    // If an element in the validator is null then we want to skip it to prevent NullReferenceExceptions in the child validator.
                    // We still need to update the counter to ensure the indexes are correct.
                    count++;
                    continue;
                }

                var newContext = context.ParentContext.CloneForChildValidator(element);
                newContext.PropertyChain.Add(context.Rule.Member);
                newContext.PropertyChain.AddIndexer(count++);

                var results = childValidator.Validate(newContext).Errors;

                foreach (var result in results) {
                    yield return result;
                }
            }
        }
        private IComparable GetComparisonValue(PropertyValidatorContext context) {
            if(valueToCompareFunc != null) {
                return (IComparable)valueToCompareFunc(context.Instance);
            }

            return (IComparable)ValueToCompare;
        }
예제 #6
0
        protected override bool IsValid(PropertyValidatorContext context) {
            if (context.PropertyValue == null) return true;

            if (!regex.IsMatch((string)context.PropertyValue)) {
                return false;
            }

            return true;
        }
예제 #7
0
        public virtual IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) {
            context.MessageFormatter.AppendPropertyName(context.PropertyDescription);

            if (!IsValid(context)) {
                return new[] { CreateValidationError(context) };
            }

            return Enumerable.Empty<ValidationFailure>();
        }
예제 #8
0
        protected override bool IsValid(PropertyValidatorContext context) {
            var comparisonValue = GetComparisonValue(context);
            bool success = !Compare(comparisonValue, context.PropertyValue);

            if (!success) {
                context.MessageFormatter.AppendArgument("PropertyValue", context.PropertyValue);
                return false;
            }

            return true;
        }
        protected sealed override bool IsValid(PropertyValidatorContext context) {
            if(context.PropertyValue == null) {
                // If we're working with a nullable type then this rule should not be applied.
                // If you want to ensure that it's never null then a NotNull rule should also be applied. 
                return true;
            }
            
            var value = GetComparisonValue(context);

            if (!IsValid((IComparable)context.PropertyValue, value)) {
                context.MessageFormatter.AppendArgument("ComparisonValue", value);
                return false;
            }

            return true;
        }
예제 #10
0
        protected override bool IsValid(PropertyValidatorContext context) {
            var propertyValue = (IComparable)context.PropertyValue;

            // If the value is null then we abort and assume success.
            // This should not be a failure condition - only a NotNull/NotEmpty should cause a null to fail.
            if (propertyValue == null) return true;

            if (propertyValue.CompareTo(From) < 0 || propertyValue.CompareTo(To) > 0) {

                context.MessageFormatter
                    .AppendArgument("From", From)
                    .AppendArgument("To", To)
                    .AppendArgument("Value", context.PropertyValue);

                return false;
            }
            return true;
        }
예제 #11
0
        public override IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) {
            if (context.Rule.Member == null) {
                throw new InvalidOperationException(string.Format("Nested validators can only be used with Member Expressions."));
            }

            var instanceToValidate = context.PropertyValue;

            if (instanceToValidate == null) {
                return Enumerable.Empty<ValidationFailure>();
            }

            var validator = GetValidator(context);

            if(validator == null) {
                return Enumerable.Empty<ValidationFailure>();
            }

            var newContext = CreateNewValidationContextForChildValidator(instanceToValidate, context);
            var results = validator.Validate(newContext).Errors;

            return results;
        }
예제 #12
0
 public abstract IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context);
예제 #13
0
 protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext context) {
     var newContext = context.ParentContext.CloneForChildValidator(instanceToValidate);
     newContext.PropertyChain.Add(context.Rule.Member);
     return newContext;
 }
예제 #14
0
 protected abstract bool IsValid(PropertyValidatorContext context);
예제 #15
0
        /// <summary>
        /// Creates an error validation result for this validator.
        /// </summary>
        /// <param name="context">The validator context</param>
        /// <returns>Returns an error validation result.</returns>
        protected virtual ValidationFailure CreateValidationError(PropertyValidatorContext context) {
            context.MessageFormatter.AppendAdditionalArguments(
                customFormatArgs.Select(func => func(context.Instance)).ToArray()
            );

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString());

            var failure = new ValidationFailure(context.PropertyName, error, errorCode, context.PropertyValue);

            if (CustomStateProvider != null) {
                failure.CustomState = CustomStateProvider(context.Instance);
            }

            return failure;
        }
예제 #16
0
 protected virtual IValidator GetValidator(PropertyValidatorContext context) {
     return Validator;
 }
예제 #17
0
 public IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) {
     if (condition(context.Instance)) {
         return InnerValidator.Validate(context);
     }
     return Enumerable.Empty<ValidationFailure>();
 }