Skip to content

Commit 738a8f1

Browse files
committed
Use root schema for custom keywords
`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
1 parent 0cc797d commit 738a8f1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/json_schemer/schema.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def validate_instance(instance, instance_location, keyword_location, context)
148148
adjacent_results[keyword_instance.class] = keyword_result
149149
end
150150

151-
if custom_keywords.any?
152-
custom_keywords.each do |custom_keyword, callable|
151+
if root.custom_keywords.any?
152+
root.custom_keywords.each do |custom_keyword, callable|
153153
if value.key?(custom_keyword)
154154
[*callable.call(instance, value, instance_location)].each do |custom_keyword_result|
155155
custom_keyword_valid = custom_keyword_result == true

test/json_schemer_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ def test_it_validates_correctly_custom_keywords
209209
refute(schema.valid?(3))
210210
end
211211

212+
def test_it_validates_custom_keywords_in_nested_schemas
213+
schemer = JSONSchemer.schema(
214+
{
215+
'properties' => {
216+
'x' => {
217+
'yah' => true
218+
}
219+
}
220+
},
221+
keywords: {
222+
'yah' => proc do |instance, _schema, _instance_location|
223+
instance == 'valid'
224+
end
225+
}
226+
)
227+
assert(schemer.valid?({ 'x' => 'valid' }))
228+
refute(schemer.valid?({ 'x' => 'invalid' }))
229+
end
230+
212231
def test_it_handles_multiple_of_floats
213232
assert(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.61))
214233
refute(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.666))

0 commit comments

Comments
 (0)