Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly stated what parameter caused the error #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/sinatra/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def param(name, type, options = {})
params[name] = coerce(params[name], type, options)
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options.has_key?(:default)
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
validate!(params[name], options)
validate!(params[name], options, name)
params[name]
rescue InvalidParameterError => exception
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
Expand Down Expand Up @@ -132,13 +132,13 @@ def coerce(param, type, options = {})
end
end

def validate!(param, options)
def validate!(param, options, name)
options.each do |key, value|
case key
when :required
raise InvalidParameterError, "Parameter is required" if value && param.nil?
raise InvalidParameterError, "The parameter #{name} is required" if value && param.nil?
when :blank
raise InvalidParameterError, "Parameter cannot be blank" if !value && case param
raise InvalidParameterError, "The parameter #{name} cannot be blank" if !value && case param
when String
!(/\S/ === param)
when Array, Hash
Expand All @@ -147,25 +147,25 @@ def validate!(param, options)
param.nil?
end
when :format
raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String)
raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value
raise InvalidParameterError, "The parameter #{name} must be a string if using the format validation" unless param.kind_of?(String)
raise InvalidParameterError, "The parameter #{name} must match format #{value}" unless param =~ value
when :is
raise InvalidParameterError, "Parameter must be #{value}" unless param === value
raise InvalidParameterError, "The parameter #{name} must be #{value}" unless param === value
when :in, :within, :range
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
raise InvalidParameterError, "The parameter #{name} must be within #{value}" unless param.nil? || case value
when Range
value.include?(param)
else
Array(value).include?(param)
end
when :min
raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
raise InvalidParameterError, "The parameter #{name} cannot be less than #{value}" unless param.nil? || value <= param
when :max
raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param
raise InvalidParameterError, "The parameter #{name} cannot be greater than #{value}" unless param.nil? || value >= param
when :min_length
raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length
raise InvalidParameterError, "The parameter #{name} cannot have length less than #{value}" unless param.nil? || value <= param.length
when :max_length
raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length
raise InvalidParameterError, "The parameter #{name} cannot have length greater than #{value}" unless param.nil? || value >= param.length
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/parameter_transformations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
it 'skips transformations when the value is nil' do
get('/transform/required') do |response|
expect(response.status).to eql 400
expect(JSON.parse(response.body)['message']).to eq("Parameter is required")
expect(JSON.parse(response.body)['message']).to eq("The parameter order is required")
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/parameter_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it 'returns 400 on requests without required fields' do
get('/validation/required') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter is required")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg is required")
end
end

Expand All @@ -20,28 +20,28 @@
it 'returns 400 on requests when string is blank' do
get('/validation/blank/string', arg: '') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be blank")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be blank")
end
end

it 'returns 400 on requests when array is blank' do
get('/validation/blank/array', arg: '') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be blank")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be blank")
end
end

it 'returns 400 on requests when hash is blank' do
get('/validation/blank/hash', arg: '') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be blank")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be blank")
end
end

it 'returns 400 on requests when hash is blank' do
get('/validation/blank/other', arg: '') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be blank")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be blank")
end
end

Expand Down Expand Up @@ -76,7 +76,7 @@
it 'returns 400 on requests when value is other than defined' do
get('/validation/is', arg: 'bar') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be foo")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg must be foo")
end
end

Expand All @@ -91,7 +91,7 @@
it 'returns 400 on requests with a value not in the set' do
get('/validation/in', arg: 'MISC') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within [\"ASC\", \"DESC\"]")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg must be within [\"ASC\", \"DESC\"]")
end
end

Expand All @@ -106,7 +106,7 @@
it 'returns 400 on requests with a value outside the range' do
get('/validation/within', arg: 20) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within 1..10")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg must be within 1..10")
end
end

Expand All @@ -121,7 +121,7 @@
it 'returns 400 on requests with a value outside the range' do
get('/validation/range', arg: 20) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within 1..10")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg must be within 1..10")
end
end

Expand All @@ -136,7 +136,7 @@
it 'returns 400 on requests with a value smaller than min' do
get('/validation/min', arg: 5) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be less than 12")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be less than 12")
end
end

Expand All @@ -151,7 +151,7 @@
it 'returns 400 on requests with a value larger than max' do
get('/validation/max', arg: 100) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot be greater than 20")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot be greater than 20")
end
end

Expand All @@ -166,7 +166,7 @@
it 'returns 400 on requests with a string shorter than min_length' do
get('/validation/min_length', arg: 'hi') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot have length less than 5")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot have length less than 5")
end
end

Expand All @@ -181,7 +181,7 @@
it 'returns 400 on requests with a string longer than max_length' do
get('/validation/max_length', arg: 'reallylongstringlongerthanmax') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter cannot have length greater than 10")
expect(JSON.parse(response.body)['message']).to eq("The parameter arg cannot have length greater than 10")
end
end

Expand Down