620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
# File 'lib/json_patterns.rb', line 620
def validate_members(path, data)
matching_first_names_by_validation =
Hash[*@alternatives.flat_map { |v| [v, v.matching_first_names(data)] }]
validations_with_matching_first_names =
matching_first_names_by_validation.select { |k, v| v.size > 0 }.keys
matching_names = set_union(matching_first_names_by_validation.values)
if matching_names.size == 0
found_names = data.empty? ?
'end of object members' :
"names: #{data.keys.map { |name| name.inspect }.join(', ')}"
return ObjectMembersValidationResult.new(
failures: [ValidationUnexpected.new(
path: path,
found: found_names,
expected: Set[*possible_first_names.map { |n| "name: #{n.inspect}" }],
)],
remainder: data,
)
elsif matching_names.size > 1
return ObjectMembersValidationResult.new(
failures: [ValidationAmbiguity.new(
path: path,
found: data.keys,
overlapping_patterns: validations_with_matching_first_names.flat_map { |v|
v.possible_first_names.to_a
},
)]
)
else
name = matching_names.to_a[0]
value = data[name]
remainder = data.dup
remainder.delete name
validations_matching_value =
validations_with_matching_first_names.select { |v| v.first_value_match?(name, value) }
if validations_matching_value.length == 0
return ObjectMembersValidationResult.new(
failures: [ValidationUnexpected.new(
path: path + [name],
found: shallow_value(data[name]),
expected: set_union(validations_with_matching_first_names.map { |v|
set_union(v.first_value_validations(name).map { |v| v.shallow_describe })
}),
)],
remainder: remainder,
)
elsif validations_matching_value.length == 1
return validations_matching_value[0].validate_members(path, data)
else
return ObjectMembersValidationResult.new(
failures: [ValidationAmbiguity.new(
path: path + [name],
found: shallow_value(data[name]),
overlapping_patterns: validations_matching_value.flat_map { |v|
v.first_value_validations(name).shallow_describe.to_a
},
)],
remainder: remainder,
)
end
end
end
|