-
-
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.
Added param validator for an allowed list of values.
- Loading branch information
1 parent
b0123a4
commit e7c44e0
Showing
8 changed files
with
120 additions
and
1 deletion.
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
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,13 @@ | ||
# encoding: utf-8 | ||
module Grape | ||
module Exceptions | ||
class IncompatibleOptionValues < Base | ||
|
||
def initialize(option1, value1, option2, value2) | ||
super(message: compose_message("incompatible_option_values", option1: option1, value1: value1, option2: option2, value2: value2)) | ||
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
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,16 @@ | ||
module Grape | ||
module Validations | ||
class ValuesValidator < Validator | ||
def initialize(attrs, options, required, scope) | ||
@values = options | ||
super | ||
end | ||
|
||
def validate_param!(attr_name, params) | ||
if params[attr_name] && (! @values.include? params[attr_name]) | ||
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message_key: :values | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations::ValuesValidator do | ||
|
||
module ValidationsSpec | ||
module ValuesValidatorSpec | ||
class API < Grape::API | ||
default_format :json | ||
|
||
params do | ||
requires :type, values: ['valid-type1', 'valid-type2', 'valid-type3'] | ||
end | ||
get '/' do | ||
{ type: params[:type] } | ||
end | ||
|
||
params do | ||
optional :type, values: ['valid-type1', 'valid-type2', 'valid-type3'], default: 'valid-type2' | ||
end | ||
get '/default/valid' do | ||
{ type: params[:type] } | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
def app | ||
ValidationsSpec::ValuesValidatorSpec::API | ||
end | ||
|
||
it 'allows a valid value for a parameter' do | ||
get("/", type: 'valid-type1') | ||
last_response.status.should eq 200 | ||
last_response.body.should eq({ type: "valid-type1" }.to_json) | ||
end | ||
|
||
it 'does not allow an invalid value for a parameter' do | ||
get("/", type: 'invalid-type') | ||
last_response.status.should eq 400 | ||
last_response.body.should eq({ error: "type does not have a valid value" }.to_json) | ||
end | ||
|
||
it 'allows a valid default value' do | ||
get("/default/valid") | ||
last_response.status.should eq 200 | ||
last_response.body.should eq({ type: "valid-type2" }.to_json) | ||
end | ||
|
||
it 'raises IncompatibleOptionValues on an invalid default value' do | ||
subject = Class.new(Grape::API) | ||
expect { | ||
subject.params { optional :type, values: ['valid-type1', 'valid-type2', 'valid-type3'], default: 'invalid-type' } | ||
}.to raise_error Grape::Exceptions::IncompatibleOptionValues | ||
end | ||
|
||
it 'raises IncompatibleOptionValues when type is incompatible with values array' do | ||
subject = Class.new(Grape::API) | ||
expect { | ||
subject.params { optional :type, values: ['valid-type1', 'valid-type2', 'valid-type3'], type: Symbol } | ||
}.to raise_error Grape::Exceptions::IncompatibleOptionValues | ||
end | ||
|
||
end |