diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index 1b38b541f..deb7717aa 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -110,39 +110,29 @@ public class ValidationService { fileToValidate.getFileContent().getBytes(), fileToValidate.getFileName(), false); - fileToValidate.setFileType(format.getExtension()); + if (format != null) { + fileToValidate.setFileType(format.getExtension()); + } } List messages = new ArrayList<>(); - ValidatedFragments validatedFragments = validator.validateAsFragments(fileToValidate.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fileToValidate.getFileType()), - request.getCliContext().getProfiles(), messages); - - if (validatedFragments.getValidatedFragments().size() == 1 && !validatedFragments.getValidatedFragments().get(0).isDerivedContent()) { - ValidatedFragment validatedFragment = validatedFragments.getValidatedFragments().get(0); - ValidationOutcome outcome = new ValidationOutcome(); - FileInfo fileInfo = new FileInfo( - fileToValidate.getFileName(), - new String(validatedFragment.getContent()), - validatedFragment.getExtension()); - outcome.setMessages(validatedFragment.getErrors()); - outcome.setFileInfo(fileInfo); - response.addOutcome(outcome); - } else { - for (ValidatedFragment validatedFragment : validatedFragments.getValidatedFragments()) { - ValidationOutcome outcome = new ValidationOutcome(); - FileInfo fileInfo = new FileInfo( - validatedFragment.getFilename(), - new String(validatedFragment.getContent()), - validatedFragment.getExtension()); - outcome.setMessages(validatedFragment.getErrors()); - outcome.setFileInfo(fileInfo); + if (fileToValidate.getFileType() == null) { + ValidationOutcome outcome = getValidationOutcomeForUnknownFileFormat( + new FileInfo(fileToValidate.getFileName(), fileToValidate.getFileContent(), null)); response.addOutcome(outcome); - } - } + } else { + ValidatedFragments validatedFragments = validator.validateAsFragments(fileToValidate.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fileToValidate.getFileType()), + request.getCliContext().getProfiles(), messages); - if (request.getCliContext().isShowTimes()) { - response.getValidationTimes().put(fileToValidate.getFileName(), validatedFragments.getValidationTime()); + List validationOutcomes = getValidationOutcomesFromValidatedFragments(fileToValidate, validatedFragments); + for (ValidationOutcome validationOutcome : validationOutcomes) { + response.addOutcome(validationOutcome); + } + + if (request.getCliContext().isShowTimes()) { + response.getValidationTimes().put(fileToValidate.getFileName(), validatedFragments.getValidationTime()); + } } } @@ -150,6 +140,45 @@ public class ValidationService { return response; } + private List getValidationOutcomesFromValidatedFragments(FileInfo fileToValidate, ValidatedFragments validatedFragments) { + List outcomes = new LinkedList<>(); + if (validatedFragments.getValidatedFragments().size() == 1 && !validatedFragments.getValidatedFragments().get(0).isDerivedContent()) { + ValidatedFragment validatedFragment = validatedFragments.getValidatedFragments().get(0); + ValidationOutcome outcome = new ValidationOutcome(); + FileInfo fileInfo = new FileInfo( + fileToValidate.getFileName(), + new String(validatedFragment.getContent()), + validatedFragment.getExtension()); + outcome.setMessages(validatedFragment.getErrors()); + outcome.setFileInfo(fileInfo); + outcomes.add(outcome); + } else { + for (ValidatedFragment validatedFragment : validatedFragments.getValidatedFragments()) { + ValidationOutcome outcome = new ValidationOutcome(); + FileInfo fileInfo = new FileInfo( + validatedFragment.getFilename(), + new String(validatedFragment.getContent()), + validatedFragment.getExtension()); + outcome.setMessages(validatedFragment.getErrors()); + outcome.setFileInfo(fileInfo); + outcomes.add(outcome); + } + } + return outcomes; + } + + private ValidationOutcome getValidationOutcomeForUnknownFileFormat(FileInfo fileInfo) { + ValidationOutcome outcome = new ValidationOutcome(); + + List errorList = new ArrayList<>() {{ + add(new ValidationMessage().setType(ValidationMessage.IssueType.EXCEPTION).setLevel(ValidationMessage.IssueSeverity.FATAL).setMessage("Unable to infer format from file. Please check that your file is in a valid FHIR format.")); + + } }; + outcome.setMessages(errorList); + outcome.setFileInfo(fileInfo); + return outcome; + } + public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception { VersionSourceInformation versions = new VersionSourceInformation(); IgLoader igLoader = new IgLoader(