Class: Inferno::DSL::FHIRValidation::Validator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#requirementsObject (readonly)

Returns the value of attribute requirements.


49
50
51
# File 'lib/inferno/dsl/fhir_validation.rb', line 49

def requirements
  @requirements
end

Instance Method Details

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

114
115
116
117
# File 'lib/inferno/dsl/fhir_validation.rb', line 114

def exclude_message(&block)
  @exclude_message = block if block_given?
  @exclude_message
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]

95
96
97
# File 'lib/inferno/dsl/fhir_validation.rb', line 95

def perform_additional_validation(&block)
  additional_validations << block
end

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

[View source]

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/inferno/dsl/fhir_validation.rb', line 120

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
    # This could be a complete failure to connect (validator isn't running)
    # or a timeout (validator took too long to respond).
    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, 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]

65
66
67
68
69
# File 'lib/inferno/dsl/fhir_validation.rb', line 65

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]

209
210
211
# File 'lib/inferno/dsl/fhir_validation.rb', line 209

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