Skip to content

Commit

Permalink
Merge branch 'master' into 0.7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jul 21, 2016
2 parents a5d57a9 + f5853fe commit 7c894b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Complete, fast and testable actions for Rack

## v0.7.0 - (unreleased)
### Added
- [Luca Guidi] Introduced `Hanami::Action::Params#error_messages` which returns a flat collection of full error messages

### Fixed
- [Luca Guidi] Params are deeply symbolized
- [Artem Nistratov] Send only changed cookies in HTTP response

### Changed
[Luca Guidi] Drop support for Ruby 2.0 and 2.1. Official support for JRuby 9.0.5.0+.
- [Luca Guidi] Drop support for Ruby 2.0 and 2.1. Official support for JRuby 9.0.5.0+.
- [Luca Guidi] Param validations now require you to add `hanami-validations` in `Gemfile`.
- [Luca Guidi] Removed "_indifferent access_" for params. Since now on, only symbols are allowed.
- [Luca Guidi] Params are immutable
Expand Down
25 changes: 25 additions & 0 deletions lib/hanami/action/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,35 @@ def raw
@input
end

# Returns structured error messages
#
# @return [Hash]
#
# @since x.x.x
#
# @example
# params.errors
# # => {:email=>["is missing", "is in invalid format"], :name=>["is missing"], :tos=>["is missing"], :age=>["is missing"], :address=>["is missing"]}
def errors
@result.messages
end

# Returns flat collection of full error messages
#
# @return [Array]
#
# @since x.x.x
#
# @example
# params.error_messages
# # => ["Email is missing", "Email is in invalid format", "Name is missing", "Tos is missing", "Age is missing", "Address is missing"]
def error_messages
errors.each_with_object([]) do |(key, messages), result|
k = Utils::String.new(key).titleize
result.concat messages.map { |message| "#{k} #{message}" }
end
end

def valid?
@result.success?
end
Expand Down
3 changes: 3 additions & 0 deletions test/action/params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,16 @@
params.errors.fetch(:name).must_equal ['is missing']
params.errors.fetch(:tos).must_equal ['is missing']
params.errors.fetch(:address).must_equal ['is missing']

params.error_messages.must_equal ['Email is missing', 'Email is in invalid format', 'Name is missing', 'Tos is missing', 'Age is missing', 'Address is missing']
end

it "is it valid when all the validation criteria are met" do
params = TestParams.new(email: 'test@hanamirb.org', name: 'Luca', tos: '1', age: '34', address: { line_one: '10 High Street', deep: { deep_attr: 'blue' } })

params.valid?.must_equal true
params.errors.must_be_empty
params.error_messages.must_be_empty
end

it "has input available through the hash accessor" do
Expand Down

0 comments on commit 7c894b8

Please sign in to comment.