-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored validation logic into SettingsValidator class
This commit decomposes the validation logic previously located in the `GenerateCommand` class, and moves it into its own `SettingsValidator` class for better organization and reusability. This provides a cleaner architecture and easier future maintenance, as now settings validation can be altered independent from command generation logic.
- Loading branch information
1 parent
fdd710a
commit 084e88c
Showing
2 changed files
with
118 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using Refitter.Core; | ||
|
||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace Refitter; | ||
|
||
public class SettingsValidator | ||
{ | ||
public ValidationResult Validate(CommandContext context, Settings settings) | ||
{ | ||
if (BothSettingsFilesAreEmpty(settings) || BothSettingsFilesArePresent(settings)) | ||
{ | ||
return GetValidationErrorForSettingsFiles(); | ||
} | ||
|
||
return !string.IsNullOrWhiteSpace(settings.SettingsFilePath) | ||
? ValidateFilePath(settings) | ||
: ValidateOperationNameAndUrl(settings); | ||
} | ||
|
||
private static bool BothSettingsFilesAreEmpty(Settings settings) | ||
{ | ||
return string.IsNullOrWhiteSpace(settings.OpenApiPath) && | ||
string.IsNullOrWhiteSpace(settings.SettingsFilePath); | ||
} | ||
|
||
private static bool BothSettingsFilesArePresent(Settings settings) | ||
{ | ||
return !string.IsNullOrWhiteSpace(settings.OpenApiPath) && | ||
!string.IsNullOrWhiteSpace(settings.SettingsFilePath); | ||
} | ||
|
||
private static ValidationResult GetValidationErrorForSettingsFiles() | ||
{ | ||
return ValidationResult.Error( | ||
"You should either specify an input URL/file directly " + | ||
"or use specify it in 'openApiPath' from the settings file, " + | ||
"not both"); | ||
} | ||
|
||
private ValidationResult ValidateFilePath(Settings settings) | ||
{ | ||
var json = File.ReadAllText(settings.SettingsFilePath!); | ||
var refitGeneratorSettings = Serializer.Deserialize<RefitGeneratorSettings>(json); | ||
settings.OpenApiPath = refitGeneratorSettings.OpenApiPath; | ||
|
||
return ValidateFileAndOutputSettings(settings, refitGeneratorSettings); | ||
} | ||
|
||
private ValidationResult ValidateFileAndOutputSettings( | ||
Settings settings, | ||
RefitGeneratorSettings refitGeneratorSettings) | ||
{ | ||
if (string.IsNullOrWhiteSpace(refitGeneratorSettings.OpenApiPath)) | ||
{ | ||
return GetValidationErrorForOpenApiPath(); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(settings.OutputPath) && | ||
settings.OutputPath != Settings.DefaultOutputPath && | ||
(!string.IsNullOrWhiteSpace(refitGeneratorSettings.OutputFolder) || | ||
!string.IsNullOrWhiteSpace(refitGeneratorSettings.OutputFilename))) | ||
{ | ||
return GetValidationErrorForOutputPath(); | ||
} | ||
|
||
return ValidateOperationNameAndUrl(settings); | ||
} | ||
|
||
private static ValidationResult GetValidationErrorForOpenApiPath() | ||
{ | ||
return ValidationResult.Error( | ||
"The 'openApiPath' in settings file is required when " + | ||
"URL or file path to OpenAPI Specification file " + | ||
"is not specified in command line argument"); | ||
} | ||
|
||
private static ValidationResult GetValidationErrorForOutputPath() | ||
{ | ||
return ValidationResult.Error( | ||
"You should either specify an output path directly from --output " + | ||
"or use specify it in 'outputFolder' and 'outputFilename' from the settings file, " + | ||
"not both"); | ||
} | ||
|
||
private ValidationResult ValidateOperationNameAndUrl(Settings settings) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(settings.OperationNameTemplate) && | ||
!settings.OperationNameTemplate.Contains("{operationName}") && | ||
settings.MultipleInterfaces != MultipleInterfaces.ByEndpoint) | ||
{ | ||
return GetValidationErrorForOperationName(); | ||
} | ||
|
||
return IsUrl(settings.OpenApiPath!) ? ValidationResult.Success() : ValidateFileExistence(settings); | ||
} | ||
|
||
private static ValidationResult GetValidationErrorForOperationName() | ||
{ | ||
return ValidationResult.Error("'{operationName}' placeholder must be present in operation name template"); | ||
} | ||
|
||
private ValidationResult ValidateFileExistence(Settings settings) | ||
{ | ||
return File.Exists(settings.OpenApiPath) | ||
? ValidationResult.Success() | ||
: ValidationResult.Error($"File not found - {Path.GetFullPath(settings.OpenApiPath!)}"); | ||
} | ||
|
||
private static bool IsUrl(string openApiPath) | ||
{ | ||
return Uri.TryCreate(openApiPath, UriKind.Absolute, out var uriResult) && | ||
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); | ||
} | ||
} |