Skip to content

Commit

Permalink
FI-3178: Validate Resource Without Validation Messages (#595)
Browse files Browse the repository at this point in the history
* update resource_is_valid method to conditionally loggin messages to runnable

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>

* update spec

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>

* update doc

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>

* update param name

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>

---------

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>
  • Loading branch information
vanessuniq authored Jan 22, 2025
1 parent 3a23fe1 commit 843491c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ PLATFORMS
arm64-darwin-23
x86_64-darwin-20
x86_64-darwin-22
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
8 changes: 5 additions & 3 deletions lib/inferno/dsl/fhir_resource_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def exclude_message(&block)
end

# @see Inferno::DSL::FHIRResourceValidation#resource_is_valid?
def resource_is_valid?(resource, profile_url, runnable)
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
Expand All @@ -173,8 +173,10 @@ def resource_is_valid?(resource, profile_url, runnable)

message_hashes = message_hashes_from_outcome(outcome, resource, profile_url)

message_hashes
.each { |message_hash| runnable.add_message(message_hash[:type], message_hash[:message]) }
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,
Expand Down
16 changes: 11 additions & 5 deletions lib/inferno/dsl/fhir_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ def self.included(klass)
# @param resource [FHIR::Model]
# @param profile_url [String]
# @param validator [Symbol] the name of the validator to use
# @param add_messages_to_runnable [Boolean] whether to add validation messages to runnable or not
# @return [Boolean] whether the resource is valid
def resource_is_valid?(resource: self.resource, profile_url: nil, validator: :default)
find_validator(validator).resource_is_valid?(resource, profile_url, self)
def resource_is_valid?(
resource: self.resource, profile_url: nil,
validator: :default, add_messages_to_runnable: true
)
find_validator(validator).resource_is_valid?(resource, profile_url, self, add_messages_to_runnable:)
end

# Find a particular validator. Looks through a runnable's parents up to
Expand Down Expand Up @@ -113,7 +117,7 @@ def exclude_message(&block)
end

# @see Inferno::DSL::FHIRValidation#resource_is_valid?
def resource_is_valid?(resource, profile_url, runnable)
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
Expand All @@ -128,8 +132,10 @@ def resource_is_valid?(resource, profile_url, runnable)

message_hashes = message_hashes_from_outcome(outcome, resource, profile_url)

message_hashes
.each { |message_hash| runnable.add_message(message_hash[:type], message_hash[:message]) }
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,
Expand Down
19 changes: 15 additions & 4 deletions spec/inferno/dsl/fhir_resource_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,22 @@
.to_return(status: 200, body: invalid_outcome)
end

it 'includes resourceType/id in error message' do
result = validator.resource_is_valid?(resource2, profile_url, runnable)
context 'when add_messages_to_runnable is set to true' do
it 'includes resourceType/id in error message' do
result = validator.resource_is_valid?(resource2, profile_url, runnable)

expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource2.resourceType}/#{resource2.id}:")
expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource2.resourceType}/#{resource2.id}:")
end
end

context 'when add_messages_to_runnable is set to false' do
it 'does not log messages' do
result = validator.resource_is_valid?(resource2, profile_url, runnable, add_messages_to_runnable: false)

expect(result).to be(false)
expect(runnable.messages).to be_empty
end
end
end

Expand Down
30 changes: 21 additions & 9 deletions spec/inferno/dsl/fhir_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,31 @@
.to_return(status: 200, body: invalid_outcome)
end

it 'includes resourceType/id in error message' do
result = validator.resource_is_valid?(resource, profile_url, runnable)
context 'when add_messages_to_runnable is set to true' do
it 'includes resourceType/id in error message' do
result = validator.resource_is_valid?(resource, profile_url, runnable)

expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource.resourceType}/#{resource.id}:")
expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource.resourceType}/#{resource.id}:")
end

it 'includes resourceType in error message if resource.id is nil' do
resource.id = nil
result = validator.resource_is_valid?(resource, profile_url, runnable)

expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource.resourceType}:")
end
end

it 'includes resourceType in error message if resource.id is nil' do
resource.id = nil
result = validator.resource_is_valid?(resource, profile_url, runnable)
context 'when add_messages_to_runnable is set to false' do
it 'does not log messages' do
resource.id = nil
result = validator.resource_is_valid?(resource, profile_url, runnable, add_messages_to_runnable: false)

expect(result).to be(false)
expect(runnable.messages.first[:message]).to start_with("#{resource.resourceType}:")
expect(result).to be(false)
expect(runnable.messages).to be_empty
end
end
end

Expand Down

0 comments on commit 843491c

Please sign in to comment.