Skip to content

Commit 9ca16d0

Browse files
committed
fix validation of nullable params when empty
1 parent 750259e commit 9ca16d0

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

lib/typed_params/validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module TypedParams
66
class Validator < Mapper
77
def call(params)
88
raise InvalidParameterError.new('is missing', path: schema.path, source: schema.source) if
9-
params.nil? && schema.required? && !schema.allow_nil?
9+
params.nil? && schema.required?
1010

1111
depth_first_map(params) do |param|
1212
schema = param.schema

spec/typed_params/validator_spec.rb

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@
5353
expect { validator.call(params) }.to_not raise_error
5454
end
5555

56+
it 'should raise on missing nested required params' do
57+
schema = TypedParams::Schema.new(type: :hash) do
58+
param :foo, type: :hash do
59+
param :bar, type: :hash do
60+
param :baz, type: :string
61+
end
62+
end
63+
end
64+
65+
params = TypedParams::Parameterizer.new(schema:).call(value: { foo: {} })
66+
validator = TypedParams::Validator.new(schema:)
67+
68+
expect { validator.call(params) }.to raise_error TypedParams::InvalidParameterError
69+
end
70+
5671
it 'should not raise on missing nested optional params' do
5772
schema = TypedParams::Schema.new(type: :hash) do
5873
param :foo, type: :hash do
@@ -68,6 +83,21 @@
6883
expect { validator.call(params) }.to_not raise_error
6984
end
7085

86+
it 'should raise on missing nested nilable params' do
87+
schema = TypedParams::Schema.new(type: :hash) do
88+
param :foo, type: :hash do
89+
param :bar, type: :hash, allow_nil: true do
90+
param :baz, type: :string
91+
end
92+
end
93+
end
94+
95+
params = TypedParams::Parameterizer.new(schema:).call(value: { foo: {} })
96+
validator = TypedParams::Validator.new(schema:)
97+
98+
expect { validator.call(params) }.to raise_error TypedParams::InvalidParameterError
99+
end
100+
71101
it 'should raise on missing required param' do
72102
schema = TypedParams::Schema.new(type: :hash) { param :foo, type: :string, optional: false }
73103
params = TypedParams::Parameterizer.new(schema:).call(value: {})
@@ -377,6 +407,14 @@
377407
expect { validator.call(params) }.to_not raise_error
378408
end
379409

410+
it 'should not raise on empty array' do
411+
schema = TypedParams::Schema.new(type: :array)
412+
params = TypedParams::Parameterizer.new(schema:).call(value: [])
413+
validator = TypedParams::Validator.new(schema:)
414+
415+
expect { validator.call(params) }.to_not raise_error
416+
end
417+
380418
it 'should raise on array of non-scalar values' do
381419
schema = TypedParams::Schema.new(type: :array)
382420
params = TypedParams::Parameterizer.new(schema:).call(value: [1, 2, [3]])
@@ -393,6 +431,32 @@
393431
expect { validator.call(params) }.to_not raise_error
394432
end
395433

434+
it 'should not raise on array of objects' do
435+
schema = TypedParams::Schema.new type: :array do
436+
items type: :hash do
437+
param :key, type: :string
438+
end
439+
end
440+
441+
params = TypedParams::Parameterizer.new(schema:).call(value: [key: 'value'])
442+
validator = TypedParams::Validator.new(schema:)
443+
444+
expect { validator.call(params) }.to_not raise_error
445+
end
446+
447+
it 'should not raise on empty array of objects' do
448+
schema = TypedParams::Schema.new type: :array do
449+
items type: :hash do
450+
param :key, type: :string
451+
end
452+
end
453+
454+
params = TypedParams::Parameterizer.new(schema:).call(value: [])
455+
validator = TypedParams::Validator.new(schema:)
456+
457+
expect { validator.call(params) }.to_not raise_error
458+
end
459+
396460
context 'with config to not ignore optional nils' do
397461
before do
398462
@ignore_nil_optionals_was = TypedParams.config.ignore_nil_optionals
@@ -521,4 +585,4 @@
521585
expect { validator.call(params) }.to raise_error TypedParams::InvalidParameterError
522586
end
523587
end
524-
end
588+
end

0 commit comments

Comments
 (0)