Probably the most common use case is to validate HTTP params. This is a special kind of a validation for a couple of reasons:

  • The input is a hash with stringified keys
  • The input can include values that are strings, hashes or arrays
  • Prior to validation, we need to symbolize keys and coerce values based on the information in a schema

For that reason, dry-schema ships with Params schemas:

schema = Dry::Schema.Params do
  required(:age).filled(:integer, gt?: 18)

errors ='email' => '', 'age' => '18').errors

puts errors.to_h.inspect
# {
#   :email => ["must be filled"],
#   :age => ["must be greater than 18"]
# }

Params-specific value coercion is handled by the hash type from dry-types. It is built automatically for you based on the type specs and used prior to applying the validation rules

Handling empty strings

Your schema will automatically coerce empty strings to nil or an empty array, provided that you allow a value to be nil:

schema = Dry::Schema.Params do
  required(:age).maybe(:integer, gt?: 18)

result ='email' => '', 'age' => '', 'tags' => '')

puts result.to_h
# {:email=>'', :age=>nil, :tags=>[]}

octocatEdit on GitHub