-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): validate concerto objects from dotnet using node (#29)
* feat(validate): add validation project Signed-off-by: Jamie Shorten <[email protected]> * feat(validation): call node to validate Signed-off-by: Jamie Shorten <[email protected]> * fix(ci): run webpack during build Signed-off-by: Jamie Shorten <[email protected]> * fix(tests): Add unit test for invalid model Signed-off-by: Jamie Shorten <[email protected]> * fix(build): use dotnet 6.0 Signed-off-by: Jamie Shorten <[email protected]> * fix(build): run npm ci Signed-off-by: Jamie Shorten <[email protected]> * fix(ci): fix github actions Signed-off-by: Jamie Shorten <[email protected]> * fix(ci): fix npm package resolution Signed-off-by: Jamie Shorten <[email protected]> * fix(ci): use c# 10.0 Signed-off-by: Jamie Shorten <[email protected]> * chore(ci): add node to publish action Signed-off-by: Jamie Shorten <[email protected]> * fix(tests): ensure sequential test runs Signed-off-by: Jamie Shorten <[email protected]> * chore(git): update gitignore Signed-off-by: Jamie Shorten <[email protected]> * chore: remove dist files Signed-off-by: Jamie Shorten <[email protected]> * chore(tests): serialise test execution Signed-off-by: Jamie Shorten <[email protected]> --------- Signed-off-by: Jamie Shorten <[email protected]>
- Loading branch information
1 parent
aefea57
commit d134f9b
Showing
30 changed files
with
2,342 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
AccordProject.Concerto.Validate.Tests/AccordProject.Concerto.Validate.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.ClearScript.Complete" Version="7.4.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AccordProject.Concerto.Validate\AccordProject.Concerto.Validate.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Remove="Fixtures\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="Fixtures\testModelAST.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Fixtures\testInstanceInvalid.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Fixtures\testInstanceValid.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Fixtures\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="GlobalUsings.cs"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Compile> | ||
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="UnitTest1.cs"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Compile> | ||
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="ValidationTests.cs"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Compile> | ||
</ItemGroup> | ||
</Project> |
4 changes: 4 additions & 0 deletions
4
AccordProject.Concerto.Validate.Tests/Fixtures/testInstanceInvalid.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$class": "[email protected]", | ||
"value": 1 | ||
} |
4 changes: 4 additions & 0 deletions
4
AccordProject.Concerto.Validate.Tests/Fixtures/testInstanceValid.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$class": "[email protected]", | ||
"value": "Hello, World!" | ||
} |
58 changes: 58 additions & 0 deletions
58
AccordProject.Concerto.Validate.Tests/Fixtures/testModelAST.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"$class": "[email protected]", | ||
"decorators": [], | ||
"namespace": "[email protected]", | ||
"imports": [], | ||
"declarations": [ | ||
{ | ||
"$class": "[email protected]", | ||
"name": "Basic", | ||
"isAbstract": false, | ||
"properties": [ | ||
{ | ||
"$class": "[email protected]", | ||
"name": "value", | ||
"isArray": false, | ||
"isOptional": false | ||
} | ||
] | ||
}, | ||
{ | ||
"$class": "[email protected]", | ||
"name": "Thing", | ||
"isAbstract": false, | ||
"properties": [ | ||
{ | ||
"$class": "[email protected]", | ||
"name": "id", | ||
"isArray": false, | ||
"isOptional": false | ||
}, | ||
{ | ||
"$class": "[email protected]", | ||
"name": "value", | ||
"isArray": false, | ||
"isOptional": false | ||
} | ||
], | ||
"identified": { | ||
"$class": "[email protected]", | ||
"name": "id" | ||
} | ||
}, | ||
{ | ||
"$class": "[email protected]", | ||
"name": "Defaults", | ||
"isAbstract": false, | ||
"properties": [ | ||
{ | ||
"$class": "[email protected]", | ||
"name": "value", | ||
"isArray": false, | ||
"isOptional": false, | ||
"defaultValue": "Hello, World!" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.ClearScript.V8; | ||
using AccordProject.Concerto.Validate; | ||
using Jering.Javascript.NodeJS; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace AccordProject.Concerto.Validate.Tests; | ||
|
||
public class ValidationTests | ||
{ | ||
|
||
[Fact] | ||
public async void GivenValidModelWhenValidateCalledThenIsValidShouldBeTrue() | ||
{ | ||
|
||
var model = File.ReadAllText("Fixtures/testModelAST.json"); | ||
var instance = File.ReadAllText("Fixtures/testInstanceValid.json"); | ||
var services = new ServiceCollection(); | ||
services.AddNodeJS(); | ||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
INodeJSService nodeJSService = serviceProvider.GetRequiredService<INodeJSService>(); | ||
|
||
|
||
var validator = new Validator(nodeJSService); | ||
String[] models = { model }; | ||
var result =await validator.Validate(models, instance); | ||
Assert.True(result.IsValid); | ||
} | ||
|
||
[Fact] | ||
public async void GivenInvalidModelWhenValidateCalledThenIsValidShouldBeFalseAndMessageShouldBePresent() | ||
{ | ||
|
||
var model = File.ReadAllText("Fixtures/testModelAST.json"); | ||
var instance = File.ReadAllText("Fixtures/testInstanceInvalid.json"); | ||
var services = new ServiceCollection(); | ||
services.AddNodeJS(); | ||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
INodeJSService nodeJSService = serviceProvider.GetRequiredService<INodeJSService>(); | ||
|
||
|
||
var validator = new Validator(nodeJSService); | ||
String[] models = { model }; | ||
var result = await validator.Validate(models, instance); | ||
Assert.False(result.IsValid); | ||
Assert.NotEmpty(result.ErrorMessage); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
AccordProject.Concerto.Validate/AccordProject.Concerto.Validate.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<LangVersion>10.0</LangVersion> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<LangVersion>10.0</LangVersion> | ||
</PropertyGroup> | ||
<Target Name="WebPack" BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild"> | ||
<Exec Command="npm run build" WorkingDirectory="./concerto-validate" /> | ||
</Target> | ||
<ItemGroup> | ||
<None Update="concerto-core.js"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="concerto-validate\dist\validate.js"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Jering.Javascript.NodeJS" Version="7.0.0" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Jering.Javascript.NodeJS; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace AccordProject.Concerto.Validate | ||
{ | ||
public class Validator | ||
{ | ||
private INodeJSService nodeJSService { get; set; } | ||
private string script { get; set; } | ||
public Validator(INodeJSService nodeJSService) { | ||
this.nodeJSService = nodeJSService; | ||
this.script = File.ReadAllText("concerto-validate/dist/validate.js"); | ||
} | ||
|
||
public async Task<ValidationResult> Validate(string[] modelJson, string instanceJson) | ||
{ | ||
var models = "[" + string.Join(",\n", modelJson) + "]"; | ||
var args = new[] { models, instanceJson }; | ||
var cacheIdentifier = "validate"; | ||
return await nodeJSService.InvokeFromStringAsync<ValidationResult>(script, cacheIdentifier, "validateInstance", args); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
registry = "https://registry.npmjs.com/" |
12 changes: 12 additions & 0 deletions
12
AccordProject.Concerto.Validate/concerto-validate/ValidationResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
namespace AccordProject.Concerto.Validate | ||
{ | ||
public class ValidationResult | ||
{ | ||
public string Instance { get; set; } | ||
public string Id { get; set; } | ||
public Boolean IsValid { get; set; } | ||
public string ErrorMessage{ get; set; } | ||
} | ||
} | ||
|
Oops, something went wrong.