Stream: hapi
Topic: Validation with a Profile
Håkan MacLean (Nov 24 2020 at 12:40):
Hi! I must be missing something fundamental here, but I've tried to follow the tutorial to try validation against a published profile, but I just can't get it to work. I have tried adding the profile both in the Resource and to the validator (I'm guessing you should only need to do one of them). This is my code:
MedicationRequest rx =
new MedicationRequest()
.setSubject(new Reference("subject_id"))
.setStatus(MedicationRequest.MedicationRequestStatus.ACTIVE);
rx.setMeta(
new Meta()
.addProfile( "https://fhir.simplifier.net/SwedishNationalMedicationList/StructureDefinition/dcae1550-a48a-4d44-a431-73c47c9dd075"));
FhirContext ctx = FhirContext.forR4();
FhirValidator validator = ctx.newValidator();
IValidatorModule module = new FhirInstanceValidator(ctx);
validator.registerValidatorModule(module);
ValidationOptions validationOptions = new ValidationOptions().addProfile("http://fhir.simplifier.net/SwedishNationalMedicationList/StructureDefinition/dcae1550-a48a-4d44-a431-73c47c9dd075");
ValidationResult result = validator.validateWithResult(rx, validationOptions);
if (!result.isSuccessful()) {
result.getMessages().forEach(msg -> System.out.println(msg.getMessage()));
}
Assert.assertTrue(result.isSuccessful());
And the output is:
Profile reference 'https://fhir.simplifier.net/SwedishNationalMedicationList/StructureDefinition/dcae1550-a48a-4d44-a431-73c47c9dd075' could not be resolved, so has not been checked
I have tried using another profile as well with the same result.
Grahame Grieve (Nov 24 2020 at 19:31):
what's XXXX?
Håkan MacLean (Nov 24 2020 at 20:23):
Grahame Grieve said:
what's XXXX?
Sorry @Grahame Grieve , don't know why I removed the link. I updated the example to reflect it. Am I using the wrong URL somehow? When I visit the URL a get a JSON with a StructureDefinition, so it makes sense to me.
Grahame Grieve (Nov 24 2020 at 20:25):
what's not obvious from what you've written is why you expect 'https://fhir.simplifier.net/SwedishNationalMedicationList/StructureDefinition/dcae1550-a48a-4d44-a431-73c47c9dd075' to be resolved
Håkan MacLean (Nov 24 2020 at 20:29):
When I hit https://fhir.simplifier.net/SwedishNationalMedicationList/StructureDefinition/dcae1550-a48a-4d44-a431-73c47c9dd075 with Postman I get back a FHIR JSON object with a StructureDefinition.
I also tried the same code, but creating a Patient pointing to http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient, but got the same result, which is why I expected it to work? But I suspect I have misunderstood something fundamentally here :)
Grahame Grieve (Nov 24 2020 at 20:30):
the stand alone validator will automatically resolve links like this - mostly - but when you host the validator in HAPI, you have total control over how it acts (e.g. external dependencies), which also gives you the obligation to ensure content is available, or you have implemented the right interfaces
Grahame Grieve (Nov 24 2020 at 20:30):
but do you really need to host the validator in HAPI?
Grahame Grieve (Nov 24 2020 at 20:31):
what are you actually trying to achieve?
Håkan MacLean (Nov 24 2020 at 21:04):
I'm writing unit tests and just want to verify that the MedicationRequests we are creating are complying with a certain Profile.
When you write:
which also gives you the obligation to ensure content is available, or you have implemented the right interfaces
Do you mean that my code is missing something? I was trying to follow the example in the HAPI tutorial. Right now I still haven't quite understood what is wrong in my example.
Grahame Grieve (Nov 24 2020 at 21:41):
you probably don't want to use the HAPI framework for the validator there. You really want to use the standalone validator. @Mark Iantorno is doing a big lift on the validator framework at the moment, and we're making something better for JUnit testing.
Grahame Grieve (Nov 24 2020 at 21:41):
the real problem is the start up time - you certainly don't want the start up on each junit test. Could you run the valdiator as a local web service?
Håkan MacLean (Nov 24 2020 at 22:20):
No the startup time is of course a problem, but I thought it would be nice to have just one regression test in our test suit that verifies that we haven't changed anything that breaks the FHIR Profile. So are you saying the HAPI validator currently does not support this use case? In the documentation here https://hapifhir.io/hapi-fhir/docs/validation/instance_validator.html it talks about doing exactly what I want to achieve:
/*
- Note: You can also explicitly declare a profile to validate against
- using the block below.
*/
// ValidationResult result = validator.validateWithResult(obs, new ValidationOptions().addProfile("http://myprofile.com"));
Sorry for being so stubborn, but I still haven't quite understood why my simple example does not work as it seems perfectly aligned with what the docs suggest :)
Grahame Grieve (Nov 25 2020 at 02:15):
that part works; what doesn't work - and should probably be mentioned there - is that you first have to ensure that that profile and anything it depends on is already loaded
Grahame Grieve (Nov 25 2020 at 02:20):
I think, though, that what you are doing is better served by just using the validator directly - say, the class org.hl7.fhir.validation.ValidationEngine. You can see how I use in Junit tests looking here: org.hl7.fhir.validation.tests.ValidationEngineTests, or org.hl7.fhir.validation.tests.ValidationTests, though the second is much complicated by being the 'test everything about the validation logic' test set
Grahame Grieve (Nov 25 2020 at 02:21):
the validation engine in that case is surrounded by a bunch of code that (probably) should do what you're expecting
Grahame Grieve (Nov 25 2020 at 02:21):
where as HAPI explicitly doesn't, since the general design guideline is that HAPI servers don't use the web directly, especially not implicitly; only be explicit system owner choice
Oliver Egger (Nov 25 2020 at 08:37):
one possibility to load profiles into hapi is providing it via an implementation guide. the hapi-fhir-jpaserver-starter load's the provided ig's from the configuration. We use this approach for exposing a $validate operation on a server and are testing this with all implementation guides examples the server is configured. If you are not using the JPA stack you can use ValidationEngine as Grahame has explained.
Mirjam Baltus (Nov 25 2020 at 10:48):
@Håkan MacLean I think your profile URL is not correct in your code example. Yes, you can retrieve a StructureDefinition from it, but that is because it is the Simplifier endpoint and technical id for that FHIR resource. In the resource instance metadata, and also for validating, you need to use the canonical URL of the profile, which is in the StructureDefinition.url field. In this case it reads "http://electronichealth.se/fhir/StructureDefinition/NLLMedicationRequest".
Kashyap Jois (Mar 30 2021 at 11:54):
Hi I am currently trying to use the ValidationEngine to validate a resource against a profile. I am using the profile the us-core patient . I keep getting the error Unable to resolve profile. I was wondering if there is any way to validate a resource against a profile that is loaded as a StructureDefinition.
My use case is to use the profile described here to validate a bundle that is sent from a mobile client
Kashyap Jois (Mar 30 2021 at 13:13):
Ok looks like I need to use
validator.loadIg(src);
call this any number of times for the Implementation Guide(s) of interest.
See https://confluence.hl7.org/display/FHIR/Using+the+FHIR+Validator for documentation about the src parameter (-ig parameter)
and load the ig which contains all the profiles. But weirdly enough the function loadIg does not exist..
Reference
Kashyap Jois (Apr 06 2021 at 11:12):
For anyone else who gets stuck due to this, you can use the validators igLoader, load the implementation guide, and then add all the profiles to the cache of the validators context.
Clotaire Delanchy (Apr 23 2021 at 12:28):
Hello everyone.
I have just discovered FHIR and HAPI FHIR these last days and it seems I have the same problem as @Håkan MacLean: I am creating JSON resources which are based on custom Profiles (available on Simplifier) and I want to validate (manually) that my JSON resources are valid against the profile (by pushing them to a FHIR server with $validate option or just seeing the return when trying to create the resource in the server). I've found that there is a CLI tool for HAPI FHIR server and I am using it to start a local HAPI FHIR very easily. But this HAPI FHIR server starting with the CLI tool use only "basic" FHIR resource, so it validates my resource at creation against the Patient profile (for example) and not the FrPatient profile available on Simplifier.
If I understand correctly the answer of @Oliver Egger and @Mirjam Baltus, I can upload the implementation Guide (containing the link to my custom profiles) to the HAPI server by editing the configuration file. But how can I access the configuration file with the CLI tool? Is it possible to give a custom config file to the HAPI server through this command line interface?
Thank you in advance for your answers, and if I'm not posting in the right topic or stream, don't hesitate to tell me!
Oliver Egger (Apr 24 2021 at 13:51):
@Clotaire Delanchy if you want to validate manually I would suggest to start with the FHIR Java validator. You can specify your custom profile and the dependent packages.
Eric Haas (Jan 04 2022 at 23:28):
(deleted)
Last updated: Apr 12 2022 at 19:14 UTC