public static RuleExpressionInfo Validate(RuleValidation validation, CodeExpression expression, bool isWritten)
        {
            if (validation == null)
            {
                throw new ArgumentNullException("validation");
            }

            // See if we've visited this node before.
            // Always check if written = true
            RuleExpressionInfo resultExprInfo = null;

            if (!isWritten)
            {
                resultExprInfo = validation.ExpressionInfo(expression);
            }
            if (resultExprInfo == null)
            {
                // First time we've seen this node.
                RuleExpressionInternal ruleExpr = GetExpression(expression);
                if (ruleExpr == null)
                {
                    string          message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, expression.GetType().FullName);
                    ValidationError error   = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled);
                    error.UserData[RuleUserDataKeys.ErrorObject] = expression;

                    if (validation.Errors == null)
                    {
                        string typeName = string.Empty;
                        if ((validation.ThisType != null) && (validation.ThisType.Name != null))
                        {
                            typeName = validation.ThisType.Name;
                        }

                        string exceptionMessage = string.Format(
                            CultureInfo.CurrentCulture, Messages.ErrorsCollectionMissing, typeName);

                        throw new InvalidOperationException(exceptionMessage);
                    }
                    else
                    {
                        validation.Errors.Add(error);
                    }

                    return(null);
                }

                resultExprInfo = validation.ValidateSubexpression(expression, ruleExpr, isWritten);
            }

            return(resultExprInfo);
        }
        private void ValidateExpression(RuleValidation validation, CodeExpression expression, string propertyName)
        {
            ValidationError error;

            if (expression == null)
            {
                error = new ValidationError(propertyName + " cannot be null", 123);
                validation.Errors.Add(error);
            }
            else
            {
                RuleExpressionInfo result = ValidateExpression(validation, expression, false);
                if ((result == null) || (result.ExpressionType != typeof(bool)))
                {
                    error = new ValidationError(propertyName + " must return boolean result", 123);
                    validation.Errors.Add(error);
                }
            }
        }
示例#3
0
        private void EvaluateRule(Rule rule, ref MSRuleSetEvaluationResult ruleSetEvaluationResult, ref RuleValidation rv)
        {
            rv.Errors.Clear();

            IRuleExpression ruleExpression = (IRuleExpression)((RuleExpressionCondition)rule.Condition).Expression;

            RuleExecution      re   = new RuleExecution(rv, _instanceOfObject);
            RuleExpressionInfo info = RuleExpressionWalker.Validate(rv, (CodeExpression)ruleExpression, true);

            if (rv.Errors.Count > 0)
            {
                //string validationErrorMessages = Helper.GetErrorMessageFromValidationErrorsCollection(errors);

                ruleSetEvaluationResult.AddValidationError(rule.Name, rule.Description, rv.Errors);
            }
            else
            {
                RuleExpressionResult result = RuleExpressionWalker.Evaluate(re, (CodeExpression)ruleExpression);
                ruleSetEvaluationResult.AddEvaluationResult(rule.Name, rule.Description, result);
            }
        }
示例#4
0
        public override bool Validate(RuleValidation validator)
        {
            ValidationError error;

            if (_message == null)
            {
                error = new ValidationError("Message cannot be null", 123);
                validator.Errors.Add(error);
                return(false);
            }
            else
            {
                RuleExpressionInfo result = RuleExpressionWalker.Validate(validator, _message, false);
                if ((result == null) || (result.ExpressionType != typeof(string)))
                {
                    error = new ValidationError("Message must return string result", 123);
                    validator.Errors.Add(error);
                    return(false);
                }
            }
            return(validator.Errors.Count == 0);
        }
