# Preprocessing certain fields in a schema

**URL:** <https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048>\
**Category:** dry-validation\
**Created:** [April 29, 2020, 6:53am UTC](https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048 "2020-04-29T06:53:48Z")\
**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:** [April 29, 2020, 6:53am UTC](https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048/1 "2020-04-29T06:53:48Z")

</div>

Hello,

I’d like to validate some data coming in from Google Datastore. Unfortunately some types there are wrapped in a StringIO and so I need to somehow preprocess them. Specifically calling `.properties.to_h` on a response gives me a hash with an `items` array that’s an array of hashes within a StringIO instance. So what I’d like to do is call `JSON.parse(input.string)` to unwrap it and then I’d like to validate each hash within that. I tried doing that with a custom type `DatastoreArrayBlob = Array.constructor { |input| ::JSON.parse(input.string) }`, but then I can’t seem to be able to validate that inside the contract:

```auto
class MyContract < Dry::Validation::Contract
  json do
    required(:items).value(Types::DatastoreArrayBlob).each do
      hash do
        required(:code).filled(:string)
      end
    end
  end
end

```

For some reason this always clears the code so in result I’m getting `(...):items=>[{}]} errors={:items=>{0=>{:code=>["is missing"]}` Now if I remove the `each` call I can see that the type constructor works just fine as `items` indeed has hashes with `code` keys, but for some reason that `.each` call clears them. Is there perhaps another way to write that for such custom arrays?

---

<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:** [April 29, 2020, 9:00am UTC](https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048/2 "2020-04-29T09:00:24Z")

</div>

This should work. Could you provide a reproduction script/test?

---

<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:** [April 30, 2020, 6:35am UTC](https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048/3 "2020-04-30T06:35:32Z")

</div>

@solnic Sure, hope this works: [https://github.com/DawidJanczak/dry-validation-custom-array-type/blob/master/test.rb](https://github.com/DawidJanczak/dry-validation-custom-array-type/blob/master/test.rb)

---

<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:** [April 30, 2020, 9:09am UTC](https://discourse.dry-rb.org/t/preprocessing-certain-fields-in-a-schema/1048/4 "2020-04-30T09:09:04Z")

</div>

@DawidJanczak damn, that’s a bug - please report it under dry-schema project. As a workaround you can use `JSON.parse(input.string, symbolize_keys: true)`.
