[docsite] fixes in the"handling empty strings" section

Ref #232
This commit is contained in:
Piotr Solnica 2020-12-12 07:55:57 +01:00
parent 4a2c6d4c27
commit 502c2aa4ca
No known key found for this signature in database
GPG Key ID: 66BF2FDA7BA0F29C
1 changed files with 15 additions and 2 deletions

View File

@ -31,17 +31,30 @@ puts errors.to_h.inspect
### 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:
Your schema will automatically coerce empty strings to `nil`, provided that you allow a value to be nil:
```ruby
schema = Dry::Schema.Params do
required(:email).filled(:string)
required(:age).maybe(:integer, gt?: 18)
required(:age).maybe(:integer)
required(:tags).maybe(:array)
end
result = schema.call('email' => 'jane@doe.org', 'age' => '', 'tags' => '')
puts result.to_h
# {:email=>'jane@doe.org', :age=>nil, :tags=>nil}
```
Your schema will automatically coerce empty strings **to an empty array**:
```ruby
schema = Dry::Schema.Params do
required(:tags).value(:array)
end
result = schema.call('tags' => '')
puts result.to_h
# {:email=>'jane@doe.org', :age=>nil, :tags=>[]}
```