private ValidationResult ValidateProperty(object target, ConfigurationPrimitivePropertyModel propertyModel)
        {
            var errors        = new List <string>();
            var propertyValue = propertyModel.GetPropertyValue(target);

            if (propertyValue == null)
            {
                return(propertyModel.ValidationRules.IsRequired
                    ? new ValidationResult(ValidationStrings.RequiredPropertyNotFound(propertyModel.ConfigurationPropertyName))
                    : ValidationResult.CreateValid());
            }
            if (propertyModel.ValidationRules.Min != null && propertyModel.ValidationRules.Min.CompareTo(propertyValue) > 0)
            {
                errors.Add(ValidationStrings.LessThanMin(propertyModel.ConfigurationPropertyName, propertyModel.ValidationRules.Min));
            }
            if (propertyModel.ValidationRules.Max != null && propertyModel.ValidationRules.Max.CompareTo(propertyValue) < 0)
            {
                errors.Add(ValidationStrings.GreaterThanMax(propertyModel.ConfigurationPropertyName, propertyModel.ValidationRules.Max));
            }
            if (propertyValue is string stringProperty)
            {
                ValidateStringProperty(stringProperty, propertyModel, errors);
            }
            return(new ValidationResult(errors));
        }
 private void ValidateStringProperty(string target, ConfigurationPrimitivePropertyModel propertyModel, List <string> errors)
 {
     if (propertyModel.ValidationRules.MaxLength.HasValue && target.Length > propertyModel.ValidationRules.MaxLength)
     {
         errors.Add(ValidationStrings.GreaterThanMaxLength(propertyModel.ConfigurationPropertyName, propertyModel.ValidationRules.MaxLength));
     }
     if (!string.IsNullOrWhiteSpace(propertyModel.ValidationRules.Pattern) && !Regex.IsMatch(target, propertyModel.ValidationRules.Pattern))
     {
         errors.Add(ValidationStrings.MatchesPattern(propertyModel.ConfigurationPropertyName, propertyModel.ValidationRules.Pattern));
     }
 }
        public async Task <ValidationResult> Validate(object target, ConfigurationModel model, ConfigurationIdentity configIdentity)
        {
            var dependencies = await GetRequiredConfiguration(model, configIdentity);

            if (model is ConfigurationOptionModel optionModel)
            {
                return(ValidateOption(target, optionModel, configIdentity, dependencies));
            }

            if (target == null || target.GetType() != model.Type)
            {
                return(new ValidationResult(ValidationStrings.InvalidConfigType(model.Type)));
            }
            return(ValidateProperties(target, model.ConfigurationProperties, configIdentity, dependencies));
        }
        private ValidationResult ValidateProperty(object target, IMultipleOptionPropertyDefinition propertyModel, ConfigurationIdentity configIdentity, IEnumerable <ConfigurationSet> configurationSets)
        {
            var errors        = new List <string>();
            var propertyValue = propertyModel.GetPropertyValue(target) as IEnumerable;
            var options       = optionSetFactory.Build(propertyModel, configIdentity, configurationSets);
            var isOptionValue = propertyModel is ConfigurationPropertyWithMultipleOptionValuesModelDefinition;

            foreach (var value in propertyValue)
            {
                if ((isOptionValue && options.ContainsKey(value)) || (!isOptionValue && options.OptionKeyInSet(value)))
                {
                    continue;
                }
                errors.Add(ValidationStrings.OptionNotFound(propertyModel.ConfigurationPropertyName));
                break;
            }
            return(new ValidationResult(errors));
        }
        private ValidationResult ValidateProperty(object target, IOptionPropertyDefinition propertyModel, ConfigurationIdentity configIdentity, IEnumerable <ConfigurationSet> configurationSets)
        {
            var errors        = new List <string>();
            var propertyValue = propertyModel.GetPropertyValue(target);
            var options       = optionSetFactory.Build(propertyModel, configIdentity, configurationSets);

            if (propertyModel is ConfigurationPropertyWithOptionValueModelDefinition valueModel)
            {
                if (!options.ContainsKey(propertyValue))
                {
                    errors.Add(ValidationStrings.OptionNotFound(propertyModel.ConfigurationPropertyName));
                }
            }
            else if (!options.OptionKeyInSet(propertyValue))
            {
                errors.Add(ValidationStrings.OptionNotFound(propertyModel.ConfigurationPropertyName));
            }
            return(new ValidationResult(errors));
        }
        private ValidationResult ValidateProperty(object target, ConfigurationCollectionPropertyDefinition propertyModel, ConfigurationIdentity configIdentity, IEnumerable <ConfigurationSet> configurationSets)
        {
            var results          = new List <ValidationResult>();
            var propertyValue    = propertyModel.GetPropertyValue(target) as IEnumerable;
            var duplicateChecker = new HashSet <string>();

            foreach (var value in propertyValue)
            {
                if (propertyModel.HasUniqueKey)
                {
                    var key = propertyModel.GetKeyFromMember(value);
                    if (!duplicateChecker.Add(key))
                    {
                        results.Add(new ValidationResult(ValidationStrings.DuplicateKeys(propertyModel.ConfigurationPropertyName, key)));
                    }
                }
                results.Add(ValidateProperties(value, propertyModel.ConfigurationProperties, configIdentity, configurationSets));
            }
            return(new ValidationResult(results));
        }
        private ValidationResult ValidateOption(object target, ConfigurationOptionModel model, ConfigurationIdentity configIdentity, IEnumerable <ConfigurationSet> configurationSets)
        {
            var results = new List <ValidationResult>();
            var options = target as IEnumerable;

            if (target == null)
            {
                return(new ValidationResult(ValidationStrings.InvalidOptionType(model.Type)));
            }
            var duplicateChecker = new HashSet <string>();

            foreach (var option in options)
            {
                var key = model.GetKeyFromObject(option);
                if (!duplicateChecker.Add(key))
                {
                    results.Add(new ValidationResult(ValidationStrings.DuplicateOptionKeys(model.Name, key)));
                }

                results.Add(ValidateProperties(option, model.ConfigurationProperties, configIdentity, configurationSets));
            }
            return(new ValidationResult(results));
        }