dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
424 stars 110 forks source link

Logical operators not supported by `each` #479

Closed ibrykov-mdsol closed 2 months ago

ibrykov-mdsol commented 2 months ago

Describe the bug

I'm trying to use logical OR to describe my array elements. I'm getting an exception:

Dry::Core::Container::KeyError: key not found: "#<Dry::Schema::Params:0x000000010d714428> OR #"

It looks like my logical operator is stringified into "#<Dry::Schema::Params:0x000000010d714428> OR #" instead of being evaluated.

To Reproduce

Here is a piece I'm trying to run

    string = Dry::Schema.Params do
      required(:type).value(eql?: "string")
    end
    integer = Dry::Schema.Params do
      required(:type).value(eql?: "integer")
    end
    schema = Dry::Schema.Params do
      required(:list).value(:array).each(string | integer)
    end

    assert_equal ({}), schema.({ list: [{ type: "string" }, { type: "integer" }] }).errors.to_h

Expected behavior

I expect my assert to be successful

My environment

ibrykov-mdsol commented 2 months ago

After reading/debugging the source code I came up with this syntax:

      required(:list).array(:hash?, string | integer)

And It seems to work as expected.