From 843491c1810f6401d83a831075b8b0e7b0bb9609 Mon Sep 17 00:00:00 2001 From: Vanessa Fotso <46642178+vanessuniq@users.noreply.github.com> Date: Wed, 22 Jan 2025 10:39:44 -0500 Subject: [PATCH] FI-3178: Validate Resource Without Validation Messages (#595) * update resource_is_valid method to conditionally loggin messages to runnable Signed-off-by: Vanessa Fotso * update spec Signed-off-by: Vanessa Fotso * update doc Signed-off-by: Vanessa Fotso * update param name Signed-off-by: Vanessa Fotso --------- Signed-off-by: Vanessa Fotso --- Gemfile.lock | 1 + lib/inferno/dsl/fhir_resource_validation.rb | 8 +++-- lib/inferno/dsl/fhir_validation.rb | 16 ++++++---- .../dsl/fhir_resource_validation_spec.rb | 19 +++++++++--- spec/inferno/dsl/fhir_validation_spec.rb | 30 +++++++++++++------ 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 88cbc9e75..77924be63 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -332,6 +332,7 @@ PLATFORMS arm64-darwin-23 x86_64-darwin-20 x86_64-darwin-22 + x86_64-darwin-23 x86_64-linux DEPENDENCIES diff --git a/lib/inferno/dsl/fhir_resource_validation.rb b/lib/inferno/dsl/fhir_resource_validation.rb index ee97e4030..794022889 100644 --- a/lib/inferno/dsl/fhir_resource_validation.rb +++ b/lib/inferno/dsl/fhir_resource_validation.rb @@ -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 @@ -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, diff --git a/lib/inferno/dsl/fhir_validation.rb b/lib/inferno/dsl/fhir_validation.rb index 998777f02..78fe34795 100644 --- a/lib/inferno/dsl/fhir_validation.rb +++ b/lib/inferno/dsl/fhir_validation.rb @@ -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 @@ -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 @@ -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, diff --git a/spec/inferno/dsl/fhir_resource_validation_spec.rb b/spec/inferno/dsl/fhir_resource_validation_spec.rb index 216ca11cb..8ac2ad28a 100644 --- a/spec/inferno/dsl/fhir_resource_validation_spec.rb +++ b/spec/inferno/dsl/fhir_resource_validation_spec.rb @@ -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 diff --git a/spec/inferno/dsl/fhir_validation_spec.rb b/spec/inferno/dsl/fhir_validation_spec.rb index 94c5a3784..c790fe200 100644 --- a/spec/inferno/dsl/fhir_validation_spec.rb +++ b/spec/inferno/dsl/fhir_validation_spec.rb @@ -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