ValidationCLI fixes: --igpack now works; ValidationSupportChain has corrected isEnabledValidationForCodingsLogicalAnd()

This commit is contained in:
Stuart McGrigor 2024-07-03 10:24:28 +12:00
parent cf7de9bc7e
commit 7835064044
3 changed files with 65 additions and 8 deletions

View File

@ -35,6 +35,7 @@ import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.text.WordUtils; import org.apache.commons.lang3.text.WordUtils;
import org.fusesource.jansi.Ansi.Color; import org.fusesource.jansi.Ansi.Color;
import org.hl7.fhir.common.hapi.validation.support.NpmPackageValidationSupport;
import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain; import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator; import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
@ -149,21 +150,25 @@ public class ValidateCommand extends BaseCommand {
} }
if (theCommandLine.hasOption("p")) { if (theCommandLine.hasOption("p")) {
ValidationSupportChain validationSupportChain;
switch (ctx.getVersion().getVersion()) { switch (ctx.getVersion().getVersion()) {
case DSTU2: { case DSTU2: {
FhirInstanceValidator instanceValidator; FhirInstanceValidator instanceValidator;
ValidationSupportChain validationSupportChain = validationSupportChain =
ValidationSupportChainCreator.getValidationSupportChainDstu2(ctx, theCommandLine); ValidationSupportChainCreator.getValidationSupportChainDstu2(ctx, theCommandLine);
validationSupportChain.oneCodingIsSufficient = true;
instanceValidator = new FhirInstanceValidator(validationSupportChain); instanceValidator = new FhirInstanceValidator(validationSupportChain);
val.registerValidatorModule(instanceValidator); val.registerValidatorModule(instanceValidator);
break; break;
} }
case DSTU3: case DSTU3:
case R4: { case R4: {
ourLog.info("Adding FHIR R4 validation support chain");
FhirInstanceValidator instanceValidator = new FhirInstanceValidator(ctx); FhirInstanceValidator instanceValidator = new FhirInstanceValidator(ctx);
val.registerValidatorModule(instanceValidator); val.registerValidatorModule(instanceValidator);
ValidationSupportChain validationSupportChain = validationSupportChain =
ValidationSupportChainCreator.getValidationSupportChainR4(ctx, theCommandLine); ValidationSupportChainCreator.getValidationSupportChainR4(ctx, theCommandLine);
validationSupportChain.oneCodingIsSufficient = true;
instanceValidator.setValidationSupport(validationSupportChain); instanceValidator.setValidationSupport(validationSupportChain);
break; break;
} }
@ -171,6 +176,21 @@ public class ValidateCommand extends BaseCommand {
throw new ParseException( throw new ParseException(
Msg.code(1620) + "Profile validation (-p) is not supported for this FHIR version"); Msg.code(1620) + "Profile validation (-p) is not supported for this FHIR version");
} }
// If they want to do profile validation - there might be a whole IG to load...
if (theCommandLine.hasOption("igpack")) {
String igPack = theCommandLine.getOptionValue("igpack");
ourLog.info("Loading IG Package from {} to the validation support chain", igPack);
try {
NpmPackageValidationSupport npmPackageSupport = new NpmPackageValidationSupport(ctx);
npmPackageSupport.loadPackageFromFile(igPack);
validationSupportChain.addValidationSupport(0, npmPackageSupport);
} catch (IOException iox) {
throw new ParseException(
Msg.code(1620) + "IGpack validation (--igpack) failed: " + iox.getMessage());
}
ourLog.info("Completed loading IG Package from {}", igPack);
}
} }
val.setValidateAgainstStandardSchema(theCommandLine.hasOption("x")); val.setValidateAgainstStandardSchema(theCommandLine.hasOption("x"));

View File

@ -3,13 +3,14 @@ package org.hl7.fhir.common.hapi.validation.support;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.parser.LenientErrorHandler; import ca.uhn.fhir.parser.LenientErrorHandler;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.util.ClasspathUtil; import ca.uhn.fhir.util.ClasspathUtil;
import jakarta.annotation.Nonnull; import jakarta.annotation.Nonnull;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.npm.NpmPackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -31,11 +32,16 @@ public class NpmPackageValidationSupport extends PrePopulatedValidationSupport {
super(theFhirContext); super(theFhirContext);
} }
@Override
public String getName() {
return getFhirContext().getVersion().getVersion() + " NPM Package Validation Support";
}
/** /**
* Load an NPM package using a classpath specification, e.g. <code>/path/to/resource/my_package.tgz</code>. The * Load an NPM package using a classpath specification, e.g. <code>/path/to/resource/my_package.tgz</code>. The
* classpath spec can optionally be prefixed with the string <code>classpath:</code> * classpath spec can optionally be prefixed with the string <code>classpath:</code>
* *
* @throws InternalErrorException If the classpath file can't be found * @throws IOException If the classpath file can't be found
*/ */
public void loadPackageFromClasspath(String theClasspath) throws IOException { public void loadPackageFromClasspath(String theClasspath) throws IOException {
try (InputStream is = ClasspathUtil.loadResourceAsStream(theClasspath)) { try (InputStream is = ClasspathUtil.loadResourceAsStream(theClasspath)) {
@ -47,6 +53,21 @@ public class NpmPackageValidationSupport extends PrePopulatedValidationSupport {
} }
} }
/**
* Load an NPM package from the filesystem e.g. <code>my_package.tgz</code>.
*
* @throws IOException If the package file can't be found
*/
public void loadPackageFromFile(String theFile) throws IOException {
File inFile = new File(theFile);
InputStream is = new FileInputStream(inFile);
NpmPackage pkg = NpmPackage.fromPackage(is);
if (pkg.getFolders().containsKey("package")) {
loadResourcesFromPackage(pkg);
loadBinariesFromPackage(pkg);
}
}
private void loadResourcesFromPackage(NpmPackage thePackage) { private void loadResourcesFromPackage(NpmPackage thePackage) {
NpmPackage.NpmPackageFolder packageFolder = thePackage.getFolders().get("package"); NpmPackage.NpmPackageFolder packageFolder = thePackage.getFolders().get("package");

View File

@ -371,12 +371,13 @@ public class ValidationSupportChain implements IValidationSupport {
if (retVal != null) { if (retVal != null) {
if (ourLog.isDebugEnabled()) { if (ourLog.isDebugEnabled()) {
ourLog.debug( ourLog.debug(
"Code {}|{} '{}' in ValueSet {} validated by {}", "Code {}|{} '{}' in ValueSet {} validated by {} result: {}",
theCodeSystem, theCodeSystem,
theCode, theCode,
theDisplay, theDisplay,
theValueSetUrl, theValueSetUrl,
next.getName()); next.getName(),
retVal.getMessage() != null ? retVal.getMessage() : "Success");
} }
return retVal; return retVal;
} }
@ -401,12 +402,13 @@ public class ValidationSupportChain implements IValidationSupport {
if (retVal != null) { if (retVal != null) {
if (ourLog.isDebugEnabled()) { if (ourLog.isDebugEnabled()) {
ourLog.debug( ourLog.debug(
"Code {}|{} '{}' in ValueSet {} validated by {}", "Code {}|{} '{}' in ValueSet {} validated by {} result: {}",
theCodeSystem, theCodeSystem,
theCode, theCode,
theDisplay, theDisplay,
theValueSet.getIdElement(), theValueSet.getIdElement(),
next.getName()); next.getName(),
retVal.getMessage() != null ? retVal.getMessage() : "Success");
} }
return retVal; return retVal;
} }
@ -445,4 +447,18 @@ public class ValidationSupportChain implements IValidationSupport {
} }
return null; return null;
} }
/**
* See VersionSpecificWorkerContextWrapper#validateCode in hapi-fhir-validation.
* <p>
* If true, validation for codings will return a positive result if AT LEAST ONE coding is valid.
* If false, validation for codings will return a positive result if there is ALL codings are valid.
* </p>
* @return if the application has configured validation to use logical AND, as opposed to logical OR, which is the default
*/
public boolean oneCodingIsSufficient = false; // default value from parent Interface (it's the wrong way round !!!)
@Override
public boolean isEnabledValidationForCodingsLogicalAnd() { // should be named ....LogicalAny
return oneCodingIsSufficient;
}
} }