コード例 #1
0
 /// <summary>
 ///   Checks value is greater
 /// </summary>
 public static IValidationCondition <TValue> IsGreater <TValue>(
     this IValidationCondition <TValue> condition,
     TValue comparable)
     where TValue : IComparable <TValue>
 {
     return(condition.Is(value => value.CompareTo(comparable) > 0));
 }
コード例 #2
0
 /// <summary>
 ///   Checks if value is not match regex
 /// </summary>
 public static IValidationCondition <string> IsNotMatch(
     this IValidationCondition <string> condition,
     string regex,
     RegexOptions options = RegexOptions.None)
 {
     return(condition.IsNot(value => Regex.IsMatch(value, regex, options)));
 }
コード例 #3
0
        /// <summary>
        ///   Adds <see cref="ValidationDetail" /> to <see cref="IValidationContext" /> with specified
        ///   <see cref="ValidationSeverity" />
        /// </summary>
        /// <exception cref="ValidationConditionException">
        ///   Throws when ValidationDetail.ValidationSeverity greater ValidationContext.ValidationSeverity.
        ///   Example: Add fatal detail, when validation context severity is error
        /// </exception>
        public static ValidationDetail?AddValidationDetail(
            this IValidationCondition condition,
            string validationMessage,
            ValidationSeverity validationSeverity = ValidationSeverity.Error)
        {
            var validationContext = condition.ValidationContext;

            var validationDetail = new ValidationDetail(
                validationContext,
                condition.ValidationKey,
                validationMessage,
                validationSeverity,
                condition.IsValid ?? false);

            // Not null or false
            // Null by default, so validationContext.When(...).AddValidationDetail(...) works without additional conditions
            if (validationDetail.IsValid)
            {
                return(validationDetail);
            }

            validationContext.ValidationDetails.Add(validationDetail);

            if (validationDetail.ValidationSeverity > validationContext.ValidationSeverity)
            {
                throw new ValidationConditionException(validationDetail);
            }

            return(validationDetail);
        }
コード例 #4
0
 /// <summary>
 ///   Checks value is less or equal
 /// </summary>
 public static IValidationCondition <TValue> IsLessOrEqual <TValue>(
     this IValidationCondition <TValue> condition,
     TValue comparable)
     where TValue : IComparable <TValue>
 {
     return(condition.Is(value => value.CompareTo(comparable) <= 0));
 }
コード例 #5
0
        public ValidationConditionEntity MapFromInterface(IValidationCondition validationCondition)
        {
            this.ApplicationID = validationCondition.ApplicationID;
            this.Expression    = validationCondition.Expression;

            return(this);
        }
コード例 #6
0
        public override IValidationCondition CreateValidationCondition(IValidationCondition validationCondition)
        {
            ValidationConditionEntity validationConditionEntity = _ctx.ValidationConditions.Add((ValidationConditionEntity)validationCondition);

            _ctx.SaveChanges();

            return(validationConditionEntity);
        }
コード例 #7
0
 /// <summary>
 ///   Checks value is not in range (min: inclusive, max: exclusive)
 /// </summary>
 public static IValidationCondition <TValue> IsNotInRange <TValue>(
     this IValidationCondition <TValue> condition,
     TValue min,
     TValue max)
     where TValue : IComparable <TValue>
 {
     return(condition.IsNot(value => value.CompareTo(min) >= 0 && value.CompareTo(max) < 0));
 }
コード例 #8
0
        public IResponse UpdateValidationCondition(ValidationCondition validationCondition)
        {
            GuardianOptions      options      = GuardianOptionsFactory.GetOptions();
            GuardianDataProvider dataProvider = options.GuardianDataProviderFactory();

            IValidationCondition createdValidation = dataProvider.UpdateValidationCondition(validationCondition);

            return(new JsonResponse(dataProvider.GetValidationCondition(createdValidation.ValidationConditionID)));
        }
コード例 #9
0
        public override IValidationCondition CreateValidationCondition(IValidationCondition validationCondition)
        {
            ValidationConditionEntity newValidationConditionEntity = new ValidationConditionEntity().MapFromInterface(validationCondition);

            newValidationConditionEntity = _ctx.ValidationConditions.Add(newValidationConditionEntity);
            _ctx.SaveChanges();

            return(newValidationConditionEntity);
        }
