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); }
private EndpointsRoot LoadFromFile() { EndpointsRoot root; try { var file = File.ReadAllText(_settings.Mock.ConfigurationPath); root = JsonSerializer.Deserialize <EndpointsRoot>(file); } catch (Exception e) when(e is ArgumentException || e is ArgumentNullException || e is PathTooLongException || e is DirectoryNotFoundException || e is IOException || e is UnauthorizedAccessException || e is FileNotFoundException || e is NotSupportedException || e is SecurityException) { return(new EndpointsRoot($"Error loading configuration file: {e.Message}")); } catch (JsonException e) { return(new EndpointsRoot($"Error reading configuration file: {e.Message}")); } catch (Exception e) { var errorMessage = $"An unexpected error occurred attemping to read the configuration file."; _logger.LogError(e, errorMessage); return(new EndpointsRoot(errorMessage)); } var validationResult = _validator.Validate(root, new SectionName("$")); if (validationResult.HasErrors) { var errorMessage = new StringBuilder(); errorMessage.AppendLine("Configuration file was read correctly but failed validation. Errors:"); foreach (var validationError in validationResult.Errors) { errorMessage.AppendLine($" - {validationError}"); } // Remove the final new line character errorMessage.Length--; return(new EndpointsRoot(errorMessage.ToString())); } return(root); }
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); }