Skip to content

Commit

Permalink
Use root schema for custom keywords
Browse files Browse the repository at this point in the history
`custom_keywords` isn't passed to nested schemas (similar to formats,
hooks, and ref resolution) so they need to be read from the root schema
instead.

Noticed this while looking into: #166
  • Loading branch information
davishmcclurg committed Feb 25, 2024
1 parent d52c130 commit 93c85a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/json_schemer/schema.rb
Expand Up @@ -148,8 +148,8 @@ def validate_instance(instance, instance_location, keyword_location, context)
adjacent_results[keyword_instance.class] = keyword_result
end

if custom_keywords.any?
custom_keywords.each do |custom_keyword, callable|
if root.custom_keywords.any?
root.custom_keywords.each do |custom_keyword, callable|
if value.key?(custom_keyword)
[*callable.call(instance, value, instance_location)].each do |custom_keyword_result|
custom_keyword_valid = custom_keyword_result == true
Expand Down
19 changes: 19 additions & 0 deletions test/json_schemer_test.rb
Expand Up @@ -209,6 +209,25 @@ def test_it_validates_correctly_custom_keywords
refute(schema.valid?(3))
end

def test_it_validates_custom_keywords_in_nested_schemas
schemer = JSONSchemer.schema(
{
'properties' => {
'x' => {
'yah' => true
}
}
},
keywords: {
'yah' => proc do |instance, _schema, _instance_location|
instance == 'valid'
end
}
)
assert(schemer.valid?({ 'x' => 'valid' }))
refute(schemer.valid?({ 'x' => 'invalid' }))
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 93c85a5

Please sign in to comment.