public ValidationResult Validate(EndpointAction section, SectionName sectionName) { var result = new ValidationResult(); if (section is null) { result.AddError(sectionName, "Action is null"); return(result); } if (section.Method is object) { if (!MethodPattern.IsMatch(section.Method)) { result.AddError(sectionName.WithProperty("method"), $"Invalid method '{section.Method}'. Method can only contain A-Z, a-z."); } } if (section.Mode is object) { // Mode isn't required, but if it's set it needs a valid value if (Array.IndexOf(AllowedModes, section.Mode.ToUpperInvariant()) < 0) { // IndexOf returns -1 if there is no match found. Using this to // determine if the mode is in the list of allowed modes. result.AddError(sectionName.WithProperty("mode"), $"Invalid mode '{section.Mode}'. Mode must be one of {string.Join(", ", AllowedModes)}."); } } if (section.Responses is null) { result.AddError(sectionName.WithProperty("responses"), "Action must have a responses array"); } else if (section.Responses.Count < 1) { result.AddError(sectionName.WithProperty("responses"), "Responses array must have at least one item"); } else { for (int i = 0; i < section.Responses.Count; i++) { var action = section.Responses[i]; var actionValidationResult = _responseValidator.Validate(action, sectionName.WithProperty("responses", i)); result.Append(actionValidationResult); } } return(result); }
public ValidationResult Validate(Endpoint section, SectionName sectionName) { var result = new ValidationResult(); if (section is null) { result.AddError(sectionName, "Endpoint is null"); return(result); } if (string.IsNullOrWhiteSpace(section.Path)) { result.AddError(sectionName.WithProperty("path"), "Endpoint must have a path"); } else if (!PathPattern.IsMatch(section.Path)) { result.AddError(sectionName.WithProperty("path"), $"Invalid path '{section.Path}'. Path can only contain A-Z, a-z, 0-9 and slashes (/)."); } if (section.Actions is null) { result.AddError(sectionName.WithProperty("actions"), "Endpoints must have an actions array"); } else if (section.Actions.Count < 1) { result.AddError(sectionName.WithProperty("actions"), "Actions array must have at least one item"); } else { for (int i = 0; i < section.Actions.Count; i++) { var action = section.Actions[i]; var actionValidationResult = _actionValidator.Validate(action, sectionName.WithProperty("actions", i)); result.Append(actionValidationResult); } } if (section.Endpoints is object) { for (int i = 0; i < section.Endpoints.Count; i++) { var endpoint = section.Endpoints[i]; var actionValidationResult = Validate(endpoint, sectionName.WithProperty("endpoints", i)); result.Append(actionValidationResult); } } return(result); }
public ValidationResult Validate(EndpointsRoot section, SectionName sectionName) { var result = new ValidationResult(); if (section is null) { result.AddError(sectionName, "Endpoints file is null"); } else if (section.Endpoints is null) { result.AddError(sectionName.WithProperty("endpoints"), "Endpoints array is null"); } else { for (int i = 0; i < section.Endpoints.Count; i++) { var endpoint = section.Endpoints[i]; var endpointValidationResult = _endpointValidator.Validate(endpoint, sectionName.WithProperty("endpoints", i)); result.Append(endpointValidationResult); } } return(result); }
public ValidationResult Validate(Response section, SectionName sectionName) { var result = new ValidationResult(); if (section is null) { result.AddError(sectionName, "Response is null"); return(result); } if (section.Headers is object) { foreach (var(key, value) in section.Headers) { if (!AsciiPattern.IsMatch(key)) { result.AddError(sectionName.WithProperty("headers", key), $"Invalid header key '{key}'. Headers can only contain ASCII characters."); } if (!AsciiPattern.IsMatch(value)) { result.AddError(sectionName.WithProperty("headers", key), $"Invalid header value '{value}'. Headers can only contain ASCII characters."); } } } // Ensure that only one of jsonBody or stringBody is set var setBodyTypes = new List <string>(); if (section.JsonBody is object) { setBodyTypes.Add("jsonBody"); } if (section.XmlBody is object) { setBodyTypes.Add("xmlBody"); } if (section.StringBody is object) { setBodyTypes.Add("stringBody"); } if (setBodyTypes.Count > 1) { string formattedBodyList; if (setBodyTypes.Count > 2) { // Create a formatted list of the set bodies int lastIndex = setBodyTypes.Count - 1; var lastItem = setBodyTypes[lastIndex]; setBodyTypes.RemoveAt(lastIndex); formattedBodyList = $"{string.Join(", ", setBodyTypes)}, and {lastItem}"; } else { // If it's just two items, don't comma separate formattedBodyList = $"{setBodyTypes[0]} and {setBodyTypes[1]}"; } result.AddError(sectionName, $"Only one type of body can be set per response. {formattedBodyList} are set."); } return(result); }