Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Proof of Concept of HL7 Validator Wrapper integration #396

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions lib/inferno/dsl/fhir_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ def url(validator_url = nil)
@url
end

# Set the IGs that the validator will need to load
# Example: ["hl7.fhir.us.core#4.0.0"]
# @param igs [Array<String>]
def igs(validator_igs = nil)
@igs = validator_igs if validator_igs

@igs
end

# @private
def additional_validations
@additional_validations ||= []
Expand Down Expand Up @@ -187,6 +196,31 @@ def issue_message(issue, resource)
"#{location_prefix}: #{location}: #{issue&.details&.text}"
end

# @private
def wrap_resource_for_hl7_wrapper(resource, profile_url)
wrapped_resource = {
cliContext: {
# TODO: these should be configurable as well
sv: '4.0.1',
# displayWarnings: true, # -display-issues-are-warnings
# txServer: nil, # -tx n/a
igs: @igs || [],
# NOTE: this profile must be part of a loaded IG,
# otherwise the response is an HTTP 500 with no content
profiles: [profile_url]
},
filesToValidate: [
{
fileName: 'manually_entered_file.json',
fileContent: resource.to_json,
fileType: 'json'
}
],
sessionId: @session_id
}
wrapped_resource.to_json
end

# Post a resource to the validation service for validating.
#
# @param resource [FHIR::Model]
Expand All @@ -195,25 +229,38 @@ def issue_message(issue, resource)
# @return [[Array(FHIR::OperationOutcome, Number)] the validation response and HTTP status code
def validate(resource, profile_url, runnable)
begin
request_body = wrap_resource_for_hl7_wrapper(resource, profile_url)
response = Faraday.new(
url,
params: { profile: profile_url }
).post('validate', resource.source_contents)
url
).post('validate', request_body, content_type: 'application/json')
rescue StandardError => e
runnable.add_message('error', e.message)
raise Inferno::Exceptions::ErrorInValidatorException, "Unable to connect to validator at #{url}."
end
outcome = operation_outcome_from_validator_response(response.body, runnable)

outcome = operation_outcome_from_validator_response(response, runnable)
[outcome, response.status]
end

# @private
def operation_outcome_from_hl7_wrapped_response(response)
res = JSON.parse(response)

@session_id = res['sessionId']

# assume for now that one resource -> one request
issues = res['outcomes'][0]['issues']&.map do |i|
{ severity: i['level'].downcase, expression: i['location'], details: { text: i['message'] } }
end
# this is circuitous, ideally we would map this response directly to message_hashes
FHIR::OperationOutcome.new(issue: issues)
end

# @private
def operation_outcome_from_validator_response(response, runnable)
if response.start_with? '{'
FHIR::OperationOutcome.new(JSON.parse(response))
if response.body.start_with? '{'
operation_outcome_from_hl7_wrapped_response(response.body)
else
runnable.add_message('error', "Validator Response: #{response}")
runnable.add_message('error', "Validator Response: HTTP #{response.status}\n#{response.body}")
raise Inferno::Exceptions::ErrorInValidatorException,
'Validator response was an unexpected format. '\
'Review Messages tab or validator service logs for more information.'
Expand Down