diff --git a/CHANGELOG.md b/CHANGELOG.md index b666f6d5b8..f1000c1a4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 55e9bd2c1f..ef738c9fff 100644 --- a/README.md +++ b/README.md @@ -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 } @@ -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 diff --git a/lib/grape/locale/en.yml b/lib/grape/locale/en.yml index 39c446b65d..6b1b6ae06f 100644 --- a/lib/grape/locale/en.yml +++ b/lib/grape/locale/en.yml @@ -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: diff --git a/lib/grape/validations/validators/length_validator.rb b/lib/grape/validations/validators/length_validator.rb index 899134cb33..f844f047ec 100644 --- a/lib/grape/validations/validators/length_validator.rb +++ b/lib/grape/validations/validators/length_validator.rb @@ -7,7 +7,7 @@ class LengthValidator < Base def initialize(attrs, options, required, scope, **opts) @min = options[:min] @max = options[:max] - @exact = options[:exact] + @is = options[:is] super @@ -15,9 +15,9 @@ def initialize(attrs, options, required, scope, **opts) 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) @@ -25,7 +25,7 @@ def validate_param!(attr_name, params) 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 @@ -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 diff --git a/spec/grape/validations/validators/length_spec.rb b/spec/grape/validations/validators/length_spec.rb index bcb6348c4d..7e85b4dd84 100644 --- a/spec/grape/validations/validators/length_spec.rb +++ b/spec/grape/validations/validators/length_spec.rb @@ -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 @@ -317,10 +317,10 @@ 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 @@ -328,7 +328,7 @@ 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 @@ -336,7 +336,7 @@ 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 @@ -344,25 +344,25 @@ 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