Skip to content

Commit f071f69

Browse files
committed
add support for any type
1 parent 80b74b7 commit f071f69

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

lib/typed_params.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require 'typed_params/transforms/transform'
2727
require 'typed_params/transformer'
2828
require 'typed_params/types'
29+
require 'typed_params/types/any'
2930
require 'typed_params/types/array'
3031
require 'typed_params/types/boolean'
3132
require 'typed_params/types/date'

lib/typed_params/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ def if? = [email protected]?
281281
def unless? = !@unless.nil?
282282
def array? = Types.array?(type)
283283
def hash? = Types.hash?(type)
284-
def scalar? = Types.scalar?(type)
284+
def scalar? = type.scalar?
285+
def any? = type.any?
285286
def formatter? = !!@formatter
286287

287288
def inspect

lib/typed_params/types/any.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module TypedParams
4+
module Types
5+
register(:any,
6+
abstract: true,
7+
match: -> _ {},
8+
)
9+
end
10+
end

lib/typed_params/types/type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def archetype
4141
def accepts_block? = !!@accepts_block
4242
def coercable? = !!@coerce
4343
def scalar? = !!@scalar
44+
def any? = @type == :any
4445
def abstract? = !!@abstract
4546
def subtype? = !!@archetype
4647

lib/typed_params/validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def call(params)
2828

2929
# Assert type
3030
raise InvalidParameterError.new("type mismatch (received #{type.humanize} expected #{schema.type.humanize})", path: param.path, source: schema.source) unless
31-
type == schema.type || type.subtype? && type.archetype == schema.type
31+
schema.any? || type == schema.type || type.subtype? && type.archetype == schema.type
3232

3333
# Assertions for params without children
3434
if schema.children.nil?

spec/typed_params/validator_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@
2929
expect { validator.call(params) }.to raise_error TypedParams::InvalidParameterError
3030
end
3131

32+
it 'should not raise for any type' do
33+
schema = TypedParams::Schema.new(type: :hash) do
34+
param :foo, type: :hash do
35+
param :bar, type: :any
36+
end
37+
end
38+
39+
values = [1, '2', :'3', { four: 4 }, [5, '6', :'7', { eight: 8 }, [9]], true, false]
40+
41+
values.each do |value|
42+
params = TypedParams::Parameterizer.new(schema:).call(value: { foo: { bar: value } })
43+
validator = TypedParams::Validator.new(schema:)
44+
45+
expect { validator.call(params) }.to_not raise_error TypedParams::InvalidParameterError
46+
end
47+
end
48+
49+
it 'should raise for nil any type' do
50+
params = TypedParams::Parameterizer.new(schema:).call(value: { foo: { bar: nil } })
51+
validator = TypedParams::Validator.new(schema:)
52+
53+
expect { validator.call(params) }.to raise_error TypedParams::InvalidParameterError
54+
end
55+
3256
it 'should not raise on missing optional root' do
3357
schema = TypedParams::Schema.new(type: :hash, optional: true)
3458
params = TypedParams::Parameterizer.new(schema:).call(value: nil)

0 commit comments

Comments
 (0)