From 105f7d9bb3f94793fc856821c529ef8bc7ad4633 Mon Sep 17 00:00:00 2001 From: AnotherRegularDude Date: Tue, 15 Aug 2023 23:17:21 +0300 Subject: [PATCH] Add validations to the required fields --- lib/table_sync/publishing/batch.rb | 3 ++ lib/table_sync/publishing/raw.rb | 3 ++ lib/table_sync/utils.rb | 1 + lib/table_sync/utils/required_validator.rb | 43 ++++++++++++++++++++++ spec/publishing/batch_spec.rb | 3 ++ spec/publishing/raw_spec.rb | 4 ++ spec/support/shared/publishers.rb | 16 ++++++++ 7 files changed, 73 insertions(+) create mode 100644 lib/table_sync/utils/required_validator.rb diff --git a/lib/table_sync/publishing/batch.rb b/lib/table_sync/publishing/batch.rb index e67ee1c..2364407 100644 --- a/lib/table_sync/publishing/batch.rb +++ b/lib/table_sync/publishing/batch.rb @@ -2,6 +2,7 @@ class TableSync::Publishing::Batch include Tainbox + include TableSync::Utils::RequiredValidator attribute :object_class attribute :original_attributes @@ -11,6 +12,8 @@ class TableSync::Publishing::Batch attribute :event, default: :update + require_attributes :object_class, :original_attributes + def publish_later job.perform_later(job_attributes) end diff --git a/lib/table_sync/publishing/raw.rb b/lib/table_sync/publishing/raw.rb index 5aa09cd..7c0f2e1 100644 --- a/lib/table_sync/publishing/raw.rb +++ b/lib/table_sync/publishing/raw.rb @@ -2,6 +2,7 @@ class TableSync::Publishing::Raw include Tainbox + include TableSync::Utils::RequiredValidator attribute :model_name attribute :table_name @@ -13,6 +14,8 @@ class TableSync::Publishing::Raw attribute :event, default: :update + require_attributes :model_name, :original_attributes + def publish_now message.publish end diff --git a/lib/table_sync/utils.rb b/lib/table_sync/utils.rb index 2629fee..b8a016b 100644 --- a/lib/table_sync/utils.rb +++ b/lib/table_sync/utils.rb @@ -5,5 +5,6 @@ module Utils require_relative "utils/proc_array" require_relative "utils/proc_keywords_resolver" require_relative "utils/interface_checker" + require_relative "utils/required_validator" end end diff --git a/lib/table_sync/utils/required_validator.rb b/lib/table_sync/utils/required_validator.rb new file mode 100644 index 0000000..a66f785 --- /dev/null +++ b/lib/table_sync/utils/required_validator.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module TableSync::Utils::RequiredValidator + module PrependedInitialization + def initialize(*) + super + + not_filled_attrs = calculate_not_filled_attributes + if not_filled_attrs.present? + raise( + ArgumentError, + "Some of required attributes is not provided: #{not_filled_attrs.inspect}", + ) + end + end + end + + module ClassMethods + def require_attributes(*attributes) + _required_attributes.push(*attributes) + end + + def _required_attributes + @_required_attributes ||= [] + end + end + + module InstanceMethods + private + + def calculate_not_filled_attributes + attributes + .select { |key, value| key.in?(self.class._required_attributes) && value.blank? } + .keys + end + end + + def self.included(klass) + klass.prepend(PrependedInitialization) + klass.extend(ClassMethods) + klass.include(InstanceMethods) + end +end diff --git a/spec/publishing/batch_spec.rb b/spec/publishing/batch_spec.rb index 5d9a4ea..3380c82 100644 --- a/spec/publishing/batch_spec.rb +++ b/spec/publishing/batch_spec.rb @@ -24,6 +24,9 @@ include_examples "publisher#publish_now with stubbed message", TableSync::Publishing::Message::Batch + include_examples "publisher#new without expected fields", + TableSync::Publishing::Batch, + %i[object_class original_attributes] context "real user" do context "sequel" do diff --git a/spec/publishing/raw_spec.rb b/spec/publishing/raw_spec.rb index 2509363..5093c09 100644 --- a/spec/publishing/raw_spec.rb +++ b/spec/publishing/raw_spec.rb @@ -36,4 +36,8 @@ include_examples "publisher#publish_now without stubbed message", TableSync::Publishing::Message::Raw end + + include_examples "publisher#new without expected fields", + TableSync::Publishing::Raw, + %i[model_name original_attributes] end diff --git a/spec/support/shared/publishers.rb b/spec/support/shared/publishers.rb index 8452c37..e36c004 100644 --- a/spec/support/shared/publishers.rb +++ b/spec/support/shared/publishers.rb @@ -54,6 +54,22 @@ end end +# needs let(:attributes) +shared_examples "publisher#new without expected fields" do |publisher_class, required_attributes| + required_attributes.each do |attribute| + context "without #{attribute}" do + it "raises an error" do + expect { publisher_class.new(attributes.except(attribute)) }.to raise_error do |error| + expect(error).to be_an_instance_of(ArgumentError) + expect(error.message).to eq( + "Some of required attributes is not provided: [:#{attribute}]", + ) + end + end + end + end +end + # needs let(:existing_user) shared_examples "publisher#publish_now with real user, for given orm" do |orm| let(:user) { DB[:users].where(id: existing_user.id).first }