コード例 #10
0
        /// <summary>
        /// Adds a Property based Condition to the rule. The Property is added to the Condition if not provided by the condition.
        /// </summary>
        /// <param name="condition">The condition that will be validated</param>
        /// <returns>A Property bsed ValidationRule</returns>
        public virtual IValidationRule <T, TPrp> AddCondition(IValidationCondition <T, TPrp> condition)
        {
            if (condition.Property == null)
            {
                condition.Property = _property;
            }

            ValidationConditions.Add(condition);

            return(this);
        }
コード例 #11
0
        public override IValidationCondition UpdateValidationCondition(IValidationCondition validationCondition)
        {
            ValidationConditionEntity validationConditionEntity = getValidationCondition(validationCondition.ValidationConditionID);

            validationConditionEntity.ApplicationID      = validationCondition.ApplicationID;
            validationConditionEntity.DateModifiedOffset = DateTimeOffset.UtcNow;;
            validationConditionEntity.Expression         = validationCondition.Expression;

            _ctx.SaveChanges();

            return(validationConditionEntity);
        }
コード例 #12
0
        public static ValidationConditionEntity FromInterface(IValidationCondition validationCondition)
        {
            return(new ValidationConditionEntity
            {
                ActiveFlag = true,
                ApplicationID = validationCondition.ApplicationID,
                Expression = validationCondition.Expression,

                DateCreatedOffset = DateTimeOffset.UtcNow,
                DateModifiedOffset = DateTimeOffset.UtcNow
            });
        }
コード例 #13
0
        private bool EvaluateValidationCondition <T>(T target, IValidationCondition validationCondition)
        {
            if (!_validationConditionResultDictionary.ContainsKey(validationCondition.ValidationConditionID))
            {
                ParameterExpression parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name);
                LambdaExpression    expression          = DynamicExpression.ParseLambda(new[] { parameterExpression }, typeof(bool), validationCondition.Expression);

                bool outcome = (bool)expression.Compile().DynamicInvoke(target);

                _validationConditionResultDictionary.Add(validationCondition.ValidationConditionID, outcome);
            }

            return(_validationConditionResultDictionary[validationCondition.ValidationConditionID]);
        }
コード例 #14
0
        private bool EvaluateNode <T>(ExpressionTreeNode node, T target, IEnumerable <IValidationCondition> validationConditions)
        {
            if (!node.Token.IsOperatorToken())
            {
                IIdentifier identifier = (IIdentifier)node.Token;

                IValidationCondition validationCondition = validationConditions.FirstOrDefault(r => r.ValidationConditionID == identifier.ID);

                return(EvaluateValidationCondition(target, validationCondition));
            }
            else
            {
                IOperator op = (IOperator)node.Token;

                return(op.Evaluate(() => EvaluateNode(node.Left, target, validationConditions),
                                   () => EvaluateNode(node.Right, target, validationConditions)));
            }
        }
コード例 #15
0
        /// <summary>
        ///   Checks value is type of <see cref="TType"/>
        /// </summary>
        public static IValidationCondition <object> IsType <TType>(
            this IValidationCondition <object> condition,
            Action <IValidationCondition <TType> >?typed = null)
        {
            if (condition.Value is TType)
            {
                var typedCondition = new ValidationCondition <TType>(
                    condition.ValidationContext,
                    condition.ValidationKey,
                    new Lazy <TType>(() => (TType)condition.Value));

                typed?.Invoke(typedCondition);

                condition.IsValid = typedCondition.IsValid ?? false;
            }
            else
            {
                condition.IsValid = true;
            }

            return(condition);
        }
コード例 #16
0
 /// <summary>
 ///   Adds <see cref="ValidationDetail" /> to <see cref="IValidationContext" /> with fatal severity
 /// </summary>
 /// <exception cref="ValidationConditionException">
 ///   Throws when ValidationDetail.ValidationSeverity greater ValidationContext.ValidationSeverity.
 ///   Example: Add fatal detail, when validation context severity is error
 /// </exception>
 public static ValidationDetail?AddValidationFatal(
     this IValidationCondition condition,
     string validationMessage)
 {
     return(condition.AddValidationDetail(validationMessage, ValidationSeverity.Fatal));
 }
