-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
ObeysCKANSchemaValidator.cs
36 lines (33 loc) · 1.11 KB
/
ObeysCKANSchemaValidator.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
35
36
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using NJsonSchema;
using CKAN.NetKAN.Model;
namespace CKAN.NetKAN.Validators
{
internal sealed class ObeysCKANSchemaValidator : IValidator
{
static ObeysCKANSchemaValidator()
{
var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedSchema);
using (var reader = new StreamReader(resourceStream))
{
schema = JsonSchema.FromJsonAsync(reader.ReadToEnd()).Result;
}
}
public void Validate(Metadata metadata)
{
var errors = schema.Validate(metadata.Json());
if (errors.Any())
{
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 JsonSchema schema;
private const string embeddedSchema = "CKAN.NetKAN.CKAN.schema";
}
}