diff --git a/CHANGELOG.md b/CHANGELOG.md index 03dca8b..c76e209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/sinatra/browse/route.rb b/lib/sinatra/browse/route.rb index b17c2f5..256e859 100644 --- a/lib/sinatra/browse/route.rb +++ b/lib/sinatra/browse/route.rb @@ -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 diff --git a/spec/date_time_validation_spec.rb b/spec/date_time_validation_spec.rb index e38f197..cb63617 100644 --- a/spec/date_time_validation_spec.rb +++ b/spec/date_time_validation_spec.rb @@ -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'}]