dry-validation for file upload

Is there any “legal” way/idea of validating key type of File?

optional(:photo).filled(:file?)

Sure, you’ll just need to define a custom type for it.

#!/usr/bin/env ruby

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "dry-schema"
end

require "dry/schema"
require "dry/types"
require "tempfile"

T = Dry::Types(default: :strict)

module T
  # Tempfile is tricky because it's a Delegator
  File = Instance(::IO) | Instance(Tempfile)
end

Schema = Dry::Schema.JSON do
  optional(:photo).value(T::File)
end

Tempfile.open do |file|
  puts Schema.({ photo: file }).inspect
end

1 Like

That solves my problem!
Thank you!
Should we consider making a PR for dry/types?