-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (74 loc) · 2.89 KB
/
Program.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using FhirResourceValidation.Validation;
using FluentValidation;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Validation;
namespace FhirResourceValidation
{
public static class Program
{
public static void Main()
{
var inputHealthPlan = FileReader.ReadFhirInputFile("careplan.json");
var fjp = new FhirJsonParser();
var resource = fjp.Parse<CarePlan>(inputHealthPlan);
foreach (var item in resource.Goal)
{
if (resource.FindContainedResource(item) is not Goal goal) continue;
ValidateUsingFluentValidation(goal, new GoalValidator());
ValidateUsingFhirProfile(goal);
}
}
private static void ValidateUsingFhirProfile(Base goal)
{
Console.WriteLine("======================================");
Console.WriteLine("Validate Goals via FHIR Profile");
var rootDir = Directory.GetCurrentDirectory();
var profileDirectory = Path.Combine(rootDir, "Profiles");
// This using CreateValidationSource seems flaky on n0man's mac - manually maintaining specification.zip in Profiles
// var coreSource = new CachedResolver(ZipSource.CreateValidationSource());
var coreSource = new CachedResolver(
new ZipSource(Path.Combine(profileDirectory, "specification.zip"))
);
var cachedResolver = new CachedResolver(new DirectorySource(profileDirectory,
new DirectorySourceSettings { IncludeSubDirectories = true }));
var combinedSource = new MultiResolver(cachedResolver, coreSource);
var settings = new ValidationSettings
{
EnableXsdValidation = true,
GenerateSnapshot = true,
Trace = false,
ResourceResolver = combinedSource,
ResolveExternalReferences = true,
SkipConstraintValidation = false
};
var validator = new Validator(settings);
var outcome = validator.Validate(goal);
WriteOutcome(outcome);
}
private static void WriteOutcome(OperationOutcome outcome)
{
Console.WriteLine($"Validation of resource {(outcome.Success ? "is successful" : "has failed:")}");
if (!outcome.Success)
{
outcome.Issue.ForEach(i => Console.WriteLine($" {i}"));
}
}
private static void ValidateUsingFluentValidation<T>(T resource,
IValidator<T> validator)
{
Console.WriteLine("======================================");
Console.WriteLine("Validate Goals via C# FluentValidation");
var validationResult = validator.Validate(resource);
Console.WriteLine($"Validation of resource {(validationResult.IsValid ? "is successful" : "has failed:")}");
if (validationResult.IsValid) return;
foreach (var failure in validationResult.Errors)
{
Console.WriteLine($"Validation Issue: {failure.PropertyName}: {failure.ErrorMessage}");
}
}
}
}