Closed radarek closed 1 year ago
Thank you! This is great ❤️ 🚀
@solnic Can you please revert it? This implementation is not correct. I need to rethink some edge cases.
Here is an example of failing specs with this PR but working on main:
it "copies key map from the parent and includes new keys from child" do
schema = Dry::Schema.Params do
config.validate_keys = true
required(:a).filled(:string)
required(:fooA).filled(:string)
required(:foo).array(:hash) do
required(:bar).filled(:string)
end
end
expect(schema.(a: "string", fooA: "string", foo: "string").errors.to_h)
.to eql({foo: ["must be an array"]})
end
In this PR it produces :foo => ["is not allowed"]
.
@radarek no worries. Could we have another PR that fixes it instead of reverting?
@solnic If it is not a problem that currently main is broken then I will create new PR. I need some time to make more tests.
I noticed that validating schema is very slow if it contains thousands of keys. Current algorithm is O(N^2), which means that doubling number of keys makes it ~four times slower. I was able to optimize it by sorting keys once and then using
Array#bsearch
method which is O(N*logN).Benchmark
Here is benchmark I run before and after the change:
Before:
After: