Exemple #1
0
        private bool CheckRules(bool checkForWarnings, IEnumerable <ValueDescriptor> descriptors)
        {
            Debug.Assert(Values != null);
            Debug.Assert(Issues != null);
            Debug.Assert(descriptors != null);

            // TODO: refactor this, it's not clear at all and there is too
            // much almost duplicated code. Separate class?
            int issueCount = 0;

            foreach (var descriptor in descriptors)
            {
                if (checkForWarnings)
                {
                    if (String.IsNullOrWhiteSpace(descriptor.WarningIfExpression))
                    {
                        continue;
                    }

                    bool hasWarning;
                    if (!_evaluator.Evaluate(() => descriptor.WarningIfExpression, out hasWarning))
                    {
                        continue;
                    }

                    if (!hasWarning)
                    {
                        continue;
                    }
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(descriptor.ValidIfExpression))
                    {
                        continue;
                    }

                    bool isValid;
                    if (!_evaluator.Evaluate(() => descriptor.ValidIfExpression, out isValid))
                    {
                        continue;
                    }

                    if (isValid)
                    {
                        continue;
                    }
                }

                DataSetValue item;
                object       itemValue = null;

                if (Values.TryGetValue(descriptor.Reference, out item))
                {
                    itemValue = item.Value;
                }

                // CHECK: message is not localized assuming that for every validation rule there will be
                // a clear ad-hoc description (and this is just for debugging purposes). If it will be not the case
                // then move this text to resources.
                string message = checkForWarnings ? descriptor.WarningMessage : descriptor.ValidationMessage;
                if (String.IsNullOrWhiteSpace(message))
                {
                    message = $"Value '{itemValue}' for '{descriptor.Reference}' is not valid.";
                }

                if (checkForWarnings)
                {
                    Issues.AddWarning(descriptor, message);
                }
                else
                {
                    Issues.AddValidationError(descriptor, message);
                }

                ++issueCount;
            }

            return(issueCount == 0);
        }