These are chat archives for dry-rb/chat

2nd
May 2018
Ryan Bigg
@radar
May 02 2018 00:28
dry-rb/dry-equalizer#9 I don't understand how / why this is different from dry-rb/dry-equalizer#8 but there you go. So very perplexed.
Turns out: I don't know Ruby.
Joshua Hansen
@binarypaladin
May 02 2018 03:38
I can't tell if I've hit a bug or I'm using some kind of behavior incorrectly, but it seems to me there are oddities when using schema inheritance with schemas that are create with JSON or Form. Consider this:
Schema = Dry::Validation.Schema do
  configure { config.type_specs = true }
  required(:first_name, :string).filled(:str?)
end

OtherSchema = Dry::Validation.Schema(Schema) do
  required(:last_name, :string).filled(:str?)
end

input = { first_name: 'John', last_name: 'Smith' }
Schema.(input).success? => true
Schema.(input).output => {:first_name=>"John", :last_name=>"Smith"}
OtherSchema.(input).success? => true
OtherSchema.(input).output => {:first_name=>"John", :last_name=>"Smith"}

Schema = Dry::Validation.JSON do
  configure { config.type_specs = true }
  required(:first_name, :string).filled(:str?)
end

OtherSchema = Dry::Validation.Schema(Schema) do
  required(:last_name, :string).filled(:str?)
end

input = { 'first_name' => 'Joe', 'last_name' => 'Smith' }

Schema.(input).success? => true
Schema.(input).output => {:first_name=>"Joe"}
OtherSchema.(input).success? => false
OtherSchema.(input).errors => {:first_name=>["is missing"]}
I cannot seem seem to get OtherSchema to recognize first_name. Inheritance works as I would expect with the normal schema. It's not a problem with the JSON macro either, as this would yield the same issues:
Schema = Dry::Validation.Schema do
  configure do
    config.type_specs = true
    config.input_processor = :json
    config.hash_type = :symbolized
  end
  required(:first_name, :string).filled(:str?)
end

OtherSchema = Dry::Validation.Schema(Schema) do
  required(:last_name, :string).filled(:str?)
end
I thought perhaps explicitly setting configuration on both would do the trick, but no dice:
OtherSchema = Dry::Validation.Schema(Schema) do
  configure do
    config.type_specs = true
    config.input_processor = :json
    config.hash_type = :symbolized
  end
  required(:last_name, :string).filled(:str?)
end
Joshua Hansen
@binarypaladin
May 02 2018 03:45
Setting input_processor is clearly the culprit when messing around with scenarios.
Any ideas on how to handle inheritance with with a preprocessor set?
Joshua Hansen
@binarypaladin
May 02 2018 04:00

One more thing to add, it isn't just the input_preprocessor but rather a combination of that and type_specs set to true. Looking at OtherSchema I see that:

OtherSchema.config[:type_map] => {:last_name=>#<Dry::Types::Definition primitive=String options={} meta={}>}

Not that first_name is not in the type_map. I haven't done a deep dive into the code yet, but if anyone familiar with the finer points can give me any pointers (or just flat out tell me if this is a bug and, if so, I'll work to patch it) that would be really handy.

Joshua Hansen
@binarypaladin
May 02 2018 16:32

Looks like the bug has actually been patched: https://github.com/dry-rb/dry-validation/pull/359/commits/71872da3985c54b6930ed39c42d004f837e9812f

This fixes the behavior. Is there something wrong with this PR?