Repository Validation: Java
To enable Repository Validation using Java code, a custom interceptor class must be created and registered against the FHIR Storage module as described on the Interceptors Page.
This interceptor may apply any rules described in the HAPI FHIR Repository Validating Interceptor documentation.
The following example shows a simple repository validating interceptor usable within a Smile CDR FHIR Storage module.
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.jpa.interceptor.validation.RepositoryValidatingInterceptor;
import ca.uhn.fhir.jpa.interceptor.validation.RepositoryValidatingRuleBuilder;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
/**
* This interceptor is intended to be registered against a Smile CDR FHIR Storage module.
*/
@Interceptor
public class DemoRepositoryValidatingInterceptor extends RepositoryValidatingInterceptor {
@Autowired
private FhirContext myFhirContext;
@Autowired
private ApplicationContext myApplicationContext;
/**
* This method will be called at startup time
*/
@PostConstruct
public void start() {
setFhirContext(myFhirContext);
// Ask the application context for a new Rule Builder
RepositoryValidatingRuleBuilder ruleBuilder =
myApplicationContext.getBean(RepositoryValidatingRuleBuilder.class);
// Here we will mandate only that any Patient resources stored in the repository
// must declare conformance to the US Core profile, and must correctly validate.
// You may add as many rules for as many resource types as you like here.
ruleBuilder
.forResourcesOfType("Patient")
.requireAtLeastProfile("http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient")
.and()
.requireValidationToDeclaredProfiles();
// Resources of types other than Patient will not be subject to any rules by
// this interceptor as it is, but more rules could be added here.
// Create the ruleset and pass it to the interceptor
setRules(ruleBuilder.build());
}
}
When ingesting HL7 v2.x and other data, it may be convenient to disable best-practice warnings. This can be done by appending .withBestPracticeWarningLevel("IGNORE")
to one's .requireValidationToDeclaredProfiles()
calls. It is worth noting that Smile CDR's HL7 v2.x data conversion is not guaranteed to produce resources that conform to anything other than the core FHIR spec.