Skip to content

Commit 317a2e5

Browse files
Move validation logic from OptionsLoaded handlers to centralized option validators
Co-authored-by: waldekmastykarz <[email protected]>
1 parent 5582a14 commit 317a2e5

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

DevProxy.Plugins/Behavior/GenericRandomErrorPlugin.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,32 @@ public override Option[] GetOptions()
7575
HelpName = "failure rate"
7676
};
7777

78+
// Add validation for rate option
79+
_rateOption.Validators.Add(result =>
80+
{
81+
var rate = result.GetValueOrDefault<int?>();
82+
if (rate is not null && (rate < 0 || rate > 100))
83+
{
84+
result.AddError($"Rate must be between 0 and 100. Received: {rate}");
85+
}
86+
});
87+
7888
return [_rateOption];
7989
}
8090

81-
public override void OptionsLoaded(OptionsLoadedArgs e)
82-
{
83-
ArgumentNullException.ThrowIfNull(e);
84-
85-
base.OptionsLoaded(e);
86-
91+
public override void OptionsLoaded(OptionsLoadedArgs e)
92+
{
93+
ArgumentNullException.ThrowIfNull(e);
94+
95+
base.OptionsLoaded(e);
96+
8797
var parseResult = e.ParseResult;
8898

8999
var rate = parseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
90100
if (rate is not null)
91101
{
92-
if (rate < 0 || rate > 100)
93-
{
94-
throw new ArgumentOutOfRangeException($"Rate must be between 0 and 100. Received: {rate}");
95-
}
96-
Configuration.Rate = rate.Value;
97-
}
102+
Configuration.Rate = rate.Value;
103+
}
98104
}
99105

100106
public override Task BeforeRequestAsync(ProxyRequestArgs e)

DevProxy.Plugins/Behavior/GraphRandomErrorPlugin.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ public override Option[] GetOptions()
117117
Description = "The percentage of chance that a request will fail",
118118
HelpName = "failure rate"
119119
};
120+
121+
// Add validation for rate option
122+
_rateOption.Validators.Add(result =>
123+
{
124+
var rate = result.GetValueOrDefault<int?>();
125+
if (rate is not null && (rate < 0 || rate > 100))
126+
{
127+
result.AddError($"Rate must be between 0 and 100. Received: {rate}");
128+
}
129+
});
120130

121131
return [_allowedErrors, _rateOption];
122132
}
@@ -147,12 +157,8 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
147157
var rate = parseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
148158
if (rate is not null)
149159
{
150-
if (rate < 0 || rate > 100)
151-
{
152-
throw new ArgumentOutOfRangeException($"Rate must be between 0 and 100. Received: {rate}");
153-
}
154160
Configuration.Rate = rate.Value;
155-
}
161+
}
156162
}
157163

158164
public override Task BeforeRequestAsync(ProxyRequestArgs e)

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,24 @@ private void ConfigureCommand()
324324
HelpName = "env",
325325
AllowMultipleArgumentsPerToken = true,
326326
Arity = ArgumentArity.ZeroOrMore
327-
};
327+
};
328+
329+
// Add validation for env option format
330+
_envOption.Validators.Add(result =>
331+
{
332+
var env = result.GetValueOrDefault<string[]?>();
333+
if (env is not null)
334+
{
335+
foreach (var e in env)
336+
{
337+
var parts = e.Split('=', 2);
338+
if (parts.Length != 2)
339+
{
340+
result.AddError($"Invalid env format: {e}. Expected format is 'key=value'.");
341+
}
342+
}
343+
}
344+
});
328345

329346
var options = new List<Option>
330347
{
@@ -489,13 +506,11 @@ private void ParseOptions(ParseResult parseResult)
489506
var env = parseResult.GetValueForOption<string[]?>(EnvOptionName, Options);
490507
if (env is not null)
491508
{
509+
// Validation is now handled by the option validator
492510
_proxyConfiguration.Env = env.Select(static e =>
493511
{
494-
// Split on first '=' only
495512
var parts = e.Split('=', 2);
496-
return parts.Length != 2
497-
? throw new ArgumentException($"Invalid env format: {e}. Expected format is 'key=value'.")
498-
: new KeyValuePair<string, string>(parts[0], parts[1]);
513+
return new KeyValuePair<string, string>(parts[0], parts[1]);
499514
}).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
500515
}
501516
}

0 commit comments

Comments
 (0)