Skip to content

Commit

Permalink
Merge pull request #133 from davishmcclurg/json-pointer-ref
Browse files Browse the repository at this point in the history
Resolve JSON pointer refs locally
  • Loading branch information
davishmcclurg authored Jun 14, 2023
2 parents 63224cf + dacec83 commit 8ac15d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/json_schemer/schema/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def validate_ref(instance, ref, &block)
ref_uri.fragment = nil
end

ref_object = if ids.key?(ref_uri) || ref_uri.to_s == @base_uri.to_s
ref_object = if ids.key?(ref_uri) || ref_uri.to_s.empty? || ref_uri.to_s == @base_uri.to_s
self
else
child(resolve_ref(ref_uri), base_uri: ref_uri)
Expand Down
23 changes: 23 additions & 0 deletions test/ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,27 @@ def test_it_handles_base_uri_change_folder_in_subschema
assert(schema.valid?({ 'list' => [1] }))
refute(schema.valid?({ 'list' => ['a'] }))
end

def test_it_handles_relative_base_uri_json_pointer_ref
refs = {
'relative' => {
'definitions' => {
'foo' => {
'type' => 'integer'
}
},
'properties' => {
'bar' => {
'$ref' => '#/definitions/foo'
}
}
}
}
schema = JSONSchemer.schema(
{ '$ref' => 'relative' },
:ref_resolver => proc { |uri| refs[uri.to_s] }
)
assert(schema.valid?({ 'bar' => 1 }))
refute(schema.valid?({ 'bar' => '1' }))
end
end

0 comments on commit 8ac15d8

Please sign in to comment.