# Java Based Repository Validation

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](/content/docs/interceptors/index.html).

This interceptor may apply any rules described in the HAPI FHIR [Repository Validating Interceptor](/content/hapi-fhir/docs/validation/repository_validating_interceptor.html) documentation.

The following example shows a simple repository validating interceptor usable within a Smile CDR FHIR Storage module.

```java
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());
   }
}
```

Copy

## Validation and Best Practice Warnings

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.

[Repository Validation](/content/docs/validation_and_conformance/repository_validation.html)

Repository Validation: Java

Validation and Conformance

[Introduction](/content/docs/validation_and_conformance/introduction.html)
[Validation Support Repository](/content/docs/validation_and_conformance/validation_support_repository.html)
[Validation Support Repository Options](/content/docs/validation_and_conformance/validation_support_repository_options.html)
[Conformance Data](/content/docs/validation_and_conformance/conformance_data.html)
[Repository Validation](/content/docs/validation_and_conformance/repository_validation.html)
[Repository Validation: Java](/content/docs/validation_and_conformance/repository_validation_java.html)
[Repository Validation: Javascript](/content/docs/validation_and_conformance/repository_validation_javascript.html)
[Repository Validation: Custom Validation Beans](/content/docs/validation_and_conformance/repository_validation_bean.html)
[Endpoint Validation](/content/docs/validation_and_conformance/endpoint_validation.html)
[Packages and Implementation Guides](/content/docs/validation_and_conformance/packages_and_igs.html)
[Package Registry Endpoint Module](/content/docs/validation_and_conformance/package_registry_endpoint_module.html)
[Remote Terminology Services](/content/docs/validation_and_conformance/remote_terminology_services.html)
[Suppressing Messages](/content/docs/validation_and_conformance/suppressing_messages.html)
[Validation Performance](/content/docs/validation_and_conformance/validation_performance.html)
[Automatic Provenance Injection](/content/docs/validation_and_conformance/automatic_provenance_injection.html)

[Repository Validation: Javascript](/content/docs/validation_and_conformance/repository_validation_javascript.html)
