Skip to content

Commit

Permalink
Pass resolved instance location to custom keywords
Browse files Browse the repository at this point in the history
Never meant to expose `Location` hashes. Looks like this was an
oversight when they were introduced.

Noticed this while looking into: #166
  • Loading branch information
davishmcclurg committed Dec 16, 2023
1 parent 738a8f1 commit e947f2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/json_schemer/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ def validate_instance(instance, instance_location, keyword_location, context)
end

if root.custom_keywords.any?
resolved_instance_location = Location.resolve(instance_location)
root.custom_keywords.each do |custom_keyword, callable|
if value.key?(custom_keyword)
[*callable.call(instance, value, instance_location)].each do |custom_keyword_result|
[*callable.call(instance, value, resolved_instance_location)].each do |custom_keyword_result|
custom_keyword_valid = custom_keyword_result == true
valid &&= custom_keyword_valid
type = custom_keyword_result.is_a?(String) ? custom_keyword_result : custom_keyword
Expand Down
26 changes: 26 additions & 0 deletions test/json_schemer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,32 @@ def test_it_validates_custom_keywords_in_nested_schemas
refute(schemer.valid?({ 'x' => 'invalid' }))
end

def test_custom_keywords_receive_resolved_instance_location
custom_keyword_instance_location = nil
schemer = JSONSchemer.schema(
{
'properties' => {
'x' => {
'yah' => true
},
'y' => {
'yah' => true
}
}
},
keywords: {
'yah' => proc do |instance, _schema, instance_location|
custom_keyword_instance_location = instance_location
instance == 'valid'
end
}
)
assert(schemer.valid?({ 'x' => 'valid' }))
assert_equal('/x', custom_keyword_instance_location)
refute(schemer.valid?({ 'y' => 'invalid' }))
assert_equal('/y', custom_keyword_instance_location)
end

def test_it_handles_multiple_of_floats
assert(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.61))
refute(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.666))
Expand Down

0 comments on commit e947f2a

Please sign in to comment.