mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
cleanup after merge
This commit is contained in:
@@ -19,7 +19,7 @@ namespace CliFx.Domain
|
||||
|
||||
public Type? ConverterType { get; }
|
||||
|
||||
private readonly Type[]? _validators;
|
||||
public readonly Type[]? ValidatorTypes;
|
||||
|
||||
protected CommandArgumentSchema(PropertyInfo? property, string? description, Type? converterType = null, Type[]? validators = null)
|
||||
{
|
||||
@@ -27,7 +27,7 @@ namespace CliFx.Domain
|
||||
Description = description;
|
||||
ConverterType = converterType;
|
||||
|
||||
_validators = validators;
|
||||
ValidatorTypes = validators;
|
||||
}
|
||||
|
||||
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||
@@ -140,7 +140,7 @@ namespace CliFx.Domain
|
||||
{
|
||||
var value = Convert(values);
|
||||
|
||||
if (_validators.NotEmpty())
|
||||
if (ValidatorTypes.NotEmpty())
|
||||
Validate(value);
|
||||
|
||||
Property?.SetValue(command, value);
|
||||
@@ -171,9 +171,9 @@ namespace CliFx.Domain
|
||||
return;
|
||||
|
||||
var failed = new List<ValidationResult>();
|
||||
foreach (var validator in _validators!)
|
||||
foreach (var validator in ValidatorTypes!)
|
||||
{
|
||||
var result = validator.InstanceOf<IArgumentValueValidator>().Validate(value!);
|
||||
var result = validator.CreateInstance<IArgumentValueValidator>().Validate(value!);
|
||||
if (result.IsValid)
|
||||
continue;
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -26,8 +25,8 @@ namespace CliFx.Domain
|
||||
bool isRequired,
|
||||
string? description,
|
||||
Type? converterType = null,
|
||||
Type[]? validators = null)
|
||||
: base(property, description, converterType, validators)
|
||||
Type[]? validatorTypes = null)
|
||||
: base(property, description, converterType, validatorTypes)
|
||||
{
|
||||
Name = name;
|
||||
ShortName = shortName;
|
||||
@@ -110,9 +109,9 @@ namespace CliFx.Domain
|
||||
internal partial class CommandOptionSchema
|
||||
{
|
||||
public static CommandOptionSchema HelpOption { get; } =
|
||||
new CommandOptionSchema(null, "help", 'h', null, false, "Shows help text.", null);
|
||||
new CommandOptionSchema(null, "help", 'h', null, false, "Shows help text.", converterType: null, validatorTypes: null);
|
||||
|
||||
public static CommandOptionSchema VersionOption { get; } =
|
||||
new CommandOptionSchema(null, "version", null, null, false, "Shows version information.", null);
|
||||
new CommandOptionSchema(null, "version", null, null, false, "Shows version information.", converterType: null, validatorTypes: null);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
@@ -19,8 +18,8 @@ namespace CliFx.Domain
|
||||
string name,
|
||||
string? description,
|
||||
Type? converterType = null,
|
||||
Type[]? validators = null)
|
||||
: base(property, description, converterType, validators)
|
||||
Type[]? validatorTypes = null)
|
||||
: base(property, description, converterType, validatorTypes)
|
||||
{
|
||||
Order = order;
|
||||
Name = name;
|
||||
|
||||
@@ -126,6 +126,18 @@ namespace CliFx.Domain
|
||||
invalidConverterParameters
|
||||
);
|
||||
}
|
||||
|
||||
var invalidValidatorParameters = command.Parameters
|
||||
.Where(p => p.ValidatorTypes != null && !p.ValidatorTypes.All(x => x.Implements(typeof(IArgumentValueValidator))))
|
||||
.ToArray();
|
||||
|
||||
if (invalidValidatorParameters.Any())
|
||||
{
|
||||
throw CliFxException.ParametersWithInvalidValidators(
|
||||
command,
|
||||
invalidValidatorParameters
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateOptions(CommandSchema command)
|
||||
@@ -208,6 +220,18 @@ namespace CliFx.Domain
|
||||
invalidConverterOptions
|
||||
);
|
||||
}
|
||||
|
||||
var invalidValidatorOptions = command.Options
|
||||
.Where(o => o.ValidatorTypes != null && !o.ValidatorTypes.All(x => x.Implements(typeof(IArgumentValueValidator))))
|
||||
.ToArray();
|
||||
|
||||
if (invalidValidatorOptions.Any())
|
||||
{
|
||||
throw CliFxException.OptionsWithInvalidValidators(
|
||||
command,
|
||||
invalidValidatorOptions
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||
|
||||
@@ -185,6 +185,19 @@ Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException ParametersWithInvalidValidators(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameter(s) with invalid value validators:
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Specified validator(s) must inherit from {typeof(ArgumentValueValidator<>).FullName}.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithNoName(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
@@ -269,6 +282,19 @@ Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithInvalidValidators(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid validators:
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Specified validators must inherit from {typeof(IArgumentValueValidator).FullName}.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
}
|
||||
|
||||
// End-user-facing exceptions
|
||||
|
||||
Reference in New Issue
Block a user