Skip to content

Commit

Permalink
Rename :exact param by :is
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakad committed Jul 30, 2024
1 parent 5e5d7bc commit 562d90e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* [#2478](https://github.com/ruby-grape/grape/pull/2478): Fix rescue_from with invalid response - [@ericproulx](https://github.com/ericproulx).
* [#2480](https://github.com/ruby-grape/grape/pull/2480): Fix rescue_from ValidationErrors exception - [@numbata](https://github.com/numbata).
* [#2464](https://github.com/ruby-grape/grape/pull/2464): The `length` validator only takes effect for parameters with types that support `#length` method - [@OuYangJinTing](https://github.com/OuYangJinTing).
* [#2485](https://github.com/ruby-grape/grape/pull/2485): Add exact param to length validator - [@dakad](https://github.com/dakad).
* [#2485](https://github.com/ruby-grape/grape/pull/2485): Add `is:` param to length validator - [@dakad](https://github.com/dakad).
* Your contribution here.

### 2.1.3 (2024-07-13)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1714,11 +1714,11 @@ end

Parameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.

The validator accepts `:min` or `:max` or both options or only `:exact` to validate that the value of the parameter is within the given limits.
The validator accepts `:min` or `:max` or both options or only `:is` to validate that the value of the parameter is within the given limits.

```ruby
params do
requires :code, type: String, length: { exact: 2 }
requires :code, type: String, length: { is: 2 }
requires :str, type: String, length: { min: 3 }
requires :list, type: [Integer], length: { min: 3, max: 5 }
requires :hash, type: Hash, length: { max: 5 }
Expand Down Expand Up @@ -2046,7 +2046,7 @@ end

```ruby
params do
requires :code, type: String, length: { exact: 2, message: 'code is expected to be exactly 2 characters long' }
requires :code, type: String, length: { is: 2, message: 'code is expected to be exactly 2 characters long' }
requires :str, type: String, length: { min: 5, message: 'str is expected to be atleast 5 characters long' }
requires :list, type: [Integer], length: { min: 2, max: 3, message: 'list is expected to have between 2 and 3 elements' }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ en:
except_values: 'has a value not allowed'
same_as: 'is not the same as %{parameter}'
length: 'is expected to have length within %{min} and %{max}'
length_exact: 'is expected to have length exactly equal to %{exact}'
length_is: 'is expected to have length exactly equal to %{is}'
length_min: 'is expected to have length greater than or equal to %{min}'
length_max: 'is expected to have length less than or equal to %{max}'
missing_vendor_option:
Expand Down
12 changes: 6 additions & 6 deletions lib/grape/validations/validators/length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ class LengthValidator < Base
def initialize(attrs, options, required, scope, **opts)
@min = options[:min]
@max = options[:max]
@exact = options[:exact]
@is = options[:is]

super

raise ArgumentError, 'min must be an integer greater than or equal to zero' if !@min.nil? && (!@min.is_a?(Integer) || @min.negative?)
raise ArgumentError, 'max must be an integer greater than or equal to zero' if !@max.nil? && (!@max.is_a?(Integer) || @max.negative?)
raise ArgumentError, "min #{@min} cannot be greater than max #{@max}" if !@min.nil? && !@max.nil? && @min > @max

return if @exact.nil?
raise ArgumentError, 'exact must be an integer greater than zero' if !@exact.is_a?(Integer) || !@exact.positive?
raise ArgumentError, 'exact cannot be combined with min or max' if !@min.nil? || !@max.nil?
return if @is.nil?
raise ArgumentError, 'is must be an integer greater than zero' if !@is.is_a?(Integer) || !@is.positive?
raise ArgumentError, 'is cannot be combined with min or max' if !@min.nil? || !@max.nil?
end

def validate_param!(attr_name, params)
param = params[attr_name]

return unless param.respond_to?(:length)

return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max) || (!@exact.nil? && param.length != @exact)
return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max) || (!@is.nil? && param.length != @is)

raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: build_message)
end
Expand All @@ -40,7 +40,7 @@ def build_message
elsif @max
format I18n.t(:length_max, scope: 'grape.errors.messages'), max: @max
else
format I18n.t(:length_exact, scope: 'grape.errors.messages'), exact: @exact
format I18n.t(:length_is, scope: 'grape.errors.messages'), is: @is
end
end
end
Expand Down
34 changes: 17 additions & 17 deletions spec/grape/validations/validators/length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@
end

params do
requires :code, length: { exact: 2 }
requires :code, length: { is: 2 }
end
post 'exact' do
post 'is' do
end

params do
requires :code, length: { exact: -2 }
requires :code, length: { is: -2 }
end
post 'negative_exact' do
post 'negative_is' do
end

params do
requires :code, length: { exact: 2, max: 10 }
requires :code, length: { is: 2, max: 10 }
end
post 'exact_with_max' do
post 'is_with_max' do
end
end
end
Expand Down Expand Up @@ -317,52 +317,52 @@
end
end

describe '/exact' do
describe '/is' do
context 'when length is exact' do
it do
post 'exact', code: 'ZZ'
post 'is', code: 'ZZ'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('')
end
end

context 'when length exceeds the limit' do
it do
post 'exact', code: 'aze'
post 'is', code: 'aze'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('code is expected to have length exactly equal to 2')
end
end

context 'when length is less than the limit' do
it do
post 'exact', code: 'a'
post 'is', code: 'a'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('code is expected to have length exactly equal to 2')
end
end

context 'when length is zero' do
it do
post 'exact', code: ''
post 'is', code: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('code is expected to have length exactly equal to 2')
end
end
end

describe '/negative_exact' do
context 'when exact is negative' do
describe '/negative_is' do
context 'when `is` is negative' do
it do
expect { post 'negative_exact', code: 'ZZ' }.to raise_error(ArgumentError, 'exact must be an integer greater than zero')
expect { post 'negative_is', code: 'ZZ' }.to raise_error(ArgumentError, 'is must be an integer greater than zero')
end
end
end

describe '/exact_with_max' do
context 'when exact is combined with max' do
describe '/is_with_max' do
context 'when `is` is combined with max' do
it do
expect { post 'exact_with_max', code: 'ZZ' }.to raise_error(ArgumentError, 'exact cannot be combined with min or max')
expect { post 'is_with_max', code: 'ZZ' }.to raise_error(ArgumentError, 'is cannot be combined with min or max')
end
end
end
Expand Down

0 comments on commit 562d90e

Please sign in to comment.