예제 #1
0
        internal static void ValidateExternalCss(ExternalCss externalCss, RulesException errors, int index, string[] duplicateUrls)
        {
            if (string.IsNullOrWhiteSpace(externalCss.Url))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.ExternalCssUrlRequired, index));
                externalCss.Invalid = true;
            }
            else
            {
                if (duplicateUrls.Contains(externalCss.Url))
                {
                    errors.ErrorForModel(string.Format(VisualEditorStrings.ExternalCssUrlDuplicate, index));
                    externalCss.Invalid = true;
                }

                if (!UrlHelpers.IsValidWebFolderUrl(externalCss.Url))
                {
                    errors.ErrorForModel(string.Format(VisualEditorStrings.ExternalCssUrlNotValid, index));
                    externalCss.Invalid = true;
                }

                if (externalCss.Url.Length > 255)
                {
                    errors.ErrorForModel(string.Format(VisualEditorStrings.ExternalCssUrlMaxLengthExceeded, index));
                    externalCss.Invalid = true;
                }
            }
        }
예제 #2
0
        public void rules_base()
        {
            var foo = new RulesException <Foo>();

            foo.ErrorForModel("Test1");
            foo.ErrorForModel("Test2");

            Assert.Equal(2, foo.ErrorMessages.Count);
        }
예제 #3
0
        private void ValidateVeCommand(VisualEditorCommand command, RulesException errors, int index, IEnumerable <string> dupNames, IEnumerable <string> dupAliases)
        {
            if (!string.IsNullOrWhiteSpace(command.Name) && dupNames.Contains(command.Name))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandNameDuplicate, index));
                command.IsInvalid = true;
            }

            if (!string.IsNullOrWhiteSpace(command.Alias) && dupAliases.Contains(command.Alias))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandAliasDuplicate, index));
                command.IsInvalid = true;
            }

            if (string.IsNullOrWhiteSpace(command.Name))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandNameRequired, index));
                command.IsInvalid = true;
            }

            if (!string.IsNullOrWhiteSpace(command.Name) && Regex.IsMatch(command.Name, RegularExpressions.InvalidEntityName))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandNameInvalidFormat, index));
                command.IsInvalid = true;
            }

            if (string.IsNullOrWhiteSpace(command.Alias))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandAliasRequired, index));
                command.IsInvalid = true;
            }

            if (command.Name.Length > 255)
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandNameMaxLengthExceeded, index));
                command.IsInvalid = true;
            }

            if (command.Alias.Length > 255)
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandAliasMaxLengthExceeded, index));
                command.IsInvalid = true;
            }

            if (!VisualEditorRepository.IsCommandNameFree(command.Name, Id))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandNameNonUnique, index));
                command.IsInvalid = true;
            }

            if (!VisualEditorRepository.IsCommandAliasFree(command.Alias, Id))
            {
                errors.ErrorForModel(string.Format(VisualEditorStrings.CommandAliasNonUnique, index));
                command.IsInvalid = true;
            }
        }
예제 #4
0
        public void Validate(RulesException <Db> errors, int index, string[] dupNames)
        {
            var messages = new List <string>();

            if (string.IsNullOrWhiteSpace(Key))
            {
                messages.Add(string.Format(DBStrings.KeyRequired, index));
            }

            if (dupNames.Contains(Key))
            {
                messages.Add(string.Format(DBStrings.KeyNotUnique, index));
            }

            if (string.IsNullOrWhiteSpace(Value))
            {
                messages.Add(string.Format(DBStrings.ValueRequired, index));
            }

            if (Key.Length > 255)
            {
                messages.Add(string.Format(DBStrings.KeyMaxLengthExceeded, index));
            }

            if (Value.Length > 255)
            {
                messages.Add(string.Format(DBStrings.ValueMaxLengthExceeded, index));
            }

            Invalid = messages.Any();
            foreach (var message in messages)
            {
                errors.ErrorForModel(message);
            }
        }
예제 #5
0
        public void RulesExceptionUseCaseModelFoo()
        {
            var modelFoo = new ModelFoo
            {
                ModelFooId = 1,
                Name       = "Test",
                ModelBar   = new ModelBar
                {
                    ModelBarId = 1,
                    Name       = "Test"
                }
            };

            var rulesException = new RulesException <ModelFoo>();

            // This will apply to the model as whole and should be used for
            // scenarios where there are multiple issues against another object.
            rulesException.ErrorForModel("There is already a ModelFoo with this Id and Name");
            Assert.Single(rulesException.Errors);

            // Should be used for property issues.
            rulesException.ErrorFor(x => x.Name, "The Name is not Unique");
            rulesException.ErrorFor(x => x.ModelBar.Name, "The Name is not Unique");

            rulesException.ErrorFor("Name", "Another Error");

            var errorMessage = rulesException.ToString();

            Assert.Equal(4, rulesException.Errors.Count());
        }
예제 #6
0
        public override void Validate()
        {
            var errors = new RulesException <VisualEditorPlugin>();

            base.Validate(errors);

            if (!string.IsNullOrEmpty(Url) && !UrlHelpers.IsValidWebFolderUrl(Url))
            {
                errors.ErrorFor(n => Url, VisualEditorStrings.UrlPrefixInvalidFormat);
            }

            var duplicateCurrentNames   = VeCommands.GroupBy(c => c.Name).Where(g => g.Count() > 1).Select(x => x.Key).ToArray();
            var duplicateCurrentAliases = VeCommands.GroupBy(c => c.Alias).Where(g => g.Count() > 1).Select(x => x.Key).ToArray();

            if (VeCommands.Count == 0)
            {
                errors.ErrorForModel(VisualEditorStrings.CommandsRequired);
            }
            else
            {
                var veCommandsArray = VeCommands.ToArray();
                for (var i = 0; i < veCommandsArray.Length; i++)
                {
                    ValidateVeCommand(veCommandsArray[i], errors, i + 1, duplicateCurrentNames, duplicateCurrentAliases);
                }
            }

            if (!errors.IsEmpty)
            {
                throw errors;
            }
        }
예제 #7
0
        private static void EnsureValidForCreation(Appointment appt)
        {
            var errors = new RulesException <Appointment>();

            if (string.IsNullOrEmpty(appt.ClientName))
            {
                errors.ErrorFor(x => x.ClientName, "Please specify a name");
            }

            if (appt.AppointmentDate < DateTime.Now.Date)
            {
                errors.ErrorFor(x => x.AppointmentDate, "Can't book in the past");
            }
            else if ((appt.AppointmentDate - DateTime.Now.Date).TotalDays > 7)
            {
                errors.ErrorFor(x => x.AppointmentDate, "Can't book more than a week in advance");
            }

            if (appt.ClientName == "Steve" && appt.AppointmentDate.DayOfWeek == DayOfWeek.Saturday)
            {
                errors.ErrorForModel("Steve can't book on weekends");
            }

            if (errors.Errors.Any())
            {
                throw errors;
            }
        }