FHIR Resource Validation

214 views
Skip to first unread message

Lakshmi Bhamidipati

unread,
Mar 9, 2020, 4:25:41 PM3/9/20
to FHIR DOTNET
Hello,
I posted my question in this site earlier - http://community.fhir.org/t/validation-of-a-fhir-resource-using-net-api/1848/2

I would like to make sure that I can validate a FHIR resource against US-Core profiles.

Where I am stumbling is how to download all the US core profiles into a local directory on my machine so that I can set the path in the CachedResolver

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

// setup the resolver to use specification.zip, and a folder with US Core 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 poco = (new FhirJsonParser()).Parse<Resource>(txtJson);
result = validator.Validate(poco);

Also, do I need to specify a 2nd parameter? Say I need to validate my json using US core AllergyIntolerance profile.
Thank you!

Brian Postlethwaite

unread,
Mar 9, 2020, 8:12:40 PM3/9/20
to FHIR DOTNET
If you don't provide the 2nd parameter that defines which profile to validate against, it will only validate against the core, unless the resource you're validating has declared that it conforms to the us core allergy intollerance.

Lakshmi Bhamidipati

unread,
Mar 9, 2020, 10:47:19 PM3/9/20
to FHIR DOTNET
Thank you Brian.
The code below worked for me - However, I don't fully understand why I need to download the IG to my local directory and then specify the hl7.fhir uri to the structure definition. The structure definition is present locally on my machine under the ig/site folder.  
               var source = new CachedResolver(new MultiResolver(
                    new DirectorySource(@"C:\Users\xxx\Downloads\full-ig\site"),
                    ZipSource.CreateValidationSource()));

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

                var validator = new Validator(ctx);

                // validate the resource; optionally enter a custom profile url as 2nd parameter
                var poco = (new FhirJsonParser()).Parse<Resource>(txtInstanceXml.Text);
                result = validator.Validate(poco, @"http://hl7.org/fhir/us/core/StructureDefinition/us-core-allergyintolerance");
Reply all
Reply to author
Forward
0 new messages