Skip to content

Commit

Permalink
Fix validation of underscored names
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Apr 15, 2019
1 parent 52141ac commit 9d9d65c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/dry/initializer/dispatchers/build_nested_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Dry::Initializer::Dispatchers::BuildNestedType
# rubocop: disable Metrics/ParameterLists
def call(parent:, source:, target:, type: nil, block: nil, **options)
check_certainty!(source, type, block)
check_name!(target)
check_name!(target, block)
type ||= build_nested_type(parent, target, block)
{ parent: parent, source: source, target: target, type: type, **options }
end
Expand All @@ -22,16 +22,17 @@ def call(parent:, source:, target:, type: nil, block: nil, **options)
private

def check_certainty!(source, type, block)
return unless type
return unless block
return unless type

raise ArgumentError, <<~MESSAGE
You should define coercer of values of argument '#{source}'
either though the parameter/option, or via nested block, but not the both.
MESSAGE
end

def check_name!(name)
def check_name!(name, block)
return unless block
return unless name[/^_|__|_$/]

raise ArgumentError, <<~MESSAGE
Expand Down
4 changes: 2 additions & 2 deletions spec/type_constraint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
before do
class Test::Foo
extend Dry::Initializer
param :foo, proc(&:to_s), optional: true
param :__foo__, proc(&:to_s), optional: true
end
end

subject { Test::Foo.new :foo }

it "coerces a value" do
expect(subject.foo).to eq "foo"
expect(subject.__foo__).to eq "foo"
end
end

Expand Down

0 comments on commit 9d9d65c

Please sign in to comment.