Skip to content

Commit 9d9d65c

Browse files
committed
Fix validation of underscored names
1 parent 52141ac commit 9d9d65c

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/dry/initializer/dispatchers/build_nested_type.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Dry::Initializer::Dispatchers::BuildNestedType
1313
# rubocop: disable Metrics/ParameterLists
1414
def call(parent:, source:, target:, type: nil, block: nil, **options)
1515
check_certainty!(source, type, block)
16-
check_name!(target)
16+
check_name!(target, block)
1717
type ||= build_nested_type(parent, target, block)
1818
{ parent: parent, source: source, target: target, type: type, **options }
1919
end
@@ -22,16 +22,17 @@ def call(parent:, source:, target:, type: nil, block: nil, **options)
2222
private
2323

2424
def check_certainty!(source, type, block)
25-
return unless type
2625
return unless block
26+
return unless type
2727

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

34-
def check_name!(name)
34+
def check_name!(name, block)
35+
return unless block
3536
return unless name[/^_|__|_$/]
3637

3738
raise ArgumentError, <<~MESSAGE

spec/type_constraint_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
before do
66
class Test::Foo
77
extend Dry::Initializer
8-
param :foo, proc(&:to_s), optional: true
8+
param :__foo__, proc(&:to_s), optional: true
99
end
1010
end
1111

1212
subject { Test::Foo.new :foo }
1313

1414
it "coerces a value" do
15-
expect(subject.foo).to eq "foo"
15+
expect(subject.__foo__).to eq "foo"
1616
end
1717
end
1818

0 commit comments

Comments
 (0)