fhir-net-api (STU3) - Validating

1.1k views Asked by At

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.
1

There are 1 answers

5
Mirjam Baltus On

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.