コード例 #17
0
 /// <summary>
 ///   Checks value is false
 /// </summary>
 public static IValidationCondition <bool> IsFalse(
     this IValidationCondition <bool> condition)
 {
     return(condition.IsNot(value => value));
 }
コード例 #18
0
        /// <summary>
        /// Adds a Condition to the rule. If the condition needs a Property to be validated, the property has to be added to the condition. This will not be done automaticaly.
        /// </summary>
        /// <param name="condition">The condition that will be validated</param>
        /// <returns>A ValidationRule</returns>
        public virtual IValidationRule <T> AddCondition(IValidationCondition <T> condition)
        {
            ValidationConditions.Add(condition);

            return(this);
        }
コード例 #19
0
 /// <summary>
 ///   Checks value is type of
 /// </summary>
 public static IValidationCondition <TValue> IsType <TValue>(
     this IValidationCondition <TValue> condition,
     Type type)
 {
     return(condition.Is(value => value?.GetType() == type));
 }
コード例 #20
0
 /// <summary>
 ///   Checks value is not equal using Equals method
 /// </summary>
 public static IValidationCondition <TValue> IsNotEqual <TValue>(
     this IValidationCondition <TValue> condition,
     TValue expect)
 {
     return(condition.IsNot(value => value?.Equals(expect) ?? expect is null));
 }
コード例 #21
0
 /// <summary>
 ///   Checks value is not null
 /// </summary>
 public static IValidationCondition <TValue> IsNotNull <TValue>(this IValidationCondition <TValue> condition)
 {
     return(condition.IsNot(value => value is null));
 }
コード例 #22
0
 /// <summary>
 ///   Checks collection has count not
 /// </summary>
 public static IValidationCondition <ICollection <TElement> > HasCountNot <TElement>(
     this IValidationCondition <ICollection <TElement> > condition,
     int count)
 {
     return(condition.IsNot(value => value?.Count == count));
 }
コード例 #23
0
 /// <summary>
 ///   Checks values has a value
 /// </summary>
 public static IValidationCondition <TValue> IsNotIn <TValue>(
     this IValidationCondition <TValue> condition,
     IEnumerable <TValue> values)
 {
     return(condition.IsNot(value => values.Contains(condition.Value)));
 }
コード例 #24
0
 /// <summary>
 ///   Checks collection is not contains
 /// </summary>
 public static IValidationCondition <ICollection <TElement> > IsNotContains <TElement>(
     this IValidationCondition <ICollection <TElement> > condition,
     TElement element)
 {
     return(condition.IsNot(value => value?.Contains(element) ?? false));
 }
コード例 #25
0
 /// <summary>
 ///   Checks collection is empty
 /// </summary>
 public static IValidationCondition <ICollection <TElement> > IsEmpty <TElement>(
     this IValidationCondition <ICollection <TElement> > condition)
 {
     return(condition.Is(value => value is null || value.Count == 0));
 }
コード例 #26
0
 /// <summary>
 ///   Checks if value is not valid
 /// </summary>
 public static IValidationCondition <TValue> IsNot <TValue>(
     this IValidationCondition <TValue> condition,
     Func <TValue, bool> predicate)
 {
     return(condition.IsNot(() => predicate(condition.Value)));
 }
コード例 #27
0
 /// <summary>
 ///   Checks if value is not default
 /// </summary>
 public static IValidationCondition <TValue> IsNotDefault <TValue>(
     this IValidationCondition <TValue> condition)
 {
     return(condition.IsNot(value => EqualityComparer <TValue> .Default.Equals(value, default)));
 }
コード例 #28
0
 public abstract IValidationCondition UpdateValidationCondition(IValidationCondition validationCondition);
コード例 #29
0
 /// <summary>
 ///   Checks values has a value
 /// </summary>
 public static IValidationCondition <TValue> IsNotIn <TValue>(
     this IValidationCondition <TValue> condition,
     params TValue[] values)
 {
     return(condition.IsNot(value => values.Contains(condition.Value)));
 }
コード例 #30
0
 /// <summary>
 ///   Checks value is true
 /// </summary>
 public static IValidationCondition <bool> IsTrue(
     this IValidationCondition <bool> condition)
 {
     return(condition.Is(value => value));
 }