Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10-10CG] Add ability to not call fully_validate_schema in SavedClaim #19468

Merged
merged 7 commits into from
Nov 18, 2024
Prev Previous commit
Next Next commit
add logic to skip fully_validate_schema when toggle is turned on
coope93 committed Nov 14, 2024
commit 852ca8ff1ef2adba33c4a5ac518451f0943d54f3
12 changes: 7 additions & 5 deletions app/models/saved_claim.rb
Original file line number Diff line number Diff line change
@@ -82,13 +82,15 @@ def form_matches_schema
return unless form_is_string

schema = VetsJsonSchema::SCHEMAS[self.class::FORM]
clear_cache = false

schema_errors = validate_schema(schema)
unless Flipper.enabled?(:saved_claim_schema_validation_disable)
schema_errors = validate_schema(schema)

clear_cache = false
unless schema_errors.empty?
Rails.logger.error('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
clear_cache = true
unless schema_errors.empty?
Rails.logger.error('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
clear_cache = true
end
end

validation_errors = validate_form(schema, clear_cache)
132 changes: 92 additions & 40 deletions spec/models/saved_claim_spec.rb
Original file line number Diff line number Diff line change
@@ -44,69 +44,121 @@ def attachment_keys
end

context 'validation errors' do
let(:schema_errors) { [{ fragment: 'error' }] }
context 'saved_claim_schema_validation_disable disabled' do
let(:schema_errors) { [{ fragment: 'error' }] }

context 'when fully_validate_schema returns errors' do
before do
allow(JSON::Validator).to receive_messages(fully_validate_schema: schema_errors, fully_validate: [])
allow(Flipper).to receive(:enabled?).with(:saved_claim_schema_validation_disable).and_return(false)
end

it 'logs schema failed error and calls fully_validate' do
expect(Rails.logger).to receive(:error)
.with('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
context 'when fully_validate_schema returns errors' do
before do
allow(JSON::Validator).to receive_messages(fully_validate_schema: schema_errors, fully_validate: [])
end

expect(saved_claim.validate).to eq true
end
end
it 'logs schema failed error and calls fully_validate' do
expect(Rails.logger).to receive(:error)
.with('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })

context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
expect(saved_claim.validate).to eq true
end
end

it 'adds validation errors to the form' do
saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
end

it 'adds validation errors to the form' do
saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
end
end
end

context 'when JSON:Validator.fully_validate_schema throws an exception' do
let(:exception) { StandardError.new('Some exception') }
context 'when JSON:Validator.fully_validate_schema throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_raise(exception)
allow(JSON::Validator).to receive(:fully_validate).and_return([])
before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_raise(exception)
allow(JSON::Validator).to receive(:fully_validate).and_return([])
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during schema validation!', { error: exception.message, backtrace: anything, schema: })

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during schema validation!', { error: exception.message, backtrace: anything, schema: })
context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_return([])
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end
end

context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }
context 'saved_claim_schema_validation_disable enabled' do
let(:schema_errors) { [{ fragment: 'error' }] }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_return([])
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
allow(Flipper).to receive(:enabled?).with(:saved_claim_schema_validation_disable).and_return(true)
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })
context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
end

expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)
it 'adds validation errors to the form' do
expect(JSON::Validator).not_to receive(:fully_validate_schema)

saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
end
end

context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
end

it 'logs exception and raises exception' do
expect(JSON::Validator).not_to receive(:fully_validate_schema)

expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })

expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end
end
end