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

Add required fields validation #80

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [6.4.1] - 2023-08-15
### Added
- Required fields validation for the Raw and Batch publishers.

## [6.4.0] - 2023-08-15
### Changed
- Fix sort in receiving (#79).
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
table_sync (6.4.0)
table_sync (6.4.1)
memery
rabbit_messaging (~> 0.13)
rails
Expand Down
3 changes: 3 additions & 0 deletions lib/table_sync/publishing/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TableSync::Publishing::Batch
include Tainbox
include TableSync::Utils::RequiredValidator

attribute :object_class
attribute :original_attributes
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/table_sync/publishing/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TableSync::Publishing::Raw
include Tainbox
include TableSync::Utils::RequiredValidator

attribute :model_name
attribute :table_name
Expand All @@ -13,6 +14,8 @@ class TableSync::Publishing::Raw

attribute :event, default: :update

require_attributes :model_name, :original_attributes

def publish_now
message.publish
end
Expand Down
1 change: 1 addition & 0 deletions lib/table_sync/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions lib/table_sync/utils/required_validator.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/table_sync/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TableSync
VERSION = "6.4.0"
VERSION = "6.4.1"
end
3 changes: 3 additions & 0 deletions spec/publishing/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/publishing/raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/support/shared/publishers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading