Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating an array of OR composed schema fails if the last schema fails #389

Open
Goltergaul opened this issue Dec 6, 2021 · 0 comments

Comments

@Goltergaul
Copy link

Goltergaul commented Dec 6, 2021

SchemaA = Dry::Schema.JSON do
  config.types = Types::TypeContainer
  required(:type).value(eql?: "typeA")
  required(:attributes).value(:hash) do
    required(:foo).filled(:string)
  end
end

SchemaB = Dry::Schema.JSON do
  config.types = Types::TypeContainer
  required(:type).value(eql?: "typeB")
  required(:attributes).value(:hash) do
    required(:bar).filled(:string)
  end
end

class Contract < Dry::Validation::Contract
  json do
    config.validate_keys = true
    config.types = Types::TypeContainer
  
    optional(:features).value(:array).each do
      schema(SchemaA) | schema(SchemaB)
    end
  end
end

If i validate the above contract with a valid value where the features array contains something that matches SchemaB then it passes, but it does fail if i pass something valid in that matches SchemaA but not SchemaB, returning errors complaining that the keys passed in are unexpected for SchemaB.

Switching the order of the schemas to schema(SchemaB) | schema(SchemaA) lets it pass again.

Also I noticed that If I remove the line config.validate_keys = true then i get the following error instead: "undefined method 'to_or' for #Array:0x000055b06a7f9d90`. Stacktrace:

# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message/or/multi_path.rb:23:in `block in initialize'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message/or/multi_path.rb:23:in `map'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message/or/multi_path.rb:23:in `initialize'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message/or.rb:22:in `new'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message/or.rb:22:in `[]'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:120:in `visit_or'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:82:in `visit'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:174:in `visit_key'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:82:in `visit'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:179:in `block in visit_set'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:179:in `map'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:179:in `visit_set'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:82:in `visit'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:174:in `visit_key'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:82:in `visit'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:88:in `visit_failure'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:82:in `visit'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:75:in `block in call'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:75:in `map'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/message_compiler.rb:75:in `call'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-schema-1.8.0/lib/dry/schema/result.rb:176:in `message_set'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:221:in `schema_errors'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:216:in `initialize_errors'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:57:in `initialize'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:24:in `new'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:24:in `new'
# /home/dog/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-validation-1.7.0/lib/dry/validation/contract.rb:105:in `call'

FYI i'm passing in symbolized strings, in contrary to what should be working as mentioned here: #306 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant