Skip to content

Commit

Permalink
fix issue where min/max length validator would raise
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Apr 26, 2024
1 parent 21d3e0e commit 96a6922
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/typed_params/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def initialize(
format.key?(:without)
)

raise ArgumentError, 'length must be a hash with :minimum, :maximum, :within, :in, or :is keys (but not multiple)' unless
raise ArgumentError, 'length must be a hash with :minimum, :maximum, :within, :in, or :is keys (but not multiple except for :minimum and :maximum)' unless
length.nil? || length.is_a?(Hash) && (
length.key?(:minimum) ^
length.key?(:maximum) ^
length.key?(:within) ^
length.key?(:in) ^
length.key?(:is)
length.key?(:minimum) && length.key?(:maximum) && length.size == 2 ||
length.key?(:minimum) ^
length.key?(:maximum) ^
length.key?(:within) ^
length.key?(:in) ^
length.key?(:is)
)

@controller = controller
Expand Down
5 changes: 5 additions & 0 deletions spec/typed_params/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
end
end

it 'should not raise on multiple :length options' do
expect { TypedParams::Schema.new(type: :string, length: { minimum: 1, maximum: 42 }) }
.to_not raise_error
end

it 'should raise on multiple :length options' do
expect { TypedParams::Schema.new(type: :string, length: { in: 1..3, maximum: 42 }) }
.to raise_error ArgumentError
Expand Down

0 comments on commit 96a6922

Please sign in to comment.