0

I have been using the Hl7.org tool org.hl7.fhir.validator.jar file to validate my messages but I would like to add this function it to my .Net project. Once I parse the message is there a class I can call to validate the Structure.

Is there a validate FHIR class in fhir-net-api that will display the same results has org.hl7.fhir.validator.jar?

    string HL7FilePath = string.Format("{0}\\{1}", System.IO.Directory.GetCurrentDirectory(), "Sample.xml");
    string HL7FileData = File.ReadAllText(HL7FilePath)

    var b = new FhirXmlParser().Parse<PlanDefinition>(HL7FileData);


FHIR Validator Build ??
Arguments: C:\HL7Tools\validator\REC78_1.xml -version 3.0
  .. connect to tx server @ http://tx.fhir.org
  .. definitions from hl7.fhir.core#3.0.1
    (v3.0.1-null)
  .. validate [C:\HL7Tools\validator\Sample.xml]
Terminology server: Check for supported code systems for http://www.nlm.nih.gov/research/umls/rxnorm
Success.
Jeremy
  • 44,950
  • 68
  • 206
  • 332

1 Answers1

1

Yes, there is. You need to add the Hl7.Fhir.Specification.STU3 package, and can then use the validation methods like this:

using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Validation;

... your code, reading the PlanDefinition from file and parsing it ...

// setup the resolver to use specification.zip, and a folder with custom profiles
var source = new CachedResolver(new MultiResolver(
                                   new DirectorySource(@"<path_to_profile_folder>"),
                                   ZipSource.CreateValidationSource()));

// prepare the settings for the validator
var ctx = new ValidationSettings()
          {
              ResourceResolver = source,
              GenerateSnapshot = true,
              Trace = false,
              EnableXsdValidation = true,
              ResolveExteralReferences = false
          }

var validator = new Validator(ctx);

// validate the resource; optionally enter a custom profile url as 2nd parameter
var result = validator.Validate(b);

The result will be an OperationOutcome resource containing the details of the validation.

Mirjam Baltus
  • 2,035
  • 9
  • 13
  • this is the message I get Overall result: FAILURE (2 errors and 0 warnings) [ERROR] Unable to resolve reference to profile 'http://hl7.org/fhir/StructureDefinition/PlanDefinition' (at PlanDefinition) [ERROR] Unable to resolve reference to profile 'http://nccn.org/fhir/StructureDefinition/order-template' (at PlanDefinition) –  Nov 26 '18 at 16:46
  • I download the whole specification from https://www.hl7.org/fhir/downloads.html so I am not sure why I would be getting this error. –  Nov 26 '18 at 17:35
  • When you add the Hl7.Fhir.Specification.STU3 package, that should automatically add a specification.zip file to your project. This is what is being read when you use the ZipSource.CreateValidationSource. There is no need to download the specs your self, but if you have, you can point the DirectorySource to that folder. The NCCN profiles are not in specification.zip, so you will need to download them and point a DirectorySource to that folder. Please also check the url for the profile, since I have this for the NCCN order-template: http://ots.nccn.org/fhir/StructureDefinition/OrderTemplate. – Mirjam Baltus Nov 26 '18 at 22:21
  • I tried to use my ig but I am still getting the same error message. var source = new CachedResolver(new MultiResolver( new DirectorySource(string.Format("{0}\\{1}", "C:\\Project\\IGPublisher\\Publish", "ig.json")), ZipSource.CreateValidationSource())); –  Nov 27 '18 at 17:32
  • Could you try to just add your folder instead of a file in the DirectorySource, or take out the DirectorySource to see if the specification.zip is at least found? You can contact me at the address found on http://docs.simplifier.net/fhirnetapi/contact.html for more troubleshooting, if you want. – Mirjam Baltus Nov 27 '18 at 18:41