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

Rescue coerce errors and return an error hash #23

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to sinatra-browse will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Next version

* `Changed` Incoercible params, such as an invalid `DateTime` param, will give back a 400 with reason `invalid` instead of raising an exception.

## [0.6.1] - 2015-07-07

* `Added` Support for Ruby 2.2.2. That means the unit tests are now run on that version. It was probably working already anyway.
Expand Down
6 changes: 5 additions & 1 deletion lib/sinatra/browse/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def process(params)
next
end

params[name] = pd.coerce(params[name])
begin
params[name] = pd.coerce(params[name])
rescue
return false, pd.build_error_hash(:invalid, params[name])
end

success, error_hash = pd.validate(params)
return false, error_hash unless success
Expand Down
7 changes: 7 additions & 0 deletions spec/date_time_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
end
end

context "with an invalid date" do
it "validates the date and returns an error" do
get("features/date_time_validation", string_min: "2019/02/29")
expect(status).to eq 400
end
end

[
[:max, {string_max: '2005-1-1'}],
[:min, {string_min: '2014-2-4'}]
Expand Down