Example #1
0
        public void Validate_SuccessfullyAccountsForEmptyNames()
        {
            var config = new ItemNameValidationConfig()
            {
                ValidateEmptyNames = true,
            };

            var validationService = new ItemNameService(config, null);
            var result            = validationService.Validate(string.Empty);

            Assert.False(result.IsValid);
            Assert.Contains(result.Errors, e => e.ErrorType == ValidationErrorType.EmptyName);
            Assert.Contains(result.Errors, e => e.ValidatorName == "EmptyNameValidator");
        }
Example #2
0
#pragma warning disable RECS0154 // Parameter is never used - but used by method which takes an action which is passed a template
        public static string GetRandomName(TemplateInfo template)
#pragma warning restore RECS0154 // Parameter is never used
        {
            for (int i = 0; i < 10; i++)
            {
                var itemNameValidationService = new ItemNameService(GenContext.ToolBox.Repo.ItemNameValidationConfig, () => new string[] { });
                var randomName = Path.GetRandomFileName().Replace(".", string.Empty);
                if (itemNameValidationService.Validate(randomName).IsValid)
                {
                    return(randomName);
                }
            }

            throw new ApplicationException("No valid randomName could be generated");
        }
Example #3
0
        public void Validate_SuccessfullyAccountsForDefaultNames(string language)
        {
            SetUpFixtureForTesting(language);

            var config = new ItemNameValidationConfig()
            {
                ValidateDefaultNames = true,
            };

            var validationService = new ItemNameService(config, null);
            var result            = validationService.Validate("LiveTile");

            Assert.False(result.IsValid);
            Assert.Contains(result.Errors, e => e.ErrorType == ValidationErrorType.ReservedName);
            Assert.Contains(result.Errors, e => e.ValidatorName == "DefaultNamesValidator");
        }
Example #4
0
        public void Validate_SuccessfullyAccountsForRegex()
        {
            var config = new ItemNameValidationConfig()
            {
                Regexs = new RegExConfig[]
                {
                    new RegExConfig()
                    {
                        Name    = "itemEndsWithPage",
                        Pattern = ".*(?<!page)$",
                    },
                },
            };

            var validationService = new ItemNameService(config, null);
            var result            = validationService.Validate("Testpage");

            Assert.False(result.IsValid);
            Assert.Contains(result.Errors, e => e.ErrorType == ValidationErrorType.Regex);
            Assert.Contains(result.Errors, e => e.ValidatorName == "itemEndsWithPage");
        }
Example #5
0
 public static ValidationResult ValidateTemplateName(string templateName)
 {
     return(_itemValidationService.Validate(templateName));
 }
Example #6
0
        internal static void ValidateUserSelection(UserSelection userSelection, bool isNewProject)
        {
            if (isNewProject)
            {
                var rootDir = Directory.GetParent(Directory.GetParent(GenContext.Current.DestinationPath).FullName).FullName;

                var projectNameService = new ProjectNameService(GenContext.ToolBox.Repo.ProjectNameValidationConfig, () => Fs.GetExistingFolderNames(rootDir));

                var result = projectNameService.Validate(GenContext.Current.ProjectName);
                {
                    if (!result.IsValid)
                    {
                        var errors = string.Join(Environment.NewLine, result.Errors.Select(e => $"Error: {e.ErrorType}; Validator: {e.ValidatorName}."));
                        throw new InvalidDataException(string.Format(StringRes.ErrorProjectNameValidationFailed, GenContext.Current.ProjectName, Environment.NewLine + errors));
                    }
                }
            }

            foreach (var item in userSelection.Items)
            {
                var template = GenContext.ToolBox.Repo.Find(t => t.Identity == item.TemplateId);

                if (template.GetItemNameEditable())
                {
                    Func <IEnumerable <string> > existingNames = () => userSelection.Items.Where(t => t.TemplateId != template.Identity).Select(n => n.Name);

                    var itemNameService = new ItemNameService(GenContext.ToolBox.Repo.ItemNameValidationConfig, existingNames);

                    var validationResult = itemNameService.Validate(item.Name);
                    {
                        if (!validationResult.IsValid)
                        {
                            var errors = string.Join(Environment.NewLine, validationResult.Errors.Select(e => $"Error: {e.ErrorType}; Validator: {e.ValidatorName}."));
                            throw new InvalidDataException(string.Format(StringRes.ErrorNamingValidationFailed, item.Name, Environment.NewLine + errors));
                        }
                    }
                }

                var dependencies = GenContext.ToolBox.Repo.GetDependencies(template, userSelection.Context, new List <ITemplateInfo>());

                foreach (var dependency in dependencies)
                {
                    if (!userSelection.Items.Any(i => i.TemplateId == dependency.Identity))
                    {
                        throw new InvalidDataException(string.Format(StringRes.ErrorDependencyMissing, dependency));
                    }
                }

                var requirements = GenContext.ToolBox.Repo.GetRequirements(template, userSelection.Context);

                if (requirements.Count() > 0 && !userSelection.Items.Any(i => requirements.Select(r => r.Identity).Contains(i.TemplateId)))
                {
                    throw new InvalidDataException(string.Format(StringRes.ErrorRequirementMissing, requirements.Select(r => r.Name).Aggregate((i, j) => $"{i},{j}")));
                }

                var exclusionTemplates = GenContext.ToolBox.Repo.GetExclusions(template, userSelection.Context);

                foreach (var exclusion in exclusionTemplates)
                {
                    if (userSelection.Items.Any(i => i.TemplateId == exclusion.Identity))
                    {
                        throw new InvalidDataException(string.Format(StringRes.ErrorExcludedTemplatesFound, exclusion.Name));
                    }
                }
            }
        }