-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This validator validates that the length of the parameter (if the parameter supports `#length` is within the specified limits).
- Loading branch information
Showing
5 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Validations | ||
module Validators | ||
class LengthValidator < Base | ||
def initialize(attrs, options, required, scope, **opts) | ||
@min_length = options[:min] | ||
@max_length = options[:max] | ||
super | ||
end | ||
|
||
def validate_param!(attr_name, params) | ||
param = params[attr_name] | ||
return unless param.respond_to?(:length) | ||
return unless (@min_length && param.length < @min_length) || (@max_length && param.length > @max_length) | ||
|
||
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: build_message) | ||
end | ||
|
||
def build_message | ||
if options_key?(:message) | ||
@option[:message] | ||
elsif @min_length && @max_length | ||
format I18n.t(:length_mismatch_both, scope: 'grape.errors.messages'), min_length: @min_length, max_length: @max_length | ||
elsif @min_length | ||
format I18n.t(:length_mismatch_min_only, scope: 'grape.errors.messages'), min_length: @min_length | ||
else | ||
format I18n.t(:length_mismatch_max_only, scope: 'grape.errors.messages'), max_length: @max_length | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Grape::Validations::Validators::LengthValidator do | ||
let_it_be(:app) do | ||
Class.new(Grape::API) do | ||
params do | ||
requires :list, length: { min: 2, max: 3 } | ||
end | ||
post 'with_min_max' do | ||
end | ||
|
||
params do | ||
requires :list, type: [Integer], length: { min: 2 } | ||
end | ||
post 'with_min_only' do | ||
end | ||
|
||
params do | ||
requires :list, type: [Integer], length: { max: 3 } | ||
end | ||
post 'with_max_only' do | ||
end | ||
|
||
params do | ||
requires :list, type: Integer, length: { max: 3 } | ||
end | ||
post 'type_is_not_array' do | ||
end | ||
|
||
params do | ||
requires :list, type: [Integer], length: { min: 2, message: 'not match' } | ||
end | ||
post '/custom-message' do | ||
end | ||
end | ||
end | ||
|
||
describe '/with_min_max' do | ||
context 'when length is within limits' do | ||
it do | ||
post '/with_min_max', list: [1, 2] | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq('') | ||
end | ||
end | ||
|
||
context 'when length is exceeded' do | ||
it do | ||
post '/with_min_max', list: [1, 2, 3, 4, 5] | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to eq('list is expected to have a length within 2 and 3') | ||
end | ||
end | ||
|
||
context 'when length is less than minimum' do | ||
it do | ||
post '/with_min_max', list: [1] | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to eq('list is expected to have a length within 2 and 3') | ||
end | ||
end | ||
end | ||
|
||
describe '/with_max_only' do | ||
context 'when length is less than limits' do | ||
it do | ||
post '/with_max_only', list: [1, 2] | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq('') | ||
end | ||
end | ||
|
||
context 'when length is exceeded' do | ||
it do | ||
post '/with_max_only', list: [1, 2, 3, 4, 5] | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to eq('list is expected to have a length less than 3') | ||
end | ||
end | ||
end | ||
|
||
describe '/with_min_only' do | ||
context 'when length is greater than limit' do | ||
it do | ||
post '/with_min_only', list: [1, 2] | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq('') | ||
end | ||
end | ||
|
||
context 'when length is less than limit' do | ||
it do | ||
post '/with_min_only', list: [1] | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to eq('list is expected to have a length greater than 2') | ||
end | ||
end | ||
end | ||
|
||
describe '/type_is_not_array' do | ||
context 'is no op' do | ||
it do | ||
post 'type_is_not_array', list: 12 | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq('') | ||
end | ||
end | ||
end | ||
|
||
describe '/custom-message' do | ||
context 'is within limits' do | ||
it do | ||
post '/custom-message', list: [1, 2, 3] | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq('') | ||
end | ||
end | ||
|
||
context 'is outside limit' do | ||
it do | ||
post '/custom-message', list: [1] | ||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to eq('list not match') | ||
end | ||
end | ||
end | ||
end |