-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfValidatingArgumentsMiddleware.cs
34 lines (30 loc) · 1.11 KB
/
SelfValidatingArgumentsMiddleware.cs
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
using CommandDotNet;
using CommandDotNet.Execution;
using Jira2AzureDevOps.Logic.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Jira2AzureDevOps.Console.Framework
{
public static class SelfValidatingArgumentsMiddleware
{
public static AppRunner UseSelfValidatingArgumentModels(this AppRunner appRunner)
{
return appRunner.Configure(c => c.UseMiddleware(ValidateModels, MiddlewareStages.PostBindValuesPreInvoke));
}
private static Task<int> ValidateModels(CommandContext context, ExecutionDelegate next)
{
var paramValues = context.InvocationContexts.All.SelectMany(ic => ic.Invocation.ParameterValues);
var errors = paramValues
.OfType<ISelfValidatingArgumentModel>()
.SelectMany(m => m.GetValidationErrors())
.ToCsv(Environment.NewLine);
if (errors.IsNullOrWhiteSpace())
{
return next(context);
}
context.Console.Out.WriteLine(errors);
return Task.FromResult(2);
}
}
}