# \[dry-schema\] Filter for each element in array

**URL:** <https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896>\
**Category:** Support\
**Created:** [September 10, 2019, 1:57pm UTC](https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896 "2019-09-10T13:57:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DawidJanczak](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dawidjanczak/32/261_2.png) [@DawidJanczak](https://discourse.dry-rb.org/u/DawidJanczak)\
**Post date:** [September 10, 2019, 1:57pm UTC](https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896/1 "2019-09-10T13:57:05Z")

</div>

Hello!

I was wondering if I can combine `filter` with `each`? For example imagine that the schema receives an array looking like this [“09”, “12”, “2019”]. Before casting that to date by using a custom type I’d like to verify that the array size is 3 and that each element is an int disguised as a string. I was thinking something like this `optional(:date).filter(size?: 3, each?: { format?: /\A\d+\Z/ })`, but that doesn’t work. On the other hand just chaining `.each` separately (`optional(:date).filter(size?: 3).each(format?: /\A\d+\Z/)`) causes it to be evaluated after type cast.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [September 10, 2019, 8:11pm UTC](https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896/2 "2019-09-10T20:11:36Z")

</div>

Yes you can do this:

```ruby
optional(:date)
  .filter(:array, size?: 3) { each(format?: /\A\d+\Z/) }
  .value(...)

```

---

<div class="post-metadata">

**Author:** ![DawidJanczak](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dawidjanczak/32/261_2.png) [@DawidJanczak](https://discourse.dry-rb.org/u/DawidJanczak)\
**Post date:** [September 11, 2019, 7:48am UTC](https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896/3 "2019-09-11T07:48:55Z")

</div>

Works like a charm, thank you!

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [May 24, 2023, 9:56pm UTC](https://discourse.dry-rb.org/t/dry-schema-filter-for-each-element-in-array/896/4 "2023-05-24T21:56:17Z")

</div>

This appears to no longer work. Was this behavior changed, or is this a regression? Or maybe I am doing something wrong?

```ruby
schema = Dry::Schema.Params do
  optional(:date)
    .filter(:array, size?: 3) { each(format?: /\A\d+\Z/) }
    .value(array[:string])
end

schema.({ date: ["1684964902", "", "1684965059"] })
#<Dry::Schema::Result{:date=>["1684964902", "", "1684965059"]} errors={:date=>{1=>["is in invalid format"]}} path=[]>

```
