-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationMiddleware.cs
More file actions
136 lines (118 loc) · 4.08 KB
/
Copy pathValidationMiddleware.cs
File metadata and controls
136 lines (118 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#nullable enable
using EfMigrationDiff.CLI;
namespace EfMigrationDiff.Middleware;
/// <summary>
/// Middleware that validates command context before execution.
/// Checks for required options, valid argument counts, and application configuration state.
/// Can short-circuit command execution if validation fails.
/// </summary>
public class ValidationMiddleware : ICommandMiddleware
{
private readonly Dictionary<string, CommandValidator> _validatorsByCommand = new();
public ValidationMiddleware()
{
}
/// <summary>
/// Registers a validator for a specific command.
/// </summary>
public ValidationMiddleware RegisterValidator(string commandName, CommandValidator validator)
{
_validatorsByCommand[commandName.ToLowerInvariant()] = validator;
return this;
}
/// <summary>
/// Validates the command context before execution.
/// Returns error result if validation fails, otherwise continues execution.
/// </summary>
public async Task<MiddlewareResult> InvokeAsync(CommandContext context)
{
var commandName = context.CommandName.ToLowerInvariant();
// Get validator for this command if registered
if (_validatorsByCommand.TryGetValue(commandName, out var validator))
{
var validationResult = validator.Validate(context);
if (!validationResult.IsValid)
{
var errorMessage = string.Join(Environment.NewLine, validationResult.Errors);
context.WriteError(errorMessage);
return MiddlewareResult.ShortCircuit(CommandResult.Error(
$"Validation failed: {validationResult.Errors.First()}",
1));
}
}
return MiddlewareResult.Continue();
}
}
/// <summary>
/// Validates command context for a specific command.
/// </summary>
public class CommandValidator
{
private readonly List<Func<CommandContext, string?>> _rules = new();
/// <summary>
/// Adds a validation rule. Rule function should return error message if invalid, null if valid.
/// </summary>
public CommandValidator AddRule(Func<CommandContext, string?> rule)
{
_rules.Add(rule);
return this;
}
/// <summary>
/// Requires a minimum number of positional arguments.
/// </summary>
public CommandValidator RequireMinArguments(int count)
{
_rules.Add(ctx => ctx.ParsedArguments.Count >= count
? null
: $"Command requires at least {count} argument(s), got {ctx.ParsedArguments.Count}");
return this;
}
/// <summary>
/// Requires a specific option to be present.
/// </summary>
public CommandValidator RequireOption(string optionName)
{
_rules.Add(ctx => ctx.HasOption(optionName)
? null
: $"Required option '{optionName}' is missing");
return this;
}
/// <summary>
/// Validates that an option has a non-empty value.
/// </summary>
public CommandValidator ValidateOptionValue(string optionName, string errorMessage = "")
{
_rules.Add(ctx =>
{
var value = ctx.GetOption(optionName);
return !string.IsNullOrWhiteSpace(value)
? null
: string.IsNullOrEmpty(errorMessage) ? $"Option '{optionName}' has an invalid value" : errorMessage;
});
return this;
}
/// <summary>
/// Runs all registered validation rules and returns aggregated result.
/// </summary>
public ValidationResult Validate(CommandContext context)
{
var errors = new List<string>();
foreach (var rule in _rules)
{
var error = rule(context);
if (!string.IsNullOrEmpty(error))
{
errors.Add(error);
}
}
return new ValidationResult { IsValid = errors.Count == 0, Errors = errors };
}
}
/// <summary>
/// Result of command validation.
/// </summary>
public class ValidationResult
{
public bool IsValid { get; set; }
public List<string> Errors { get; set; } = new();
}