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:
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?