Skip to content

Commit f7d1767

Browse files
authored
Add abstract validations API + FluentValidations extension (#49)
* Add Resolve overload to take in any type * Add FluentValidation extension project * Add public abstract validation API * Fix tests * Update sample app with fluent validations * Add FluentValidations extension project * WIP * Fix error * Add validator implementation * Add Fluent Validations Extensions test project * Add Validator extension specific tests * Add correct Fluent Validation behaviour * Fix failed merge commit * Add cake support for FVE project. * Update nuspec * Update build script * Add extra tests for fluent validations * Add nuspec and update build. * Update relative path * Add docs
1 parent 28e4894 commit f7d1767

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1121
-97
lines changed

CommandLineParser.Tests/Command/SubCommandTests.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MatthiWare.CommandLine;
22
using MatthiWare.CommandLine.Abstractions;
33
using MatthiWare.CommandLine.Abstractions.Command;
4+
using MatthiWare.CommandLine.Core;
45
using MatthiWare.CommandLine.Core.Attributes;
56
using System;
67
using System.Linq;
@@ -41,7 +42,7 @@ public void TestSubCommandWorksCorrectlyInModel(bool autoExecute, string bla, in
4142
Assert.All(result.CommandResults.Select(r => r.Executed), Assert.True);
4243
}
4344

44-
private class CustomInstantiator : IContainerResolver
45+
private class CustomInstantiator : DefaultContainerResolver
4546
{
4647
private readonly ManualResetEventSlim lock1;
4748
private readonly ManualResetEventSlim lock2;
@@ -60,14 +61,16 @@ public CustomInstantiator(ManualResetEventSlim lock1, ManualResetEventSlim lock2
6061
this.n = n;
6162
}
6263

63-
public T Resolve<T>()
64+
public override T Resolve<T>() => (T)Resolve(typeof(T));
65+
66+
public override object Resolve(Type type)
6467
{
65-
if (typeof(T) == typeof(MainCommand))
66-
return (T)Activator.CreateInstance(typeof(T), lock1, autoExecute, bla, i, n);
67-
else if (typeof(T) == typeof(SubCommand))
68-
return (T)Activator.CreateInstance(typeof(T), lock2, autoExecute, bla, i, n);
68+
if (type == typeof(MainCommand))
69+
return Activator.CreateInstance(type, lock1, autoExecute, bla, i, n);
70+
else if (type == typeof(SubCommand))
71+
return Activator.CreateInstance(type, lock2, autoExecute, bla, i, n);
6972
else
70-
throw new InvalidCastException($"Unable to resolve {(typeof(T)).Name}");
73+
return base.Resolve(type);
7174
}
7275
}
7376

CommandLineParser.Tests/Parsing/ResolverFactoryTest.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using MatthiWare.CommandLine.Abstractions.Models;
44
using MatthiWare.CommandLine.Abstractions.Parsing;
5+
using MatthiWare.CommandLine.Core;
56
using MatthiWare.CommandLine.Core.Parsing;
67
using MatthiWare.CommandLine.Core.Parsing.Resolvers;
78

@@ -25,7 +26,7 @@ public enum Output
2526
[Fact]
2627
public void ContainsWork()
2728
{
28-
var factory = new DefaultArgumentResolverFactory();
29+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
2930

3031
Assert.True(factory.Contains<string>());
3132
Assert.True(factory.Contains<int>());
@@ -39,7 +40,7 @@ public void ContainsWork()
3940
[Fact]
4041
public void CreateEnumResolver()
4142
{
42-
var factory = new DefaultArgumentResolverFactory();
43+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
4344

4445
var output = factory.CreateResolver<Output>();
4546
var output2 = factory.CreateResolver(typeof(Output));
@@ -59,7 +60,7 @@ public void RegisterAndGet()
5960
mockResolver.Setup(_ => _.CanResolve(It.IsAny<ArgumentModel>())).Returns(true);
6061
mockResolver.Setup(_ => _.Resolve(It.IsAny<ArgumentModel>())).Returns(instance);
6162

62-
var factory = new DefaultArgumentResolverFactory();
63+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
6364

6465
factory.Register(mockResolver.Object);
6566

@@ -79,7 +80,7 @@ public void RegisterOverrideWorks()
7980
{
8081
var mockResolver = new Mock<ArgumentResolver<string>>();
8182

82-
var factory = new DefaultArgumentResolverFactory();
83+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
8384

8485
factory.Register(typeof(string), mockResolver.Object.GetType(), true);
8586
factory.Register<string, StringResolver>(true);
@@ -90,7 +91,7 @@ public void RegisterThrowsException()
9091
{
9192
var mockResolver = new Mock<ArgumentResolver<string>>();
9293

93-
var factory = new DefaultArgumentResolverFactory();
94+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
9495

9596
Assert.Throws<ArgumentException>(() => factory.Register<string, StringResolver>());
9697
}
@@ -105,7 +106,7 @@ public void RegisterObjectResolver()
105106
resolver.Setup(_ => _.CanResolve(It.IsAny<ArgumentModel>())).Returns(true);
106107
resolver.Setup(_ => _.Resolve(It.IsAny<ArgumentModel>())).Returns(obj);
107108

108-
var factory = new DefaultArgumentResolverFactory();
109+
var factory = new DefaultArgumentResolverFactory(new DefaultContainerResolver());
109110
var dummyArg = new ArgumentModel();
110111

111112
factory.Register(resolver.Object);

CommandLineParser.sln

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1818
README.md = README.md
1919
EndProjectSection
2020
EndProject
21+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions", "{9AE7A2BC-8475-4331-9C61-3B5045A1389A}"
22+
EndProject
23+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentValidationsExtensions", "Extensions\FluentValidationsExtensions\FluentValidationsExtensions.csproj", "{5B43A461-C8AC-44C0-94D5-DAEE102A5F41}"
24+
EndProject
25+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5046CC21-D258-4313-8866-E952ABB472E4}"
26+
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentValidationsExtensions.Tests", "Extensions\Tests\FluentValidationsExtensions.Tests\FluentValidationsExtensions.Tests.csproj", "{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061}"
28+
EndProject
2129
Global
2230
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2331
Debug|Any CPU = Debug|Any CPU
@@ -36,10 +44,23 @@ Global
3644
{6871A016-97E8-48C5-B797-DD0FA3DD6288}.Debug|Any CPU.Build.0 = Debug|Any CPU
3745
{6871A016-97E8-48C5-B797-DD0FA3DD6288}.Release|Any CPU.ActiveCfg = Release|Any CPU
3846
{6871A016-97E8-48C5-B797-DD0FA3DD6288}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{5B43A461-C8AC-44C0-94D5-DAEE102A5F41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{5B43A461-C8AC-44C0-94D5-DAEE102A5F41}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{5B43A461-C8AC-44C0-94D5-DAEE102A5F41}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{5B43A461-C8AC-44C0-94D5-DAEE102A5F41}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061}.Release|Any CPU.Build.0 = Release|Any CPU
3955
EndGlobalSection
4056
GlobalSection(SolutionProperties) = preSolution
4157
HideSolutionNode = FALSE
4258
EndGlobalSection
59+
GlobalSection(NestedProjects) = preSolution
60+
{5B43A461-C8AC-44C0-94D5-DAEE102A5F41} = {9AE7A2BC-8475-4331-9C61-3B5045A1389A}
61+
{5046CC21-D258-4313-8866-E952ABB472E4} = {9AE7A2BC-8475-4331-9C61-3B5045A1389A}
62+
{07BCB8B9-AFA2-485B-A01D-EB11FAFD5061} = {5046CC21-D258-4313-8866-E952ABB472E4}
63+
EndGlobalSection
4364
GlobalSection(ExtensibilityGlobals) = postSolution
4465
SolutionGuid = {CE28B97B-DDE7-41B6-B779-91914BDBCB80}
4566
EndGlobalSection
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
namespace MatthiWare.CommandLine.Abstractions
1+
using System;
2+
3+
namespace MatthiWare.CommandLine.Abstractions
24
{
35
public interface IContainerResolver
46
{
57
T Resolve<T>();
8+
object Resolve(Type type);
69
}
710
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace MatthiWare.CommandLine.Abstractions.Validations
4+
{
5+
public interface IValidationResult
6+
{
7+
bool IsValid { get; }
8+
Exception Error { get; }
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MatthiWare.CommandLine.Abstractions.Validations
6+
{
7+
public interface IValidator
8+
{
9+
IValidationResult Validate(object @object);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MatthiWare.CommandLine.Abstractions.Validations
6+
{
7+
public interface IValidator<T> : IValidator
8+
{
9+
IValidationResult Validate(T @object);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MatthiWare.CommandLine.Abstractions.Validations
6+
{
7+
public interface IValidatorsContainer
8+
{
9+
void AddValidator<TKey>(IValidator<TKey> validator);
10+
void AddValidator(Type key, IValidator validator);
11+
12+
void AddValidator<TKey, V>() where V : IValidator<TKey>;
13+
void AddValidator(Type key, Type validator);
14+
15+
bool HasValidatorFor<TKey>();
16+
bool HasValidatorFor(Type type);
17+
18+
IReadOnlyCollection<IValidator> GetValidators<TKey>();
19+
IReadOnlyCollection<IValidator> GetValidators(Type key);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace MatthiWare.CommandLine.Abstractions.Validations
2+
{
3+
public abstract class ValidationConfigurationBase
4+
{
5+
protected IValidatorsContainer Validators { get; private set; }
6+
7+
public ValidationConfigurationBase(IValidatorsContainer validators)
8+
{
9+
Validators = validators;
10+
}
11+
}
12+
}

CommandLineParser/CommandLineParser.csproj

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>MatthiWare.CommandLine</RootNamespace>
66
<PackageId>MatthiWare.CommandLineParser</PackageId>
7-
<Version>0.2.3</Version>
7+
<Version>0.2.4</Version>
88
<Authors>Matthias Beerens</Authors>
99
<Company>MatthiWare</Company>
1010
<Product>Command Line Parser</Product>
11-
<Description>Command Line Parser for .Net Core</Description>
11+
<Description>Command Line Parser for .NET Core written in .NET Standard</Description>
1212
<PackageProjectUrl>https://github.com/MatthiWare/CommandLineParser.Core</PackageProjectUrl>
13-
<PackageLicenseUrl>https://github.com/MatthiWare/CommandLineParser.Core/blob/master/LICENSE</PackageLicenseUrl>
13+
<PackageLicenseUrl></PackageLicenseUrl>
1414
<RepositoryUrl>https://github.com/MatthiWare/CommandLineParser.Core</RepositoryUrl>
15-
<PackageTags>Commandline parser</PackageTags>
15+
<PackageTags>Commandline parser commandline-parser cli</PackageTags>
1616
<LangVersion>7.3</LangVersion>
17-
<AssemblyVersion>0.2.3.0</AssemblyVersion>
18-
<FileVersion>0.2.3.0</FileVersion>
17+
<AssemblyVersion>0.2.4.0</AssemblyVersion>
18+
<FileVersion>0.2.4.0</FileVersion>
19+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
20+
<PackageReleaseNotes>Validation API added, general bugfixes and improvements</PackageReleaseNotes>
21+
<Copyright>Copyright Matthias Beerens 2018</Copyright>
1922
</PropertyGroup>
2023

2124
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -32,4 +35,11 @@
3235
<PackageReference Include="System.Memory" Version="4.5.3" />
3336
</ItemGroup>
3437

38+
<ItemGroup>
39+
<None Include="..\LICENSE">
40+
<Pack>True</Pack>
41+
<PackagePath></PackagePath>
42+
</None>
43+
</ItemGroup>
44+
3545
</Project>

CommandLineParser/CommandLineParser.nuspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>MatthiWare.CommandLineParser</id>
5-
<version>0.2.3</version>
5+
<version>0.2.4</version>
66
<title>CommandLineParser.Core</title>
77
<authors>Matthias Beerens</authors>
88
<owners>Matthiee</owners>
@@ -21,6 +21,7 @@
2121
<tags>commandline parser commandline-parser</tags>
2222
</metadata>
2323
<files>
24+
<file src="CommandLineParser.dll" target="lib\netstandard2.0" />
2425
<file src=".\CommandLineParser.xml" target="lib\netstandard2.0" />
2526
</files>
2627
</package>

CommandLineParser/CommandLineParser.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)