Skip to content

Commit

Permalink
Switch to NJsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jun 19, 2019
1 parent 2153070 commit e0d3519
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cmdline/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="1.9.71" targetFramework="net45" />
<package id="log4net" version="2.0.8" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net45" />
</packages>
4 changes: 1 addition & 3 deletions Netkan/CKAN-netkan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\_build\lib\nuget\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json.Schema">
<HintPath>..\_build\lib\nuget\Newtonsoft.Json.Schema.3.0.11\lib\net45\Newtonsoft.Json.Schema.dll</HintPath>
</Reference>
<PackageReference Include="NJsonSchema" Version="10.0.19" />
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions Netkan/Validators/ObeysCKANSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Schema;
using NJsonSchema;
using CKAN.NetKAN.Model;

namespace CKAN.NetKAN.Validators
Expand All @@ -14,22 +14,23 @@ static ObeysCKANSchemaValidator()
var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedSchema);
using (var reader = new StreamReader(resourceStream))
{
schema = JSchema.Parse(reader.ReadToEnd());
schema = JsonSchema.FromJsonAsync(reader.ReadToEnd()).Result;
}
}

public void Validate(Metadata metadata)
{
var json = metadata.Json();
IList<string> messages;
if (!json.IsValid(schema, out messages))
var errors = schema.Validate(metadata.Json());
if (errors.Any())
{
string msg = messages.Aggregate((a, b) => $"{a}\r\n{b}");
string msg = errors
.Select(err => $"{err.Path}: {err.Kind}")
.Aggregate((a, b) => $"{a}\r\n{b}");
throw new Kraken($"Schema validation failed: {msg}");
}
}

private static readonly JSchema schema;
private static readonly JsonSchema schema;
private const string embeddedSchema = "CKAN.NetKAN.CKAN.schema";
}
}
1 change: 0 additions & 1 deletion Netkan/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
<package id="ICSharpCode.SharpZipLib.Patched" version="0.86.5.1" targetFramework="net45" />
<package id="log4net" version="2.0.8" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json.Schema" version="3.0.11" targetFramework="net45" />
</packages>

0 comments on commit e0d3519

Please sign in to comment.