Skip to content

Commit

Permalink
Merge pull request #348 from dry-rb/add-optional-params
Browse files Browse the repository at this point in the history
Add Types::Optional::Params
  • Loading branch information
flash-gordon committed Aug 9, 2019
2 parents aec32db + 3b63a45 commit 4bdde9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/dry/types/params.rb
Expand Up @@ -55,5 +55,10 @@ module Types
register('params.symbol') do
self['nominal.symbol'].constructor(Coercions::Params.method(:to_symbol))
end

COERCIBLE.each_key do |name|
next if name.equal?(:string)
register("optional.params.#{name}", self['params.nil'] | self["params.#{name}"])
end
end
end
16 changes: 16 additions & 0 deletions spec/dry/types/types/params_spec.rb
Expand Up @@ -219,4 +219,20 @@
expect(type['a']).to eql(:a)
end
end

context 'optional types' do
subject(:type) { Dry::Types['optional.params.integer'] }

it 'coerces empty strings to nil' do
expect(type['']).to be_nil
end

it 'parses integers' do
expect(type['40']).to be(40)
end

it 'raises an error on random strings' do
expect { type['abc'] }.to raise_error(Dry::Types::CoercionError)
end
end
end

0 comments on commit 4bdde9c

Please sign in to comment.