Stream: dotnet
Topic: Custom FHIR validations
Yassiel Oliva (Oct 22 2018 at 19:30):
I'm implementing an API in order to accept FHIR resources to create laboratory orders. I'm trying to perform custom validations and based in some business rules I would like to require a field or not. For example patient's address. In some cases I just want to require zip code but in some cases I want to require the entire address. It is possible to achieve this with the fhir .net API and FhirValidator or do I need to perform those validations by myself?
Michel Rutten (Oct 23 2018 at 07:51):
Hi @Yassiel Oliva, sure you can. I suggest you look into FHIRPath constraints, they allow you to define such business rules, and are supported by both .NET and Java API's.
Michel Rutten (Oct 23 2018 at 07:53):
FHIRPath constraints are supported by the standard validator implementations, probably no need to author any custom logic.
Ewout Kramer (Oct 23 2018 at 09:37):
Hi @Yassiel Oliva - I have created a demo project that shows how to run the validator in case you needed help there: https://github.com/ewoutkramer/Furore.Fhir.ValidationDemo
Yassiel Oliva (Oct 24 2018 at 15:55):
@Michel Rutten can you provide me an example of how to achieve this? I'm pretty new to Fhir and I'm having issues trying to define custom rules. My current use case is I'm receiving a Bundle
representing a Laboratory order via API and I want to validate for example incoming ServiceRequest
in order to see if the requested services are tagged as orderable or not.
Yassiel Oliva (Oct 24 2018 at 16:00):
@Ewout Kramer I'm having troubles running the validation demo offline because of a missing file: specifications.zip
Where do I need to place the file? pasted image
Michel Rutten (Oct 24 2018 at 16:07):
The specification.zip archive is part of the Hl7.Fhir.Specification
NuGet package. After installing the package, this file should be added to the root folder of your project. In Visual Studio, please ensure following settings:
Build Action = Content Copy to Output Directory = Copy if newer
This ensures that the compiler copies specification.zip to the output directory. The API expects this file to be located in the same folder as the assembly.
Michel Rutten (Oct 24 2018 at 16:32):
For information about authoring constraints, see the FHIRPath specification:
http://hl7.org/fhirpath/
Michel Rutten (Oct 24 2018 at 16:32):
For examples, you can inspect the core resources, as many introduce their own constraints.
e.g. pat-1 on Patient:
http://hl7.org/fhir/patient.html#invs
Ewout Kramer (Oct 26 2018 at 10:27):
If you are just running the exe, it should end up in the same directory as the .exe...
Brian Postlethwaite (Oct 27 2018 at 00:02):
Or you can put the file wherever you like - AppData, UserData etc, and use the DirectorySourceResolver with the path that you use.
Yassiel Oliva (Nov 26 2018 at 22:00):
@Ewout Kramer we are having issues to perform the validations. We tried with Fhir.Specification.STU3
and Fhir.Specification.R4
but validation is failing with both validators. We have selected a sample patient info for each version:
https://www.hl7.org/fhir/patient-example-a.json.html
http://hl7.org/fhir/2018Sep/patient-example-a.json.html
var parser = new FhirJsonParser(); var patient = parser.Parse<Patient>(File.ReadAllText("samplePatient.json")); var validator = new Validator(); var result = validator.Validate(patient); Console.WriteLine(result.Success); foreach (var err in result.Issue.Select(e => e.Details.Text)) Console.WriteLine(err);
with both versions we are getting the same validation issue:
Unable to resolve reference to profile 'http://hl7.org/fhir/StructureDefinition/Patient'
Are we missing something?
Richard Kavanagh (Nov 27 2018 at 23:10):
You need to pass some settings into the validator..
ValidationSettings settings = ValidationSettings.Default; settings.EnableXsdValidation = false; settings.Trace = false; settings.ResourceResolver = ...need to add some form of resolver so the validator can find the profiles. settings.TerminologyService = ... need to add some form of terminology server
Ewout Kramer (Nov 28 2018 at 11:04):
Yes - the Validator needs access to the definitions of the FHIR resources, these are normally shipped with the Specification library (specification.zip). That's the first step - the other steps are:
- the Validator will need to be told how to get to that specification.zip.
- This is done by passing it a concrete implementation of IResourceResolver
- The resource resolver can be indicated in the ValidationSettings you pass to the constructor of the validator
The .NET API ships with a few concrete implementations for IResourceResolver - one for resolving references to definitions in the specification.zip, one for resolving it from a directory.
I wrote an example Windows Forms application that does all this (don't worry if you're not on Windows, the code is quite windows independent):
https://github.com/ewoutkramer/Furore.Fhir.ValidationDemo/blob/master/MainForm.cs.
I spoke about this part of the API at DevDays last year: https://vimeo.com/album/5317523/video/243081070 (video), I also have a slide deck about it here: https://www.slideshare.net/DevDays/validation-in-net-and-java-ewout-james
Hope that helps. And yes, I guess I should add some documentation about this at our docs site (http://docs.simplifier.net/fhirnetapi/index.html)
Ewout Kramer (Nov 28 2018 at 11:06):
By the way, the R4 validator is still totally broken - We try to release the first R4-validator in the 1.1 release of the API.
Martin Andersen (Mar 06 2019 at 10:44):
@Ewout Kramer I don't know the state of the R4 validator atm, but is it correct to assume that it's an issue that the R4 validator, can't validate profiles that doesn't have a URL starting with "http://hl7.org/fhir/StructureDefinition/". Specifically code in ProfilePreprocessor.
Martijn Harthoorn (Mar 06 2019 at 13:02):
We are not aware of this issue. And we have some unit tests that seem to say otherwise. Is it possible to send us the profile for us to debug it?
Martin Andersen (Mar 06 2019 at 18:12):
@Martijn Harthoorn Sure I could do that tomorrow when I'm back at work, but the "issue" seems to be located in https://github.com/FirelyTeam/fhir-net-api/blob/develop/src/Hl7.Fhir.Specification/Validation/ProfilePreprocessor.cs
line: if (instance.InstanceType != null) _profiles.SetInstanceType(ModelInfo.CanonicalUriForFhirCoreType(instance.InstanceType));
But maybe im using the validator wrong, but all my debugging leads to that point where it always "thinks" it's a Fhir Core type.
Martin Andersen (Mar 06 2019 at 18:14):
@Martijn Harthoorn I'm using a derived POCO class. But the more I read about Fhir profiling in net-fhir-api, the more it seems to be the wrong approach.
Brian Postlethwaite (Mar 06 2019 at 20:31):
That namespace is where core data types live.
Creating other types in there is bad practice, and will be confusing to look at. HL7 didn't create them...
If its your profile, usie your own namespace.
Brian Postlethwaite (Mar 06 2019 at 20:32):
And from memory, the instance type is always a core type.
Martin Andersen (Mar 06 2019 at 21:28):
@Brian Postlethwaite What namespace are your referring to? I haven't posted the namespace for my derived POCO class anywhere?
Martijn Harthoorn (Mar 07 2019 at 10:47):
I think Brian meant the start of the canonical url, or the canonical base as we call them on Simplifier.
Ewout Kramer (Apr 01 2019 at 12:30):
@Ewout Kramer I don't know the state of the R4 validator atm, but is it correct to assume that it's an issue that the R4 validator, can't validate profiles that doesn't have a URL starting with "http://hl7.org/fhir/StructureDefinition/". Specifically code in ProfilePreprocessor.
That is absolutely correct at the moment, however there is a very recent PR (https://github.com/FirelyTeam/fhir-net-api/pull/917) that will fix this. We expect to have it ready in version 1.3 of the API!
sanjay shekhar (May 06 2019 at 14:13):
I am using hl7.fhir.stu3.specification nuget package for the validation of fhir resource.I have resource with extension which is giving error.
Is there any way to turn off that extension url validation?
Michel Rutten (May 06 2019 at 15:26):
Hi @sanjay shekhar, what kind of error are you getting? The preferred approach would be to fix the models.
sanjay shekhar (May 07 2019 at 04:17):
Hi sanjay shekhar, what kind of error are you getting? The preferred approach would be to fix the models.
I have one observation resource which has extension as following
"extension": [
{
"url": "http:\/\/hl7.org\/fhir\/StructureDefinition\/T_ObservationImageExtension",
"valueReference": {
"reference": "ImagingStudy\/test ImageStudy Extention"
}
}
]
first I parse it using FhirJsonParser to resource
and then pass it to validator
like below
CachedResolver source = new CachedResolver(new MultiResolver(
new DirectorySource(Environment.CurrentDirectory),
ZipSource.CreateValidationSource()));
// prepare the settings for the validator ValidationSettings validationSettings = new ValidationSettings { ResourceResolver = source, GenerateSnapshot = true, Trace = false, EnableXsdValidation = true, ResolveExteralReferences = false, }; // var sd = source.FindStructureDefinition(@"https://fhir.nhs.uk/STU3/StructureDefinition/CareConnect-GPC-Patient-1") Validator validator = new Validator(validationSettings);
I have the specification zip file copied to source directory
using specification hl7.fhir.specification nuget 1.2.1
result operation outcome has this issue
"issue": [
{
"severity": "error",
"code": "incomplete",
"details": {
"coding": [
{
"system": "http://hl7.org/fhir/dotnet-api-operation-outcome",
"code": "4000"
}
],
"text": "Unable to resolve reference to profile 'http://hl7.org/fhir/StructureDefinition/T_ObservationImageExtension'"
},
"location": [
"Observation.extension[0]"
]
}
Michel Rutten (May 07 2019 at 13:49):
The validator needs access to all core & custom profiles that the instance touches (directly or indirectly), in order to perform the validation. If one or more profiles are missing/unresolved, the validation report is incomplete.
I'd suggest to put all your custom profiles in a separate folder and make sure that the DirectorySource
can find them.
Anand Jahagirdar (May 07 2019 at 14:50):
thanks Michel for the reply.
we would need some more information:
1. if we don;t want to create custom profile, is there any way to inform Validation Code to skip the validation of reference? Currently it gives error only for extension having ValueReference param.
2. Please suggest on how to create the profile ? Any hints would be helpful.
Michel Rutten (May 07 2019 at 15:19):
If I understand correctly, I think all you need to do is to put the required extension definitions in a directory, such as the ObervationImageExtension in the example above. This ensures that the validator can verify the extensions that are present in your instance. You don't have to create custom profiles if you don't need them.
Michel Rutten (May 07 2019 at 15:20):
In case you want/decide to create a new/custom profile, you can use e.g. Forge, the FHIR profile editor, which you can download for free from Simplifier: https://simplifier.net/downloads/forge
Michel Rutten (May 07 2019 at 15:21):
To learn more about FHIR profiling, visit e.g. https://simplifier.net/guide/profilingacademy
Michel Rutten (May 07 2019 at 15:22):
(but it looks like you don't really need to create custom profiles in this case)
Anand Jahagirdar (May 07 2019 at 16:13):
thanks Michel.
We actually dont need custom profile. But since we are using FHIR specification validation , the validation fails.
is there any other way to indicate validation that it can skip validation for ImagingStudy
Michel Rutten (May 07 2019 at 16:17):
AFAIK, there is no easy way to skip a specific part of the validation.
Preferred way is to give the validator all the necessary information - otherwise results are not guaranteed to be complete/reliable.
Michel Rutten (May 07 2019 at 16:18):
Did you try to initialize the DirectorySource with the extension definitions that you use? That shouldn't be hard.
Michel Rutten (May 07 2019 at 16:20):
Core extension definitions are part of the spec. You can download them from http://hl7.org/fhir/downloads.html.
You can also download extension definitions from the FHIR registry (https://registry.fhir.org/) or from Simplifier (https://simplifier.net).
Anand Jahagirdar (May 07 2019 at 16:23):
I will try to initialize with extension definition. thanks. I will update here.
Michel Rutten (May 07 2019 at 16:25):
Note: same goes for other dependencies, e.g. if you want the validator to verify your terminology bindings, you'd have to provide the required valuesets.
Brian Postlethwaite (May 07 2019 at 17:39):
I encounter the same issues, and with mine, since I don't care on that specific rule, I just ignore/remove those incomplete errors and let it pass through.
Last updated: Apr 12 2022 at 19:14 UTC