Skip to content

Commit e947f2a

Browse files
committed
Pass resolved instance location to custom keywords
Never meant to expose `Location` hashes. Looks like this was an oversight when they were introduced. Noticed this while looking into: #166
1 parent 738a8f1 commit e947f2a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/json_schemer/schema.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ def validate_instance(instance, instance_location, keyword_location, context)
149149
end
150150

151151
if root.custom_keywords.any?
152+
resolved_instance_location = Location.resolve(instance_location)
152153
root.custom_keywords.each do |custom_keyword, callable|
153154
if value.key?(custom_keyword)
154-
[*callable.call(instance, value, instance_location)].each do |custom_keyword_result|
155+
[*callable.call(instance, value, resolved_instance_location)].each do |custom_keyword_result|
155156
custom_keyword_valid = custom_keyword_result == true
156157
valid &&= custom_keyword_valid
157158
type = custom_keyword_result.is_a?(String) ? custom_keyword_result : custom_keyword

test/json_schemer_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,32 @@ def test_it_validates_custom_keywords_in_nested_schemas
228228
refute(schemer.valid?({ 'x' => 'invalid' }))
229229
end
230230

231+
def test_custom_keywords_receive_resolved_instance_location
232+
custom_keyword_instance_location = nil
233+
schemer = JSONSchemer.schema(
234+
{
235+
'properties' => {
236+
'x' => {
237+
'yah' => true
238+
},
239+
'y' => {
240+
'yah' => true
241+
}
242+
}
243+
},
244+
keywords: {
245+
'yah' => proc do |instance, _schema, instance_location|
246+
custom_keyword_instance_location = instance_location
247+
instance == 'valid'
248+
end
249+
}
250+
)
251+
assert(schemer.valid?({ 'x' => 'valid' }))
252+
assert_equal('/x', custom_keyword_instance_location)
253+
refute(schemer.valid?({ 'y' => 'invalid' }))
254+
assert_equal('/y', custom_keyword_instance_location)
255+
end
256+
231257
def test_it_handles_multiple_of_floats
232258
assert(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.61))
233259
refute(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.666))

0 commit comments

Comments
 (0)