8000 Fixing Error Received When First Validator is a Custom Presence Validator by knittingdev · Pull Request #12 · rewinfrey/ActionLogic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixing Error Received When First Validator is a Custom Presence Validator #12

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

Merged
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
2 changes: 1 addition & 1 deletion lib/action_logic/action_validation/type_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.validate!(validation_rules, context)
return unless validation_rules.values.find { |expected_validation| expected_validation[:type] }

type_errors = validation_rules.reduce([]) do |collection, (expected_attribute, expected_validation)|
next unless expected_validation[:type]
next collection unless expected_validation[:type]

if context.to_h[expected_attribute].class != expected_validation[:type]
collection << "Attribute: #{expected_attribute} with value: #{context.to_h[expected_attribute]} was expected to be of type #{expected_validation[:type]} but is #{context.to_h[expected_attribute].class}"
Expand Down
21 changes: 21 additions & 0 deletions spec/action_logic/active_use_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ module ActionLogic
raise_error(ActionLogic::UnrecognizablePresenceValidatorError)
end
end

describe "mixed custom presence and type" do
it "allows custom presence validation to be defined without type if a type validation is defined" do
expect { ValidateBeforeMixedTypeAndPresenceUseCase.execute(odd_integer_test: 1, string_test: "Test") }.to_not raise_error
end

it "raises error if custom presence validation is not satisfied" do
expect { ValidateBeforeMixedTypeAndPresenceUseCase.execute(odd_integer_test: 2, string_test: "Test") }.to \
raise_error(ActionLogic::PresenceError)
end

it "raises error if type validation is not satisfied" do
expect { ValidateBeforeMixedTypeAndPresenceUseCase.execute(odd_integer_test: 1, string_test: 15) }.to \
raise_error(ActionLogic::AttributeTypeError)
end

it "raises error if type presence validation is not satisfied" do
expect { ValidateBeforeMixedTypeAndPresenceUseCase.execute(odd_integer_test: 1) }.to \
raise_error(ActionLogic::MissingAttributeError)
end
end
end

describe "after validations" do
Expand Down
15 changes: 15 additions & 0 deletions spec/fixtures/use_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ def tasks
end
end

class ValidateBeforeMixedTypeAndPresenceUseCase
include ActionLogic::ActionUseCase

validates_before odd_integer_test: { presence: ->(i) { i % 2 != 0 } },
string_test: { type: String, presence: true }

def call
end

def tasks
[UseCaseTestTask1,
UseCaseTestTask2]
end
end

class ValidateAfterTestUseCase
include ActionLogic::ActionUseCase

Expand Down
0