-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Problem with hash predicate logic in array field #357
Comments
This fixes the crash but it's hard to say whether it can be treated as THE fix: diff --git lib/dry/schema/message/or.rb lib/dry/schema/message/or.rb
index f8d4736..c73fcc1 100644
--- lib/dry/schema/message/or.rb
+++ lib/dry/schema/message/or.rb
@@ -15,14 +15,20 @@ module Dry
msgs = [left, right].flatten
paths = msgs.map(&:path)
- if paths.uniq.size == 1
- SinglePath.new(left, right, messages)
+ if left.is_a?(Array)
+ if right.is_a?(Array) && paths.uniq.size > 1
+ MultiPath.new(left, right)
+ else
+ left
+ end
elsif right.is_a?(Array)
if left.is_a?(Array) && paths.uniq.size > 1
MultiPath.new(left, right)
else
right
end
+ elsif paths.uniq.size == 1
+ SinglePath.new(left, right, messages)
else
msgs.max
end Then with your example slightly tweaked: require 'dry/validation'
class Pictures < Dry::Schema::Params
define do
required(:name).filled(:string, eql?: 'pictures')
end
end
class ScanBarcode < Dry::Schema::Params
define do
required(:name).filled(:string, eql?: 'scan_barcode')
end
end
class MissionActionTypeSchema < Dry::Schema::Params
define do
optional(:id).filled(:string)
required(:previous_mission_status_type_reference).filled(:string)
required(:next_mission_status_type_reference).filled(:string)
optional(:mission_event_type_references).array(:string)
optional(:hidden).maybe(:bool)
optional(:prerequisites).array(:hash, Pictures.new | ScanBarcode.new)
end
end
s = MissionActionTypeSchema.new
res = s.call({ id: '1', previous_mission_status_type_reference: 'a', next_mission_status_type_reference: 'b', prerequisites: [{name: 'pictu'}]})
puts res.errors.to_h.inspect
# {:prerequisites=>{0=>{:name=>["must be equal to pictures"]}}} It's tricky because producing "OR" messages coming from two different schemas requires special processing. You need to gather all errors, see which paths are the same and then convert them into "Message::Or". So, the patch fixes the crash but it doesn't seem to be the final solution. |
Maybe this idea have already thread but why don't implement something like @JsonSubTypes from the Jackson java library to treat this case ? |
@fijemax I'm not familiar with this library, how would this help? |
@fijemax could you see if this patch works for you? #387 It fixes a few problems I've encountered, but I also wouldn't call it a proper fix. In my opinion, it might be better to rearchitect all of I'm not a regular contributor, so my intuition might not be best here though. |
Describe the bug
Problem during schema validation: On the prerequisites field of the example I want validate that this array contain only hash describe by the previous schemas. Every schema contain a name with a specifig value. When all hashes match with "pictures" or "scan_barcode" everything is ok. But when an I put anything else in the name field of a hash the errors method on result return an exception
To Reproduce
This is my code:
Expected behavior
I think in normal case res.errors should return an error relative to the index of hash who fail to correpsond of one of the schemas.
My environment
The text was updated successfully, but these errors were encountered: