Class: Inferno::DSL::FHIRResourceValidation::Validator
- Inherits:
-
Object
- Object
- Inferno::DSL::FHIRResourceValidation::Validator
- Defined in:
- lib/inferno/dsl/fhir_resource_validation.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#requirements ⇒ Object
readonly
Returns the value of attribute requirements.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#test_suite_id ⇒ Object
Returns the value of attribute test_suite_id.
Instance Method Summary collapse
- #additional_validation_messages(resource, profile_url) ⇒ Object
- #additional_validations ⇒ Object
- #call_validator(resource, profile_url) ⇒ Object
-
#cli_context(definition = nil) ⇒ Object
Set the cliContext used as part of each validation request.
- #default_validator_url ⇒ Object
-
#exclude_message {|message| ... } ⇒ Object
Filter out unwanted validation messages.
- #filter_messages(message_hashes) ⇒ Object
-
#igs(*validator_igs) ⇒ Object
Set the IGs that the validator will need to load.
-
#initialize(name = nil, test_suite_id = nil, requirements = nil) ⇒ Validator
constructor
A new instance of Validator.
- #issue_message(issue, resource) ⇒ Object
- #issue_severity(issue) ⇒ Object
- #message_hash_from_issue(issue, resource) ⇒ Object
- #message_hashes_from_outcome(outcome, resource, profile_url) ⇒ Object
- #operation_outcome_from_hl7_wrapped_response(response_hash) ⇒ Object
- #operation_outcome_from_validator_response(response, runnable) ⇒ Object
-
#perform_additional_validation {|resource, profile_url| ... } ⇒ Object
Perform validation steps in addition to FHIR validation.
- #remove_invalid_characters(string) ⇒ Object
- #resource_is_valid?(resource, profile_url, runnable) ⇒ Boolean
-
#url(validator_url = nil) ⇒ Object
Set the url of the validator service.
-
#validate(resource, profile_url) ⇒ String
Post a resource to the validation service for validating.
- #validator_session_repo ⇒ Object
- #wrap_resource_for_hl7_wrapper(resource, profile_url) ⇒ Object
Constructor Details
#initialize(name = nil, test_suite_id = nil, requirements = nil) ⇒ Validator
Returns a new instance of Validator.
33 34 35 36 37 38 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 33 def initialize(name = nil, test_suite_id = nil, requirements = nil, &) @name = name @test_suite_id = test_suite_id instance_eval(&) @requirements = requirements end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
30 31 32 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 30 def name @name end |
#requirements ⇒ Object (readonly)
Returns the value of attribute requirements.
29 30 31 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 29 def requirements @requirements end |
#session_id ⇒ Object
Returns the value of attribute session_id.
30 31 32 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 30 def session_id @session_id end |
#test_suite_id ⇒ Object
Returns the value of attribute test_suite_id.
30 31 32 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 30 def test_suite_id @test_suite_id end |
Instance Method Details
#additional_validation_messages(resource, profile_url) ⇒ Object
140 141 142 143 144 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 140 def (resource, profile_url) additional_validations .flat_map { |step| step.call(resource, profile_url) } .select { || .is_a? Hash } end |
#additional_validations ⇒ Object
112 113 114 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 112 def additional_validations @additional_validations ||= [] end |
#call_validator(resource, profile_url) ⇒ Object
278 279 280 281 282 283 284 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 278 def call_validator(resource, profile_url) request_body = wrap_resource_for_hl7_wrapper(resource, profile_url) Faraday.new( url, request: { timeout: 600 } ).post('validate', request_body, content_type: 'application/json') end |
#cli_context(definition = nil) ⇒ Object
Set the cliContext used as part of each validation request. Fields may be passed as either a Hash or block. Note that all fields included here will be sent directly in requests, there is no check that the fields are correct.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 98 def cli_context(definition = nil, &) if @cli_context if definition @cli_context.definition.merge!(definition.deep_symbolize_keys) elsif block_given? @cli_context.instance_eval(&) end else @cli_context = CliContext.new(definition || {}, &) end @cli_context end |
#default_validator_url ⇒ Object
41 42 43 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 41 def default_validator_url ENV.fetch('FHIR_RESOURCE_VALIDATOR_URL') end |
#exclude_message {|message| ... } ⇒ Object
Filter out unwanted validation messages. Any messages for which the block evalutates to a truthy value will be excluded.
154 155 156 157 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 154 def (&block) @exclude_message = block if block_given? @exclude_message end |
#filter_messages(message_hashes) ⇒ Object
195 196 197 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 195 def () .reject! { || .call(Entities::Message.new()) } if end |
#igs(*validator_igs) ⇒ Object
Set the IGs that the validator will need to load
64 65 66 67 68 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 64 def igs(*validator_igs) cli_context(igs: validator_igs) if validator_igs cli_context.igs end |
#issue_message(issue, resource) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 231 def (issue, resource) location = if issue.respond_to?(:expression) issue.expression&.join(', ') else issue.location&.join(', ') end location_prefix = resource.id ? "#{resource.resourceType}/#{resource.id}" : resource.resourceType "#{location_prefix}: #{location}: #{issue&.details&.text}" end |
#issue_severity(issue) ⇒ Object
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 219 def issue_severity(issue) case issue.severity when 'warning' 'warning' when 'information' 'info' else 'error' end end |
#message_hash_from_issue(issue, resource) ⇒ Object
211 212 213 214 215 216 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 211 def (issue, resource) { type: issue_severity(issue), message: (issue, resource) } end |
#message_hashes_from_outcome(outcome, resource, profile_url) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 200 def (outcome, resource, profile_url) = outcome.issue&.map { |issue| (issue, resource) } || [] .concat((resource, profile_url)) () end |
#operation_outcome_from_hl7_wrapped_response(response_hash) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 287 def operation_outcome_from_hl7_wrapped_response(response_hash) # This is a workaround for some test kits which for legacy reasons # call this method directly with a String instead of a Hash. # See FI-3178. response_hash = JSON.parse(remove_invalid_characters(response_hash)) if response_hash.is_a? String if response_hash['sessionId'] && response_hash['sessionId'] != @session_id validator_session_repo.save(test_suite_id:, validator_session_id: response_hash['sessionId'], validator_name: name.to_s, suite_options: requirements) @session_id = response_hash['sessionId'] end # assume for now that one resource -> one request issues = (response_hash.dig('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 |
#operation_outcome_from_validator_response(response, runnable) ⇒ Object
313 314 315 316 317 318 319 320 321 322 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 313 def operation_outcome_from_validator_response(response, runnable) sanitized_body = remove_invalid_characters(response.body) operation_outcome_from_hl7_wrapped_response(JSON.parse(sanitized_body)) rescue JSON::ParserError runnable.('error', "Validator Response: HTTP #{response.status}\n#{sanitized_body}") raise Inferno::Exceptions::ErrorInValidatorException, 'Validator response was an unexpected format. ' \ 'Review Messages tab or validator service logs for more information.' end |
#perform_additional_validation {|resource, profile_url| ... } ⇒ Object
Perform validation steps in addition to FHIR validation.
135 136 137 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 135 def perform_additional_validation(&block) additional_validations << block end |
#remove_invalid_characters(string) ⇒ Object
308 309 310 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 308 def remove_invalid_characters(string) string.gsub(/[^[:print:]\r\n]+/, '') end |
#resource_is_valid?(resource, profile_url, runnable) ⇒ Boolean
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 160 def resource_is_valid?(resource, profile_url, runnable) profile_url ||= FHIR::Definitions.resource_definition(resource.resourceType).url begin response = call_validator(resource, profile_url) rescue StandardError => e # This could be a complete failure to connect (validator isn't running) # or a timeout (validator took too long to respond). runnable.('error', e.) raise Inferno::Exceptions::ErrorInValidatorException, "Unable to connect to validator at #{url}." end outcome = operation_outcome_from_validator_response(response, runnable) = (outcome, resource, profile_url) .each { || runnable.([:type], [:message]) } unless response.status == 200 raise Inferno::Exceptions::ErrorInValidatorException, 'Error occurred in the validator. Review Messages tab or validator service logs for more information.' end .none? { || [:type] == 'error' } rescue Inferno::Exceptions::ErrorInValidatorException raise rescue StandardError => e runnable.('error', e.) raise Inferno::Exceptions::ErrorInValidatorException, 'Error occurred in the validator. Review Messages tab or validator service logs for more information.' end |
#url(validator_url = nil) ⇒ Object
Set the url of the validator service
52 53 54 55 56 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 52 def url(validator_url = nil) @url = validator_url if validator_url @url ||= default_validator_url @url end |
#validate(resource, profile_url) ⇒ String
Post a resource to the validation service for validating.
273 274 275 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 273 def validate(resource, profile_url) call_validator(resource, profile_url).body end |
#validator_session_repo ⇒ Object
45 46 47 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 45 def validator_session_repo @validator_session_repo ||= Inferno::Repositories::ValidatorSessions.new end |
#wrap_resource_for_hl7_wrapper(resource, profile_url) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 244 def wrap_resource_for_hl7_wrapper(resource, profile_url) validator_session_id = validator_session_repo.find_validator_session_id(test_suite_id, name.to_s, requirements) @session_id = validator_session_id if validator_session_id wrapped_resource = { cliContext: { **cli_context.definition, profiles: [profile_url] }, filesToValidate: [ { fileName: "#{resource.resourceType}/#{resource.id}.json", fileContent: resource.source_contents, fileType: 'json' } ], sessionId: @session_id } wrapped_resource.to_json end |