示例#5
0
        /// <summary>
        /// we could also do here alot of custom things say if we wanted to try to resolve the problem by providing a default
        /// or whatever we have many options here...
        /// </summary>
        /// <param name="validation"></param>
        /// <param name="expression"></param>
        /// <param name="propertyName"></param>
        private void ValidateExpression(RuleValidation validation, CodeExpression expression, string propertyName)
        {
            ValidationError error;

            if (expression == null)
            {
                error = new ValidationError(propertyName + " cannot be null", 123);
                validation.Errors.Add(error);
            }
            // here would could make sure that the value is not null if we want
            //or perhaps we could verify that the result is a particular type, we have
            //lots of options here to do whatever granular custom validation that we want.
            else
            {
                //do not remove this line very important, will not execute w/o it
                RuleExpressionInfo result = ValidateExpression(validation, expression, false);

                //if (result == null)
                //{
                //    error = new ValidationError(propertyName + " cannot be null value ", 123);
                //    validation.Errors.Add(error);
                //}
            }
        }
        internal override bool Validate(RuleValidation validation)
        {
            bool success = false;

            if (exprStatement.Expression == null)
            {
                ValidationError error = new ValidationError(Messages.NullInvokeStatementExpression, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = exprStatement;
                validation.Errors.Add(error);
            }
            else if (exprStatement.Expression is CodeMethodInvokeExpression)
            {
                RuleExpressionInfo exprInfo = RuleExpressionWalker.Validate(validation, exprStatement.Expression, false);
                success = (exprInfo != null);
            }
            else
            {
                ValidationError error = new ValidationError(Messages.InvokeNotHandled, ErrorNumbers.Error_CodeExpressionNotHandled);
                error.UserData[RuleUserDataKeys.ErrorObject] = exprStatement;
                validation.Errors.Add(error);
            }

            return(success);
        }
        public override bool Validate(RuleValidation validator)
        {
            ValidationError error;

            if (_applicationID == null || _typeID == null || _ruleID == null || _ruleName == null || _result == null || _createDate == null || _referenceID == null)
            {
                error = new ValidationError("Rule Stat Parameters cannot be null", 123);
                validator.Errors.Add(error);
                return(false);
            }
            else
            {
                RuleExpressionInfo applicationIDResult = RuleExpressionWalker.Validate(validator, _applicationID, false);
                RuleExpressionInfo typeIDResult        = RuleExpressionWalker.Validate(validator, _typeID, false);
                RuleExpressionInfo ruleIDResult        = RuleExpressionWalker.Validate(validator, _ruleID, false);
                RuleExpressionInfo ruleNameResult      = RuleExpressionWalker.Validate(validator, _ruleName, false);
                RuleExpressionInfo resultResult        = RuleExpressionWalker.Validate(validator, _result, false);
                RuleExpressionInfo createDateResult    = RuleExpressionWalker.Validate(validator, _createDate, false);
                RuleExpressionInfo referenceIDResult   = RuleExpressionWalker.Validate(validator, _referenceID, false);

                if ((applicationIDResult == null) || (applicationIDResult.ExpressionType != typeof(Guid)))
                {
                    error = new ValidationError("application id must be Guid", 123);
                    validator.Errors.Add(error);
                    return(false);
                }

                if ((typeIDResult == null) || (typeIDResult.ExpressionType != typeof(Guid)))
                {
                    error = new ValidationError("type id must be Guid", 123);
                    validator.Errors.Add(error);
                    return(false);
                }


                if ((ruleIDResult == null) || (ruleIDResult.ExpressionType != typeof(Guid)))
                {
                    error = new ValidationError("rule id must be a guid", 123);
                    validator.Errors.Add(error);
                    return(false);
                }

                if ((ruleNameResult == null) || (ruleNameResult.ExpressionType != typeof(string)))
                {
                    error = new ValidationError("rule name must be a string", 123);
                    validator.Errors.Add(error);
                    return(false);
                }

                if ((resultResult == null) || (resultResult.ExpressionType != typeof(bool)))
                {
                    error = new ValidationError("result must be a true or false", 123);
                    validator.Errors.Add(error);
                    return(false);
                }

                if ((createDateResult == null) || (createDateResult.ExpressionType != typeof(DateTime)))
                {
                    error = new ValidationError("create date must be a DateTime", 123);
                    return(false);
                }

                if ((referenceIDResult == null) || (referenceIDResult.ExpressionType != typeof(Guid)))
                {
                    error = new ValidationError("reference id must be Guid", 123);
                    validator.Errors.Add(error);
                    return(false);
                }
            }
            return(validator.Errors.Count == 0);
        }
示例#8
0
        public RuleExpressionInfo ValidateExpression(RuleValidation validation, CodeExpression expression, bool isWritten)
        {
            RuleExpressionInfo result = RuleExpressionWalker.Validate(validation, expression, false);

            return(result);
        }
        internal override bool Validate(RuleValidation validation)
        {
            bool               success = false;
            string             message;
            RuleExpressionInfo lhsExprInfo = null;

            if (assignStatement.Left == null)
            {
                ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                validation.Errors.Add(error);
            }
            else
            {
                lhsExprInfo = validation.ExpressionInfo(assignStatement.Left);
                if (lhsExprInfo == null)
                {
                    lhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Left, true);
                }
            }

            RuleExpressionInfo rhsExprInfo = null;

            if (assignStatement.Right == null)
            {
                ValidationError error = new ValidationError(Messages.NullAssignRight, ErrorNumbers.Error_RightOperandMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                validation.Errors.Add(error);
            }
            else
            {
                rhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Right, false);
            }

            if (lhsExprInfo != null && rhsExprInfo != null)
            {
                Type expressionType = rhsExprInfo.ExpressionType;
                Type assignmentType = lhsExprInfo.ExpressionType;

                if (assignmentType == typeof(NullLiteral))
                {
                    // Can't assign to a null literal.
                    ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandInvalidType);
                    error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                    validation.Errors.Add(error);
                    success = false;
                }
                else if (assignmentType == expressionType)
                {
                    // Easy case, they're both the same type.
                    success = true;
                }
                else
                {
                    // The types aren't the same, but it still might be a legal assignment.
                    if (!RuleValidation.TypesAreAssignable(expressionType, assignmentType, assignStatement.Right, out ValidationError error))
                    {
                        if (error == null)
                        {
                            message = string.Format(CultureInfo.CurrentCulture, Messages.AssignNotAllowed, RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(assignmentType));
                            error   = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible);
                        }
                        error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                        validation.Errors.Add(error);
                    }
                    else
                    {
                        success = true;
                    }
                }
            }

            return(success);
        }