public static void ValidatePoliciesRequest(PoliciesValidationModel policiesValidationModel)
        {
            if (policiesValidationModel.RateLimitPolicy != null)
            {
                if (string.IsNullOrEmpty(policiesValidationModel.RateLimitPolicy.Name))
                {
                    throw new ValidationException(ValidationRules.CannotBeNull, "RateLimitPolicy.Name");
                }

                if (policiesValidationModel.RateLimitPolicy.Calls < 1)
                {
                    throw new ValidationException(ValidationRules.InclusiveMinimum, "RateLimitPolicy.Calls");
                }

                if (policiesValidationModel.RateLimitPolicy.RenewalPeriod < 1)
                {
                    throw new ValidationException(ValidationRules.InclusiveMinimum, "RateLimitPolicy.RenewalPeriod");
                }

                if (policiesValidationModel.RateLimitPolicy.RenewalPeriod > 300)
                {
                    throw new ValidationException(ValidationRules.InclusiveMaximum, "RateLimitPolicy.RenewalPeriod");
                }
            }

            if (policiesValidationModel.CustomPolicies != null)
            {
                foreach (var customPolicy in policiesValidationModel.CustomPolicies)
                {
                    if (string.IsNullOrEmpty(customPolicy.Name))
                    {
                        throw new ValidationException(ValidationRules.CannotBeNull, "CustomPolicy.Name");
                    }

                    if (string.IsNullOrEmpty(customPolicy.Xml))
                    {
                        throw new ValidationException(ValidationRules.CannotBeNull, "CustomPolicy.Xml");
                    }

                    try
                    {
                        XElement.Parse(customPolicy.Xml);
                    }
                    catch (XmlException xe)
                    {
                        throw new ValidationException("XmlError", "CustomPolicy.Xml", xe.Message);
                    }
                }
            }
        }
예제 #2
0
 public ApiValidationModel(
     string name,
     string path,
     string version,
     Stream openApiSpec,
     IEnumerable <string> productNames,
     IEnumerable <ApiRevisionValidationModel> revisions,
     PoliciesValidationModel policies)
 {
     this.Name         = name;
     this.Path         = path;
     this.Version      = version;
     this.OpenApiSpec  = openApiSpec;
     this.ProductNames = productNames;
     this.Revisions    = revisions;
     this.Policies     = policies;
 }
        private static void AddPolicies(MultipartFormDataContent requestContent, string rootPrefix, PoliciesValidationModel policies)
        {
            if (policies.RateLimitPolicy != null)
            {
                requestContent.Add(new StringContent(policies.RateLimitPolicy.Name), $"{rootPrefix}.Policies.RateLimitPolicy.Name");
                requestContent.Add(new StringContent($"{policies.RateLimitPolicy.Calls}"), $"{rootPrefix}.Policies.RateLimitPolicy.Calls");
                requestContent.Add(new StringContent($"{policies.RateLimitPolicy.RenewalPeriod}"), $"{rootPrefix}.Policies.RateLimitPolicy.RenewalPeriod");
            }

            for (int cpIndex = 0; cpIndex < policies.CustomPolicies.Count(); cpIndex++)
            {
                var customPolicy = policies.CustomPolicies.ElementAt(cpIndex);
                requestContent.Add(new StringContent(customPolicy.Name), $"{rootPrefix}.Policies.CustomPolicies[{cpIndex}].Name");
                requestContent.Add(new StringContent(customPolicy.Xml), $"{rootPrefix}.Policies.CustomPolicies[{cpIndex}].Xml");
            }
        }