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 19, 2016
2 parents 3e42e7e + 35ea313 commit a5d57a9
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 191 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,21 @@ Params represent an untrusted input.
For security reasons it's recommended to whitelist them.

```ruby
require 'hanami/validations'
require 'hanami/controller'

class Signup
include Hanami::Action

params do
param :first_name
param :last_name
param :email
param :address do
param :line_one
param :state
param :country
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).filled(:str?)

required(:address).schema do
required(:line_one).filled(:str?)
required(:state).filled(:str?)
required(:country).filled(:str?)
end
end

Expand All @@ -174,7 +175,7 @@ class Signup
# Whitelist :first_name, but not :admin
puts params[:first_name] # => "Luca"
puts params[:admin] # => nil

# Whitelist nested params [:address][:line_one], not [:address][:line_two]
puts params[:address][:line_one] # => '69 Tender St'
puts params[:address][:line_two] # => nil
Expand All @@ -191,20 +192,21 @@ when params are invalid.
If you specify the `:type` option, the param will be coerced.

```ruby
require 'hanami/validations'
require 'hanami/controller'

class Signup
MEGABYTE = 1024 ** 2
include Hanami::Action

params do
param :first_name, presence: true
param :last_name, presence: true
param :email, presence: true, format: /@/, confirmation: true
param :password, presence: true, confirmation: true
param :terms_of_service, acceptance: true
param :avatar, size: 0..(MEGABYTE * 3)
param :age, type: Integer, size: 18..99
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).confirmation.filled?(:str?, format?: /@/)
required(:password).confirmation.filled(:str?)
required(:terms_of_service).filled(:bool?)
required(:age).filled(:int?, included_in?: 18..99)
optional(:avatar).filled(size?: 1..(MEGABYTE * 3))
end

def call(params)
Expand All @@ -219,10 +221,10 @@ action.call(valid_params) # => [200, {}, ...]
action.errors.empty? # => true

action.call(invalid_params) # => [400, {}, ...]
action.errors # => #<Hanami::Validations::Errors:0x007fabe4b433d0 @errors={...}>
action.errors.empty? # => false

action.errors.for(:email)
# => [#<Hanami::Validations::Error:0x007fabe4b432e0 @attribute=:email, @validation=:presence, @expected=true, @actual=nil>]
action.errors.fetch(:email)
# => ['is missing', 'is in invalid format']
```

### Response
Expand Down
6 changes: 5 additions & 1 deletion lib/hanami/action/base_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def [](key)
#
# @return [Object,NilClass] return the associated value, if found
#
# @raise [NoMethodError] if key is nil
#
# @since x.x.x
#
# @example
Expand All @@ -89,7 +91,9 @@ def [](key)
# end
def get(key)
key, *keys = key.to_s.split(GET_SEPARATOR)
result = self[key.to_sym]
return if key.nil?

result = self[key.to_sym]

Array(keys).each do |k|
break if result.nil?
Expand Down
Loading

0 comments on commit a5d57a9

Please sign in to comment.