|
3 | 3 | module MediaTypes |
4 | 4 | class Scheme |
5 | 5 | class ValidationOptions |
6 | | - attr_accessor :exhaustive, :strict, :backtrace |
| 6 | + attr_accessor :exhaustive, :strict, :backtrace, :context |
7 | 7 |
|
8 | | - def initialize(exhaustive: true, strict: true, backtrace: []) |
| 8 | + def initialize(context = {}, exhaustive: true, strict: true, backtrace: []) |
9 | 9 | self.exhaustive = exhaustive |
10 | 10 | self.strict = strict |
11 | 11 | self.backtrace = backtrace |
| 12 | + self.context = context |
12 | 13 | end |
13 | 14 |
|
14 | 15 | def inspect |
15 | | - "backtrack: #{backtrace.inspect}, strict: #{strict.inspect}, exhaustive: #{exhaustive}" |
| 16 | + "backtrack: #{backtrace.inspect}, strict: #{strict.inspect}, exhaustive: #{exhaustive}, current_obj: #{scoped_output.to_json}" |
| 17 | + end |
| 18 | + |
| 19 | + def scoped_output |
| 20 | + current = context |
| 21 | + |
| 22 | + backtrace.drop(1).first([0, backtrace.size - 2].max).each do |e| |
| 23 | + current = current[e] unless current.nil? |
| 24 | + end |
| 25 | + |
| 26 | + current |
16 | 27 | end |
17 | 28 |
|
18 | 29 | def with_backtrace(backtrace) |
19 | | - ValidationOptions.new(exhaustive: exhaustive, strict: strict, backtrace: backtrace) |
| 30 | + ValidationOptions.new(context, exhaustive: exhaustive, strict: strict, backtrace: backtrace) |
20 | 31 | end |
21 | 32 |
|
22 | 33 | def trace(*traces) |
23 | 34 | with_backtrace(backtrace.dup.concat(traces)) |
24 | 35 | end |
25 | 36 |
|
26 | 37 | def exhaustive! |
27 | | - ValidationOptions.new(exhaustive: true, strict: strict, backtrace: backtrace) |
| 38 | + ValidationOptions.new(context, exhaustive: true, strict: strict, backtrace: backtrace) |
28 | 39 | end |
29 | 40 | end |
30 | 41 | end |
|
0 commit comments