Skip to content

Commit

Permalink
Add more examples for maybe(:array) (close #480)
Browse files Browse the repository at this point in the history
Co-Authored-By: Kenneth Teh <[email protected]>
  • Loading branch information
flash-gordon and kennethteh90 committed Jan 6, 2025
1 parent 6bb917f commit e143710
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docsite/source/nested-data.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,33 @@ puts errors.to_h.inspect
# }

```

Use `maybe(array[?])` to validate an array that could be `nil`:

```ruby
nested_schema = Dry::Schema.Params do
required(:name).filled(:string)
end

schema = Dry::Schema.Params do
required(:tags).maybe(array[nested_schema])
end

schema.call(tags: nil).success? # true
schema.call(tags: [{ name: 'Alice' }, { name: 'Bob' }]).success? # true
```

Or use `maybe(:array)` with a block:

```ruby
schema = Dry::Schema.Params do
required(:tags).maybe(:array) do
nil? | each(:hash) do
required(:name).filled(:string)
end
end
end

schema.call(tags: nil).success? # true
schema.call(tags: [{ name: 'Alice' }, { name: 'Bob' }]).success? # true
```

0 comments on commit e143710

Please sign in to comment.