Skip to content

Commit

Permalink
Allow exclusive refs into sibling definitions
Browse files Browse the repository at this point in the history
I don't think this is really correct according to the
[specification][0], but it matches the previous behavior and seems
useful. Drafts after 7 don't have a problem because they allow all
keywords as `$ref` siblings.

Related:

- #44 (comment)
- json-schema-org/JSON-Schema-Test-Suite#458

[0]: https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-8.3
  • Loading branch information
davishmcclurg committed Aug 20, 2023
1 parent 410c3d7 commit 1f19ca6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/json_schemer/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ def parse

if value.key?('$ref') && keywords.fetch('$ref').exclusive?
@parsed['$ref'] = keywords.fetch('$ref').new(value.fetch('$ref'), self, '$ref')
defs_keyword = meta_schema.defs_keyword
if value.key?(defs_keyword) && keywords.key?(defs_keyword)
@parsed[defs_keyword] = keywords.fetch(defs_keyword).new(value.fetch(defs_keyword), self, defs_keyword)
end
else
keyword_order = meta_schema.keyword_order
last = keywords.size
Expand Down
15 changes: 15 additions & 0 deletions test/ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,19 @@ def test_it_handles_relative_base_uri_json_pointer_ref
assert(schema.valid?({ 'bar' => 1 }))
refute(schema.valid?({ 'bar' => '1' }))
end

def test_exclusive_ref_supports_definitions
schema = JSONSchemer.schema({
'$schema' => 'http://json-schema.org/draft-07/schema#',
'$ref' => '#yah',
'definitions' => {
'yah' => {
'$id' => '#yah',
'type' => 'integer'
}
}
})
assert(schema.valid?(1))
refute(schema.valid?('1'))
end
end

0 comments on commit 1f19ca6

Please sign in to comment.