Skip to content

Commit

Permalink
Allow RequiredParam to display a custom message
Browse files Browse the repository at this point in the history
  • Loading branch information
blakesmith committed May 11, 2011
1 parent 7dd9e04 commit 1ae43e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/goliath/rack/validation/required_param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ module Validation
#
class RequiredParam
include Goliath::Rack::Validator
attr_reader :type, :key
attr_reader :type, :key, :message

# Creates the Goliath::Rack::Validation::RequiredParam validator
#
# @param app The app object
# @param opts [Hash] The validator options
# @option opts [String] :key The key to look for in params (default: id)
# @option opts [String] :type The type string to put in the error message. (default: :key)
# @option opts [String] :message The message string to display after the type string. (default: 'identifier missing')
# @return [Goliath::Rack::Validation::RequiredParam] The validator
def initialize(app, opts = {})
@app = app
@key = opts[:key] || 'id'
@type = opts[:type] || @key.capitalize
@message = opts[:message] || 'identifier missing'
end

def call(env)
return validation_error(400, "#{@type} identifier missing") unless key_valid?(env['params'])
return validation_error(400, "#{@type} #{@message}") unless key_valid?(env['params'])
@app.call(env)
end

Expand All @@ -51,4 +53,4 @@ def key_valid?(params)
end
end
end
end
end
13 changes: 10 additions & 3 deletions spec/unit/rack/validation/required_param_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@
end

it 'accepts options on create' do
opts = { :type => 1, :key => 2 }
opts = { :type => 1, :key => 2, :message => 'is required' }
lambda { Goliath::Rack::Validation::RequiredParam.new('my app', opts) }.should_not raise_error
end

it 'defaults type and key' do
it 'defaults type, key and message' do
@rp = Goliath::Rack::Validation::RequiredParam.new('app')
@rp.key.should_not be_nil
@rp.key.should_not =~ /^\s*$/

@rp.type.should_not be_nil
@rp.type.should_not =~ /^\s*$/

@rp.message.should == 'identifier missing'
end

describe 'with middleware' do
before(:each) do
@app = mock('app').as_null_object
@env = {'params' => {}}
@rp = Goliath::Rack::Validation::RequiredParam.new(@app, {:type => 'Monkey', :key => 'mk'})
@rp = Goliath::Rack::Validation::RequiredParam.new(@app, {:type => 'Monkey', :key => 'mk', :message => 'is required'})
end

it 'stores type and key options' do
@rp.type.should == 'Monkey'
@rp.key.should == 'mk'
end

it 'calls validation_error with a custom message' do
@rp.should_receive(:validation_error).with(anything, 'Monkey is required')
@rp.call(@env)
end

it 'returns the app status, headers and body' do
app_headers = {'Content-Type' => 'app'}
app_body = {'b' => 'c'}
Expand Down

0 comments on commit 1ae43e7

Please sign in to comment.