Class: Inferno::DSL::FHIRResourceValidation::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/dsl/fhir_resource_validation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.


42
43
44
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 42

def name
  @name
end

#requirementsObject (readonly)

Returns the value of attribute requirements.


41
42
43
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 41

def requirements
  @requirements
end

#session_idObject

Returns the value of attribute session_id.


42
43
44
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 42

def session_id
  @session_id
end

#test_suite_idObject

Returns the value of attribute test_suite_id.


42
43
44
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 42

def test_suite_id
  @test_suite_id
end

Instance Method Details

#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.

Examples:

# Passing fields in a block
fhir_resource_validator do
  url 'http://example.com/validator'
  cli_context do
    noExtensibleBindingMessages true
    allowExampleUrls true
    txServer nil
  end
end
# Passing fields in a Hash
fhir_resource_validator do
  url 'http://example.org/validator'
  cli_context({
    noExtensibleBindingMessages: true,
    allowExampleUrls: true,
    txServer: nil
  })
end

Parameters:

  • definition (Hash) (defaults to: nil)

    raw fields to set, optional

[View source]

110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 110

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

#exclude_message {|message| ... } ⇒ Object

Filter out unwanted validation messages. Any messages for which the block evalutates to a truthy value will be excluded.

Examples:

validator do
  exclude_message { |message| message.type == 'info' }
end

Yield Parameters:

[View source]

166
167
168
169
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 166

def exclude_message(&block)
  @exclude_message = block if block_given?
  @exclude_message
end

#igs(*validator_igs) ⇒ Object

Set the IGs that the validator will need to load

Examples:

igs "hl7.fhir.us.core#4.0.0"
igs("hl7.fhir.us.core#3.1.1", "hl7.fhir.us.core#6.0.0")

Parameters:

  • validator_igs (Array<String>)
[View source]

76
77
78
79
80
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 76

def igs(*validator_igs)
  cli_context(igs: validator_igs) if validator_igs.any?

  cli_context.igs
end

#perform_additional_validation {|resource, profile_url| ... } ⇒ Object

Perform validation steps in addition to FHIR validation.

Examples:

perform_additional_validation do |resource, profile_url|
  if something_is_wrong
    { type: 'error', message: 'something is wrong' }
  else
    { type: 'info', message: 'everything is ok' }
  end
end

Yield Parameters:

  • resource (FHIR::Model)

    the resource being validated

  • profile_url (String)

    the profile the resource is being validated against

Yield Returns:

  • (Array<Hash<Symbol, String>>, Hash<Symbol, String>)

    The block should return a Hash or an Array of Hashes if any validation messages should be added. The Hash must contain two keys: :type and :message. :type can have a value of 'info', 'warning', or 'error'. A type of 'error' means the resource is invalid. :message contains the message string itself.

[View source]

147
148
149
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 147

def perform_additional_validation(&block)
  additional_validations << block
end

#resource_is_valid?(resource, profile_url, runnable, add_messages_to_runnable: true) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Inferno::DSL::FHIRResourceValidation#resource_is_valid?
[View source]

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 172

def resource_is_valid?(resource, profile_url, runnable, add_messages_to_runnable: true) # rubocop:disable Metrics/CyclomaticComplexity
  profile_url ||= FHIR::Definitions.resource_definition(resource.resourceType).url

  begin
    response = call_validator(resource, profile_url)
  rescue StandardError => e
    runnable.add_message('error', e.message)
    Application[:logger].error(e.message)

    raise Inferno::Exceptions::ErrorInValidatorException, validator_error_message(e)
  end

  outcome = operation_outcome_from_validator_response(response, runnable)

  message_hashes = message_hashes_from_outcome(outcome, resource, profile_url)

  if add_messages_to_runnable
    message_hashes
      .each { |message_hash| runnable.add_message(message_hash[:type], message_hash[:message]) }
  end

  unless response.status == 200
    raise Inferno::Exceptions::ErrorInValidatorException,
          'Error occurred in the validator. Review Messages tab or validator service logs for more information.'
  end

  message_hashes
    .none? { |message_hash| message_hash[:type] == 'error' }
rescue Inferno::Exceptions::ErrorInValidatorException
  raise
rescue StandardError => e
  runnable.add_message('error', e.message)
  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

Parameters:

  • validator_url (String) (defaults to: nil)
[View source]

64
65
66
67
68
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 64

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.

Parameters:

Returns:

  • (String)

    the body of the validation response

[View source]

293
294
295
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 293

def validate(resource, profile_url)
  call_validator(resource, profile_url).body
end

#validator_session_repoObject

[View source]

57
58
59
# File 'lib/inferno/dsl/fhir_resource_validation.rb', line 57

def validator_session_repo
  @validator_session_repo ||= Inferno::Repositories::ValidatorSessions.new
end