forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
218 additions
and
0 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
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,118 @@ | ||
require 'virtus' | ||
Boolean = Virtus::Attribute::Boolean | ||
module Grape | ||
|
||
class Validator | ||
def initialize(attrs, options) | ||
@attrs = Array(attrs) | ||
|
||
if options.is_a?(Hash) && !options.empty? | ||
raise "unknown options: #{options.keys}" | ||
end | ||
end | ||
|
||
def validate!(params) | ||
@attrs.each do |attr_name| | ||
validate_param!(attr_name, params) | ||
end | ||
end | ||
end | ||
|
||
|
||
class SingleOptionValidator < Validator | ||
def initialize(attrs, options) | ||
@option = options | ||
super | ||
end | ||
|
||
end | ||
|
||
|
||
class PresenceValidator < Validator | ||
def validate_param!(attr_name, params) | ||
unless params.has_key?(attr_name) | ||
throw :error, :status => 400, :message => "missing parameter: #{attr_name}" | ||
end | ||
end | ||
|
||
end | ||
|
||
class CoerceValidator < SingleOptionValidator | ||
def validate_param!(attr_name, params) | ||
params[attr_name] = coerce_value(@option, params[attr_name]) | ||
end | ||
|
||
private | ||
def coerce_value(type, val) | ||
converter = Virtus::Attribute.build(:a, type) | ||
converter.coerce(val) | ||
end | ||
end | ||
|
||
class RegExpValidator < SingleOptionValidator | ||
def validate_param!(attr_name, params) | ||
if params[attr_name] && !( params[attr_name].to_s =~ @option ) | ||
throw :error, :status => 400, :message => "invalid parameter: #{attr_name}" | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
module Validations | ||
|
||
class <<self | ||
attr_accessor :validators | ||
end | ||
|
||
self.validators = {} | ||
self.validators[:presence] = PresenceValidator | ||
self.validators[:regexp] = RegExpValidator | ||
self.validators[:coerce] = CoerceValidator | ||
|
||
def self.included(klass) | ||
klass.instance_eval do | ||
extend ClassMethods | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def reset_validations! | ||
settings[:validations] = [] | ||
end | ||
|
||
def requires(*attrs) | ||
validations = {:presence => true} | ||
if attrs.last.is_a?(Hash) | ||
validations.merge!(attrs.pop) | ||
end | ||
|
||
validates(attrs, validations) | ||
end | ||
|
||
def optional(*attrs) | ||
validations = {} | ||
if attrs.last.is_a?(Hash) | ||
validations.merge!(attrs.pop) | ||
end | ||
|
||
validates(attrs, validations) | ||
end | ||
|
||
def validates(attrs, validations) | ||
validations.each do |type, options| | ||
validator_class = Validations::validators[type] | ||
if validator_class | ||
settings[:validations] << validator_class.new(attrs, options) | ||
else | ||
raise "unknown validator: #{type}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations do | ||
def app; @app; end | ||
|
||
before do | ||
@app = Class.new(Grape::API) do | ||
default_format :json | ||
|
||
requires :id, :regexp => /^[0-9]+$/ | ||
post do | ||
{:ret => params[:id]} | ||
end | ||
|
||
requires :name, :company | ||
optional :a_number, :regexp => /^[0-9]+$/ | ||
get do | ||
"Hello" | ||
end | ||
|
||
requires :int, :coerce => Integer | ||
optional :arr, :coerce => Array[Integer] | ||
optional :bool, :coerce => Array[Boolean] | ||
get '/coerce' do | ||
{ | ||
:int => params[:int].class, | ||
:arr => params[:arr] ? params[:arr][0].class : nil, | ||
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil | ||
} | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
it 'validates id' do | ||
post('/') | ||
last_response.status.should == 400 | ||
last_response.body.should == "missing parameter: id" | ||
|
||
post('/', {}, 'rack.input' => StringIO.new('{"id" : "a56b"}')) | ||
last_response.body.should == 'invalid parameter: id' | ||
last_response.status.should == 400 | ||
|
||
post('/', {}, 'rack.input' => StringIO.new('{"id" : 56}')) | ||
last_response.body.should == '{"ret":56}' | ||
last_response.status.should == 201 | ||
end | ||
|
||
it 'validates name, company' do | ||
get('/') | ||
last_response.status.should == 400 | ||
last_response.body.should == "missing parameter: name" | ||
|
||
get('/', :name => "Bob") | ||
last_response.status.should == 400 | ||
last_response.body.should == "missing parameter: company" | ||
|
||
get('/', :name => "Bob", :company => "TestCorp") | ||
last_response.status.should == 200 | ||
last_response.body.should == "Hello" | ||
end | ||
|
||
it 'validates optional parameter if present' do | ||
get('/', :name => "Bob", :company => "TestCorp", :a_number => "string") | ||
last_response.status.should == 400 | ||
last_response.body.should == "invalid parameter: a_number" | ||
|
||
get('/', :name => "Bob", :company => "TestCorp", :a_number => 45) | ||
last_response.status.should == 200 | ||
last_response.body.should == "Hello" | ||
end | ||
|
||
it 'should coerce inputs' do | ||
get('/coerce', :int => "43") | ||
last_response.status.should == 200 | ||
ret = MultiJson.load(last_response.body) | ||
ret["int"].should == "Fixnum" | ||
|
||
get('/coerce', :int => "40", :arr => ["1","20","3"], :bool => [1, 0]) | ||
last_response.status.should == 200 | ||
ret = MultiJson.load(last_response.body) | ||
ret["int"].should == "Fixnum" | ||
ret["arr"].should == "Fixnum" | ||
ret["bool"].should == true | ||
end | ||
|
||
end |