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.



45
46
47
# File 'lib/inferno/dsl/fhir_validation.rb', line 45

def requirements
  @requirements
end

Instance Method Details

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

Filter out unwanted validation messages

Examples:

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

Yield Parameters:



109
110
111
112
# File 'lib/inferno/dsl/fhir_validation.rb', line 109

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.



91
92
93
# File 'lib/inferno/dsl/fhir_validation.rb', line 91

def perform_additional_validation(&block)
  additional_validations << block
end

#resource_is_valid?(resource, profile_url, runnable) ⇒ Boolean



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/inferno/dsl/fhir_validation.rb', line 115

def resource_is_valid?(resource, profile_url, runnable)
  profile_url ||= FHIR::Definitions.resource_definition(resource.resourceType).url

  outcome = FHIR::OperationOutcome.new(JSON.parse(validate(resource, profile_url)))

  message_hashes = outcome.issue&.map { |issue| message_hash_from_issue(issue, resource) } || []

  message_hashes.concat(additional_validation_messages(resource, profile_url))

  filter_messages(message_hashes)

  message_hashes
    .each { |message_hash| runnable.add_message(message_hash[:type], message_hash[:message]) }
    .none? { |message_hash| message_hash[:type] == 'error' }
end

#url(validator_url = nil) ⇒ Object

Set the url of the validator service

Parameters:

  • validator_url (String) (defaults to: nil)


61
62
63
64
65
# File 'lib/inferno/dsl/fhir_validation.rb', line 61

def url(validator_url = nil)
  @url = validator_url if 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



174
175
176
177
178
179
# File 'lib/inferno/dsl/fhir_validation.rb', line 174

def validate(resource, profile_url)
  Faraday.new(
    url,
    params: { profile: profile_url }
  ).post('validate', resource.source_contents).body
end